not yet working so far

This commit is contained in:
2022-10-10 17:25:24 +02:00
parent 84ce8640e9
commit 9a6bdc5738
2 changed files with 38 additions and 12 deletions

View File

@ -2,10 +2,15 @@ import secrets
SSID = 'telemetry'
WPA_KEY = secrets.WPA_KEY
MQTT_SERVER = '172.16.10.47'
MQTT_LOGIN = 'thermo-anna'
MQTT_PASSWORD = secrets.MQTT_PASSWORD
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
WATCHDOG_TOPIC = b'IoT/Watchdog'
PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup'
MAX_CONNECT_RETRY_CNT = 1000000
MAX_CONNECT_RETRY_CNT = 25
MAIN_PERIOD_MS = 60000

43
main.py
View File

@ -15,20 +15,32 @@ ringFlag = False
wdt = None
wdtCnt = 0
client = None
wlan = None
def init():
global wdt
wdt = WDT(timeout=8000)
wdt.feed()
print("Watchdog enabled")
global wlan
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):
print("Not yet connected")
wdt.feed()
connectCnt += 1
time.sleep(1)
if (not wlan.isconnected()):
print("Failed to connect to wlan, restarting")
machine.reset()
print('Connected to WLAN')
print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}")
global client
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
@ -39,19 +51,15 @@ def init():
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
client.subscribe(config.WATCHDOG_TOPIC)
print(f"Subscribed to {config.WATCHDOG_TOPIC}")
global wdt
wdt = WDT(timeout=5000)
wdt.feed()
print("Watchdog enabled")
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
def mqtt_callback(topic, msg):
global wdt, wdtCnt
print(f"Received: {topic}: {msg}")
# print(f"Received: {topic}: {msg}")
if topic == config.WATCHDOG_TOPIC:
wdtCnt += 1
print(f"feed watchdog {wdtCnt}")
# print(f"feed watchdog {wdtCnt}")
wdt.feed()
def main():
@ -66,15 +74,28 @@ def main():
time.sleep(15)
machine.reset()
last_time = time.ticks_ms()
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) > 250):
last_time = current_time
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()