Browse Source

First full code drop

Bernhard Fröhlich 2 years ago
parent
commit
8765dd12c4
2 changed files with 104 additions and 0 deletions
  1. 103
    0
      main.py
  2. 1
    0
      mcp9808.py

+ 103
- 0
main.py View File

@@ -0,0 +1,103 @@
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
+
10
+from machine import Pin, I2C
11
+from mcp9808 import MCP9808, T_RES_AVG
12
+from umqtt.simple import MQTTClient
13
+import time
14
+import utime
15
+import network
16
+import ubinascii
17
+import machine
18
+import ntptime
19
+
20
+WLAN_SSID = "<your-ssid>"
21
+WLAN_PSK = "<pre-shared-key>"
22
+
23
+MQTT_HOST = "test.mosquitto.org"
24
+MQTT_PORT = 1883
25
+MQTT_USER = ""
26
+MQTT_PASS = ""
27
+
28
+NTP_HOST = "pool.ntp.org"
29
+
30
+# Interval to measure temperature (minutes)
31
+SEND_INTERVAL = 1
32
+
33
+
34
+def connectWiFi():
35
+   sta_if = network.WLAN(network.STA_IF)
36
+
37
+   if not sta_if.isconnected():
38
+      print('connecting to network ...')
39
+      sta_if.active(True)
40
+      sta_if.connect(WLAN_SSID, WLAN_PSK)
41
+
42
+      while not sta_if.isconnected():
43
+         time.sleep_ms(50)
44
+
45
+   print('network config: ', sta_if.ifconfig())
46
+
47
+def getDeviceId():
48
+   return "ESP8266-{}".format(ubinascii.hexlify(machine.unique_id()))
49
+
50
+def pubMQTT(temperature, timestamp):
51
+   c = MQTTClient(getDeviceId(), MQTT_HOST, MQTT_PORT, MQTT_USER, MQTT_PASS)
52
+   c.connect()
53
+   c.publish("/sensors/{}/temperature".format(getDeviceId()), "{}|{}".format(timestamp, temperature), False, 1)
54
+   c.disconnect()
55
+
56
+def getTemperature():
57
+   i2c = I2C(Pin(5), Pin(4))
58
+
59
+   sensor = MCP9808(i2c)
60
+   sensor.set_shutdown_mode(False)
61
+   sensor.set_resolution(T_RES_AVG)
62
+
63
+   # give it some time to measure
64
+   time.sleep_ms(130)
65
+
66
+   temp = sensor.get_temp()
67
+   sensor.set_shutdown_mode(True)
68
+
69
+   return temp
70
+
71
+def sleepToFullMinute(minutes):
72
+   localtime = utime.localtime(utime.time() + minutes*60)
73
+   return minutes*60 - localtime[5] + 30
74
+
75
+def deepsleep(sleepsec):
76
+   rtc = machine.RTC()
77
+   rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
78
+   rtc.alarm(rtc.ALARM0, sleepsec*1000)
79
+
80
+   # this will actually do a reset
81
+   machine.deepsleep()
82
+
83
+def unixTimestamp():
84
+   # Offset Unix timestamp (1.1.1970) to RTC (1.1.2000)
85
+   RTC_OFFSET = 946684800
86
+   return utime.time()+RTC_OFFSET
87
+
88
+
89
+temp = getTemperature()
90
+print('Temp: ', temp)
91
+
92
+connectWiFi()
93
+
94
+ntptime.host = NTP_HOST
95
+ntptime.settime()
96
+
97
+lt = utime.localtime()
98
+print('{}/{}/{} {}:{}:{}'.format(lt[0], lt[1], lt[2], lt[3], lt[4], lt[5]))
99
+
100
+pubMQTT("{:.1f}".format(temp), unixTimestamp())
101
+
102
+# this will result in a reset
103
+deepsleep(sleepToFullMinute(SEND_INTERVAL))

+ 1
- 0
mcp9808.py View File

@@ -1,3 +1,4 @@
1
+# micropython-mcp9808
1 2
 from machine import I2C
2 3
 
3 4
 R_CFG    = const(1)

Loading…
Cancel
Save