change cycle approach

This commit is contained in:
Wolfgang Hottgenroth 2022-10-18 13:14:07 +02:00
parent a45cccad06
commit 5d5c27d8d6
Signed by: wn
GPG Key ID: 836E9E1192A6B132
3 changed files with 43 additions and 42 deletions

View File

@ -4,10 +4,10 @@ SSID = 'gast1000'
WPA_KEY = secrets.WPA_KEY WPA_KEY = secrets.WPA_KEY
MQTT_SERVER = 'broker.hottis.de' MQTT_SERVER = 'broker.hottis.de'
MQTT_LOGIN = 'thermo-anna' MQTT_LOGIN = secrets.MQTT_LOGIN
MQTT_PASSWORD = secrets.MQTT_PASSWORD MQTT_PASSWORD = secrets.MQTT_PASSWORD
LOCATION = "AnnaKoeln" LOCATION = secrets.LOCATION
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat' HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
WATCHDOG_TOPIC = b'IoT/Watchdog' WATCHDOG_TOPIC = b'IoT/Watchdog'

29
main.py
View File

@ -39,6 +39,14 @@ try:
print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}") print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}")
wdt.feed() wdt.feed()
except Exception as e:
print(f"Some error when starting: {e.__class__.__name__}, {e}")
print("Restarting")
time.sleep(5)
machine.reset()
while True:
try:
clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}" clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}"
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)
@ -47,13 +55,6 @@ try:
wdt.feed() wdt.feed()
# 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(5)
machine.reset()
try:
sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP))) sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP)))
devices = sensor.scan() devices = sensor.scan()
sensor.convert_temp() sensor.convert_temp()
@ -72,19 +73,15 @@ try:
client.disconnect() client.disconnect()
print("Disconnected from broker") 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:
for w in range(config.MAIN_PERIOD_S): for w in range(config.MAIN_PERIOD_S):
print(f"Waiting {w}...") print(f"Waiting {w}...")
wdt.feed() wdt.feed()
time.sleep(1) time.sleep(1)
except Exception as e:
print(f"Error in main loop: {e.__class__.__name__}, {e}")
print("Restarting") print("Restarting")
time.sleep(1) time.sleep(5)
machine.reset() machine.reset()

View File

@ -1,2 +1,6 @@
WPA_KEY = 'geheim' WPA_KEY = 'geheim'
MQTT_LOGIN = 'testuser'
MQTT_PASSWORD = 'geheim' MQTT_PASSWORD = 'geheim'
LOCATION = "somewhere"