add sensor stuff
This commit is contained in:
@ -7,6 +7,8 @@ MQTT_SERVER = '172.16.10.47'
|
|||||||
MQTT_LOGIN = 'thermo-anna'
|
MQTT_LOGIN = 'thermo-anna'
|
||||||
MQTT_PASSWORD = secrets.MQTT_PASSWORD
|
MQTT_PASSWORD = secrets.MQTT_PASSWORD
|
||||||
|
|
||||||
|
LOCATION = "AnnaKoeln"
|
||||||
|
|
||||||
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
|
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
|
||||||
WATCHDOG_TOPIC = b'IoT/Watchdog'
|
WATCHDOG_TOPIC = b'IoT/Watchdog'
|
||||||
PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
|
PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
|
||||||
|
26
main.py
26
main.py
@ -2,12 +2,16 @@ import machine
|
|||||||
from umqttsimple import MQTTClient
|
from umqttsimple import MQTTClient
|
||||||
import time
|
import time
|
||||||
import ubinascii
|
import ubinascii
|
||||||
# from machine import WDT
|
from machine import Pin
|
||||||
|
from onewire import OneWire
|
||||||
|
from ds18x20 import DS18X20
|
||||||
import network
|
import network
|
||||||
import os
|
import json
|
||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_PIN = 16
|
||||||
|
|
||||||
|
|
||||||
print("Hello")
|
print("Hello")
|
||||||
resetCause = machine.reset_cause()
|
resetCause = machine.reset_cause()
|
||||||
@ -42,7 +46,7 @@ try:
|
|||||||
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
|
||||||
# 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:
|
except Exception as e:
|
||||||
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
print(f"Some error when starting: {e.__class__.__name__}, {e}")
|
||||||
time.sleep(15)
|
time.sleep(15)
|
||||||
@ -50,8 +54,20 @@ except Exception as e:
|
|||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client.publish(config.PAYLOAD_TOPIC, f"measurement")
|
sensor = DS18X20(OneWire(Pin(SENSOR_PIN, Pin.IN, Pin.PULL_UP)))
|
||||||
print("Payload sent")
|
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()
|
# wdt.feed()
|
||||||
|
|
||||||
client.disconnect()
|
client.disconnect()
|
||||||
|
Reference in New Issue
Block a user