datalogger/uP/main.py

39 lines
736 B
Python

from machine import Pin
from time import sleep
import machine
import dht
from umqtt.robust import MQTTClient
import json
import gc
with open("config.json", 'r') as f:
config = json.load(f)
print(config)
led = Pin(2, Pin.OUT)
d = dht.DHT11(Pin(12))
adc = machine.ADC(0)
c = MQTTClient("umqtt_client", config["mqtt"]["server"],ssl=True, user=config["mqtt"]["user"], password=config["mqtt"]["password"])
gc.enable()
while True:
led(0)
d.measure()
data = {'temp': d.temperature(), 'humid': d.humidity(),'light': adc.read(), 'id': config["id"]}
print(data)
led(1)
try:
c.connect()
c.publish(b"sensors", json.dumps(data))
finally:
c.disconnect()
gc.collect()