Use mqtt robust, use config file
This commit is contained in:
parent
9ee2603137
commit
71247d0154
23
uP/main.py
23
uP/main.py
|
@ -2,26 +2,37 @@ from machine import Pin
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import machine
|
import machine
|
||||||
import dht
|
import dht
|
||||||
from umqtt.simple import MQTTClient
|
from umqtt.robust import MQTTClient
|
||||||
import json
|
import json
|
||||||
import gc
|
import gc
|
||||||
|
|
||||||
|
with open("config.json", 'r') as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
print(config)
|
||||||
|
|
||||||
led = Pin(2, Pin.OUT)
|
led = Pin(2, Pin.OUT)
|
||||||
d = dht.DHT11(Pin(16))
|
d = dht.DHT11(Pin(12))
|
||||||
adc = machine.ADC(0)
|
adc = machine.ADC(0)
|
||||||
server="10.1.1.162"
|
|
||||||
c = MQTTClient("umqtt_client", server)
|
c = MQTTClient("umqtt_client", config["mqtt"]["server"],ssl=True, user=config["mqtt"]["user"], password=config["mqtt"]["password"])
|
||||||
gc.enable()
|
gc.enable()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
led(0)
|
led(0)
|
||||||
d.measure()
|
d.measure()
|
||||||
data = {'temp': d.temperature(), 'humid': d.humidity(),'light': adc.read(), 'id': 1}
|
data = {'temp': d.temperature(), 'humid': d.humidity(),'light': adc.read(), 'id': config["id"]}
|
||||||
print(data)
|
print(data)
|
||||||
led(1)
|
led(1)
|
||||||
|
try:
|
||||||
c.connect()
|
c.connect()
|
||||||
c.publish(b"sensors", json.dumps(data))
|
c.publish(b"sensors", json.dumps(data))
|
||||||
|
finally:
|
||||||
c.disconnect()
|
c.disconnect()
|
||||||
gc.collect()
|
gc.collect()
|
||||||
sleep(2)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue