Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dht
- import machine
- import network
- import utime
- from umqtt.simple import MQTTClient
- NODEMCU_PINS = (16, 5, 4, 0, 2, 14, 12, 13, 15, 3, 1, 9, 10)
- d = dht.DHT11(machine.Pin(NODEMCU_PINS[4]))
- sta_if = network.WLAN(network.STA_IF)
- while True:
- if not sta_if.isconnected():
- print('Connecting to WiFi...')
- sta_if.active(True)
- sta_if.connect('essid', 'password')
- while not sta_if.isconnected():
- utime.sleep(1)
- print('Connecting to MQTT server...')
- c = MQTTClient("unique-client-id", "mqtt-server-addr", user="user", password="password")
- c.connect()
- d.measure()
- print('Publishing sensors...')
- c.publish("node/temp", str(d.temperature()), retain=True, qos=1)
- c.publish("node/hum", str(d.humidity()), retain=True, qos=1)
- c.disconnect()
- utime.sleep(5)
- rtc = machine.RTC()
- rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
- rtc.alarm(rtc.ALARM0, 60000)
- print('Zzzzz...')
- machine.deepsleep()
Add Comment
Please, Sign In to add comment