change cycle approach
This commit is contained in:
parent
a45cccad06
commit
5d5c27d8d6
@ -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'
|
||||||
|
77
main.py
77
main.py
@ -39,52 +39,49 @@ try:
|
|||||||
print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}")
|
print(f"Connected to WLAN: {resetCause} {connectCnt} {wlan.ifconfig()}")
|
||||||
wdt.feed()
|
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:
|
except Exception as e:
|
||||||
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
||||||
|
print("Restarting")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
machine.reset()
|
machine.reset()
|
||||||
|
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP)))
|
clientId = f"rpipwtherm-{ubinascii.hexlify(machine.unique_id())}"
|
||||||
devices = sensor.scan()
|
client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60,
|
||||||
sensor.convert_temp()
|
ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD)
|
||||||
time.sleep(1)
|
client.connect()
|
||||||
for idx, device in enumerate(devices):
|
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||||
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}...")
|
|
||||||
wdt.feed()
|
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)
|
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()
|
|
||||||
|
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
WPA_KEY = 'geheim'
|
WPA_KEY = 'geheim'
|
||||||
|
|
||||||
|
MQTT_LOGIN = 'testuser'
|
||||||
MQTT_PASSWORD = 'geheim'
|
MQTT_PASSWORD = 'geheim'
|
||||||
|
|
||||||
|
LOCATION = "somewhere"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user