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'
|
||||
|
||||
MAX_CONNECT_RETRY_CNT = 25
|
||||
MAIN_PERIOD_MS = 60000
|
||||
MAIN_PERIOD_S = 60
|
112
main.py
112
main.py
@ -1,38 +1,30 @@
|
||||
# main.py -- put your code here!
|
||||
|
||||
from umqttsimple import MQTTClient, MQTTException
|
||||
import machine
|
||||
from umqttsimple import MQTTClient
|
||||
import time
|
||||
import ubinascii
|
||||
from machine import WDT
|
||||
# from machine import WDT
|
||||
import network
|
||||
import os
|
||||
import config
|
||||
|
||||
last_message = 0
|
||||
message_interval = 5
|
||||
counter = 0
|
||||
ringFlag = False
|
||||
wdt = None
|
||||
wdtCnt = 0
|
||||
client = None
|
||||
wlan = None
|
||||
|
||||
|
||||
def init():
|
||||
global wdt
|
||||
wdt = WDT(timeout=8000)
|
||||
wdt.feed()
|
||||
print("Watchdog enabled")
|
||||
print("Hello")
|
||||
resetCause = machine.reset_cause()
|
||||
|
||||
global wlan
|
||||
# wdt = WDT(timeout=8000)
|
||||
# wdt.feed()
|
||||
# print("Watchdog enabled")
|
||||
|
||||
try:
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(config.SSID, config.WPA_KEY)
|
||||
|
||||
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")
|
||||
wdt.feed()
|
||||
# wdt.feed()
|
||||
connectCnt += 1
|
||||
time.sleep(1)
|
||||
|
||||
@ -40,64 +32,42 @@ def init():
|
||||
print("Failed to connect to wlan, restarting")
|
||||
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 = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
|
||||
clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}"
|
||||
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||
client.set_callback(mqtt_callback)
|
||||
client.connect()
|
||||
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||
client.subscribe(config.WATCHDOG_TOPIC)
|
||||
print(f"Subscribed to {config.WATCHDOG_TOPIC}")
|
||||
# wdt.feed()
|
||||
|
||||
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):
|
||||
global wdt, wdtCnt
|
||||
# print(f"Received: {topic}: {msg}")
|
||||
if topic == config.WATCHDOG_TOPIC:
|
||||
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")
|
||||
|
||||
try:
|
||||
client.publish(config.PAYLOAD_TOPIC, f"measurement")
|
||||
print("Payload sent")
|
||||
# wdt.feed()
|
||||
|
||||
client.disconnect()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
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()
|
||||
|
||||
|
Reference in New Issue
Block a user