bluething IoT Sensor based on ESP8266
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # bluething-sensor
  2. #
  3. # This code reads the temperature from an MCP9808 and sends it via MQTT to
  4. # a remote server. Then the device is send into deep sleep to be able to
  5. # run from a battery for a long time.
  6. #
  7. # Developed and tested with an Adafruit Feather HUZZAH and Adafruit MCP9808
  8. # on MicroPython 1.8.6
  9. from machine import Pin, I2C
  10. from mcp9808 import MCP9808, T_RES_AVG
  11. from umqtt.simple import MQTTClient
  12. import time
  13. import utime
  14. import network
  15. import ubinascii
  16. import machine
  17. import ntptime
  18. WLAN_SSID = "<your-ssid>"
  19. WLAN_PSK = "<pre-shared-key>"
  20. MQTT_HOST = "test.mosquitto.org"
  21. MQTT_PORT = 1883
  22. MQTT_USER = ""
  23. MQTT_PASS = ""
  24. NTP_HOST = "pool.ntp.org"
  25. # Interval to measure temperature (minutes)
  26. SEND_INTERVAL = 1
  27. def connectWiFi():
  28. sta_if = network.WLAN(network.STA_IF)
  29. if not sta_if.isconnected():
  30. print('connecting to network ...')
  31. sta_if.active(True)
  32. sta_if.connect(WLAN_SSID, WLAN_PSK)
  33. while not sta_if.isconnected():
  34. time.sleep_ms(50)
  35. print('network config: ', sta_if.ifconfig())
  36. def getDeviceId():
  37. return "ESP8266-{}".format(ubinascii.hexlify(machine.unique_id()))
  38. def pubMQTT(temperature, timestamp):
  39. c = MQTTClient(getDeviceId(), MQTT_HOST, MQTT_PORT, MQTT_USER, MQTT_PASS)
  40. c.connect()
  41. c.publish("/sensors/{}/temperature".format(getDeviceId()), "{}|{}".format(timestamp, temperature), False, 1)
  42. c.disconnect()
  43. def getTemperature():
  44. i2c = I2C(Pin(5), Pin(4))
  45. sensor = MCP9808(i2c)
  46. sensor.set_shutdown_mode(False)
  47. sensor.set_resolution(T_RES_AVG)
  48. # give it some time to measure
  49. time.sleep_ms(130)
  50. temp = sensor.get_temp()
  51. sensor.set_shutdown_mode(True)
  52. return temp
  53. def sleepToFullMinute(minutes):
  54. localtime = utime.localtime(utime.time() + minutes*60)
  55. return minutes*60 - localtime[5] + 30
  56. def deepsleep(sleepsec):
  57. rtc = machine.RTC()
  58. rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
  59. rtc.alarm(rtc.ALARM0, sleepsec*1000)
  60. # this will actually do a reset
  61. machine.deepsleep()
  62. def unixTimestamp():
  63. # Offset Unix timestamp (1.1.1970) to RTC (1.1.2000)
  64. RTC_OFFSET = 946684800
  65. return utime.time()+RTC_OFFSET
  66. temp = getTemperature()
  67. print('Temp: ', temp)
  68. connectWiFi()
  69. ntptime.host = NTP_HOST
  70. ntptime.settime()
  71. lt = utime.localtime()
  72. print('{}/{}/{} {}:{}:{}'.format(lt[0], lt[1], lt[2], lt[3], lt[4], lt[5]))
  73. pubMQTT("{:.1f}".format(temp), unixTimestamp())
  74. # this will result in a reset
  75. deepsleep(sleepToFullMinute(SEND_INTERVAL))