datalogger/uP/main.py

39 lines
736 B
Python
Raw Permalink Normal View History

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