add sensor stuff

This commit is contained in:
2022-10-11 19:14:18 +02:00
parent 482704896d
commit 6cb101108b
2 changed files with 23 additions and 5 deletions

View File

@ -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'

26
main.py
View File

@ -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()