From 6cb101108b27db258e312ddc3ad4d21d867bed22 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 11 Oct 2022 19:14:18 +0200 Subject: [PATCH] add sensor stuff --- config.py | 2 ++ main.py | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index ebd9ec1..a56a403 100644 --- a/config.py +++ b/config.py @@ -7,6 +7,8 @@ MQTT_SERVER = '172.16.10.47' MQTT_LOGIN = 'thermo-anna' MQTT_PASSWORD = secrets.MQTT_PASSWORD +LOCATION = "AnnaKoeln" + HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat' WATCHDOG_TOPIC = b'IoT/Watchdog' PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload' diff --git a/main.py b/main.py index 1961d0d..36d7551 100644 --- a/main.py +++ b/main.py @@ -2,12 +2,16 @@ import machine from umqttsimple import MQTTClient import time import ubinascii -# from machine import WDT +from machine import Pin +from onewire import OneWire +from ds18x20 import DS18X20 import network -import os +import json import config +SENSOR_PIN = 16 + print("Hello") resetCause = machine.reset_cause() @@ -42,7 +46,7 @@ try: 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()}") + # 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) @@ -50,8 +54,20 @@ except Exception as e: try: - client.publish(config.PAYLOAD_TOPIC, f"measurement") - print("Payload sent") + 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()