different sleep approach
This commit is contained in:
@ -13,4 +13,4 @@ PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
|
|||||||
STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup'
|
STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup'
|
||||||
|
|
||||||
MAX_CONNECT_RETRY_CNT = 25
|
MAX_CONNECT_RETRY_CNT = 25
|
||||||
MAIN_PERIOD_MS = 60000
|
MAIN_PERIOD_S = 60
|
110
main.py
110
main.py
@ -1,38 +1,30 @@
|
|||||||
# main.py -- put your code here!
|
import machine
|
||||||
|
from umqttsimple import MQTTClient
|
||||||
from umqttsimple import MQTTClient, MQTTException
|
|
||||||
import time
|
import time
|
||||||
import ubinascii
|
import ubinascii
|
||||||
from machine import WDT
|
# from machine import WDT
|
||||||
import network
|
import network
|
||||||
import os
|
import os
|
||||||
import config
|
import config
|
||||||
|
|
||||||
last_message = 0
|
|
||||||
message_interval = 5
|
|
||||||
counter = 0
|
|
||||||
ringFlag = False
|
|
||||||
wdt = None
|
|
||||||
wdtCnt = 0
|
|
||||||
client = None
|
|
||||||
wlan = None
|
|
||||||
|
|
||||||
|
|
||||||
def init():
|
print("Hello")
|
||||||
global wdt
|
resetCause = machine.reset_cause()
|
||||||
wdt = WDT(timeout=8000)
|
|
||||||
wdt.feed()
|
|
||||||
print("Watchdog enabled")
|
|
||||||
|
|
||||||
global wlan
|
# wdt = WDT(timeout=8000)
|
||||||
|
# wdt.feed()
|
||||||
|
# print("Watchdog enabled")
|
||||||
|
|
||||||
|
try:
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
wlan.connect(config.SSID, config.WPA_KEY)
|
wlan.connect(config.SSID, config.WPA_KEY)
|
||||||
|
|
||||||
connectCnt = 0
|
connectCnt = 0
|
||||||
while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT):
|
while (not wlan.isconnected() or (connectCnt > config.MAX_CONNECT_RETRY_CNT)):
|
||||||
print("Not yet connected")
|
print("Not yet connected")
|
||||||
wdt.feed()
|
# wdt.feed()
|
||||||
connectCnt += 1
|
connectCnt += 1
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
@ -40,64 +32,42 @@ def init():
|
|||||||
print("Failed to connect to wlan, restarting")
|
print("Failed to connect to wlan, restarting")
|
||||||
machine.reset()
|
machine.reset()
|
||||||
|
|
||||||
print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}")
|
print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}")
|
||||||
|
# wdt.feed()
|
||||||
|
|
||||||
global client
|
clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}"
|
||||||
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
|
|
||||||
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||||
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||||
client.set_callback(mqtt_callback)
|
|
||||||
client.connect()
|
client.connect()
|
||||||
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||||
client.subscribe(config.WATCHDOG_TOPIC)
|
# wdt.feed()
|
||||||
print(f"Subscribed to {config.WATCHDOG_TOPIC}")
|
|
||||||
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
|
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
||||||
|
time.sleep(15)
|
||||||
|
machine.reset()
|
||||||
|
|
||||||
|
|
||||||
def mqtt_callback(topic, msg):
|
try:
|
||||||
global wdt, wdtCnt
|
client.publish(config.PAYLOAD_TOPIC, f"measurement")
|
||||||
# print(f"Received: {topic}: {msg}")
|
print("Payload sent")
|
||||||
if topic == config.WATCHDOG_TOPIC:
|
# wdt.feed()
|
||||||
wdtCnt += 1
|
|
||||||
# print(f"feed watchdog {wdtCnt}")
|
|
||||||
wdt.feed()
|
|
||||||
|
|
||||||
def main():
|
|
||||||
global client
|
|
||||||
print("Hello")
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
try:
|
|
||||||
init()
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
|
||||||
time.sleep(15)
|
|
||||||
machine.reset()
|
|
||||||
|
|
||||||
last_time_mqtt_loop = time.ticks_ms()
|
|
||||||
last_time_1_second = time.ticks_ms()
|
|
||||||
last_time_main_period = time.ticks_ms()
|
|
||||||
payload_cnt = 0
|
|
||||||
while True:
|
|
||||||
current_time = time.ticks_ms()
|
|
||||||
|
|
||||||
if (time.ticks_diff(current_time, last_time_mqtt_loop) > 250):
|
|
||||||
last_time_mqtt_loop = current_time
|
|
||||||
client.ping()
|
|
||||||
client.wait_msg()
|
|
||||||
|
|
||||||
if (time.ticks_diff(current_time, last_time_1_second) > 1000):
|
|
||||||
last_time_1_second = current_time
|
|
||||||
client.publish(config.HEARTBEAT_TOPIC, f"still alive {current_time}")
|
|
||||||
|
|
||||||
if (time.ticks_diff(current_time, last_time_main_period) > config.MAIN_PERIOD_MS):
|
|
||||||
last_time_main_period = current_time
|
|
||||||
client.publish(config.PAYLOAD_TOPIC, f"measurement {payload_cnt}")
|
|
||||||
payload_cnt += 1
|
|
||||||
print("Payload sent")
|
|
||||||
|
|
||||||
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
print("Disconnected from broker")
|
||||||
|
time.sleep(1)
|
||||||
|
wlan.disconnect()
|
||||||
|
wlan.active(False)
|
||||||
|
print("Disconnected from network")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error in main loop: {e.__class__.__name__}, {e}")
|
||||||
|
finally:
|
||||||
|
print("Waiting ...")
|
||||||
|
# wdt.feed()
|
||||||
|
|
||||||
|
time.sleep(config.MAIN_PERIOD_S)
|
||||||
|
print("Restarting")
|
||||||
|
time.sleep(1)
|
||||||
|
machine.reset()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Reference in New Issue
Block a user