-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
53 lines (45 loc) · 1.29 KB
/
main.py
1
import ubinasciiimport timeimport machineimport jsonfrom connect import Connectfrom led_pwm import LedPwmfrom lcd import Lcdfrom temperature_dht11 import TemperatureSensor as Dhtfrom mqtt import Mqttdht = Dht(4)led = LedPwm(2)lcd = Lcd(spi=1, cs=2, dc=15, rst=0, bl=12)config = json.loads(open('config.json').read())client_id = ubinascii.hexlify(machine.unique_id())try: mqtt = Mqtt(client_id, config['broker_ip'])except: mqtt = Falsewhile True: # Connect to wifi Connect.check_and_reconnect() try: data = {'temperature': dht.read_temp_and_hum()[0], 'humidity': dht.read_temp_and_hum()[1]} print(str(time.localtime()) + ' ' + str(data)) if not mqtt: try: mqtt = Mqtt(client_id, config['broker_ip']) except: mqtt = False if mqtt: try: mqtt.publish(data, topic='esp/tempandhum') except: mqtt = False publish_msg = 'Pub ok' else: publish_msg = 'Not pub' lcd.text('temp: ' + str(data['temperature']), 'hum: ' + str(data['humidity']), '=============', publish_msg) led.blink_duty(2, 0.1, 1010) except: pass time.sleep(5)