Guest User

Untitled

a guest
Feb 10th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import dht
  2. import machine
  3. import network
  4. import utime
  5. from umqtt.simple import MQTTClient
  6.  
  7. NODEMCU_PINS = (16, 5, 4, 0, 2, 14, 12, 13, 15, 3, 1, 9, 10)
  8.  
  9. d = dht.DHT11(machine.Pin(NODEMCU_PINS[4]))
  10. sta_if = network.WLAN(network.STA_IF)
  11.  
  12. while True:
  13. if not sta_if.isconnected():
  14. print('Connecting to WiFi...')
  15. sta_if.active(True)
  16. sta_if.connect('essid', 'password')
  17. while not sta_if.isconnected():
  18. utime.sleep(1)
  19.  
  20. print('Connecting to MQTT server...')
  21. c = MQTTClient("unique-client-id", "mqtt-server-addr", user="user", password="password")
  22. c.connect()
  23. d.measure()
  24. print('Publishing sensors...')
  25. c.publish("node/temp", str(d.temperature()), retain=True, qos=1)
  26. c.publish("node/hum", str(d.humidity()), retain=True, qos=1)
  27. c.disconnect()
  28.  
  29. utime.sleep(5)
  30.  
  31. rtc = machine.RTC()
  32. rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
  33. rtc.alarm(rtc.ALARM0, 60000)
  34. print('Zzzzz...')
  35. machine.deepsleep()
Add Comment
Please, Sign In to add comment