diff --git a/config.py b/config.py index 08442b2..dedb202 100644 --- a/config.py +++ b/config.py @@ -4,10 +4,10 @@ SSID = 'gast1000' WPA_KEY = secrets.WPA_KEY MQTT_SERVER = 'broker.hottis.de' -MQTT_LOGIN = 'thermo-anna' +MQTT_LOGIN = secrets.MQTT_LOGIN MQTT_PASSWORD = secrets.MQTT_PASSWORD -LOCATION = "AnnaKoeln" +LOCATION = secrets.LOCATION HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat' WATCHDOG_TOPIC = b'IoT/Watchdog' diff --git a/main.py b/main.py index 553e360..56a3fa3 100644 --- a/main.py +++ b/main.py @@ -39,52 +39,49 @@ try: print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}") wdt.feed() - 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.connect() - print(f"Connected to MQTT broker {config.MQTT_SERVER} ") - 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}") + print("Restarting") time.sleep(5) machine.reset() - -try: - sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP))) - devices = sensor.scan() - sensor.convert_temp() - time.sleep(1) - for idx, device in enumerate(devices): - t = sensor.read_temp(device) - msg = { - "sensor" : idx, - "location": config.LOCATION, - "temperature": t - } - print(f"Sensor: {idx}, {json.dumps(msg)}") - client.publish(config.PAYLOAD_TOPIC, json.dumps(msg)) - print(f"Payload {idx} sent") - wdt.feed() - - 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: - for w in range(config.MAIN_PERIOD_S): - print(f"Waiting {w}...") +while True: + try: + 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.connect() + print(f"Connected to MQTT broker {config.MQTT_SERVER} ") wdt.feed() + + # client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}") + sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP))) + devices = sensor.scan() + sensor.convert_temp() time.sleep(1) + for idx, device in enumerate(devices): + t = sensor.read_temp(device) + msg = { + "sensor" : idx, + "location": config.LOCATION, + "temperature": t + } + print(f"Sensor: {idx}, {json.dumps(msg)}") + client.publish(config.PAYLOAD_TOPIC, json.dumps(msg)) + print(f"Payload {idx} sent") + wdt.feed() + + client.disconnect() + print("Disconnected from broker") + + for w in range(config.MAIN_PERIOD_S): + print(f"Waiting {w}...") + wdt.feed() + time.sleep(1) + except Exception as e: + print(f"Error in main loop: {e.__class__.__name__}, {e}") + print("Restarting") + time.sleep(5) + machine.reset() - print("Restarting") - time.sleep(1) - machine.reset() diff --git a/secrets.py-example b/secrets.py-example index 0ae97c4..02a32d6 100644 --- a/secrets.py-example +++ b/secrets.py-example @@ -1,2 +1,6 @@ WPA_KEY = 'geheim' + +MQTT_LOGIN = 'testuser' MQTT_PASSWORD = 'geheim' + +LOCATION = "somewhere"