|
13 | 13 | import wifi |
14 | 14 | import displayio |
15 | 15 | import adafruit_ntp |
| 16 | +import adafruit_connection_manager |
16 | 17 | from adafruit_display_text import bitmap_label, wrap_text_to_lines |
17 | 18 | from adafruit_bitmap_font import bitmap_font |
18 | 19 | from adafruit_azureiot import IoTCentralDevice |
19 | 20 | import adafruit_bme680 |
20 | | -from adafruit_lc709203f import LC709203F, PackSize |
| 21 | +import adafruit_max1704x |
| 22 | +#from adafruit_lc709203f import LC709203F, PackSize |
21 | 23 |
|
22 | 24 |
|
23 | 25 | # Get wifi details and more from a secrets.py file |
|
33 | 35 | print("Connected to WiFi!") |
34 | 36 |
|
35 | 37 | # ntp clock - update tz_offset to your timezone |
36 | | -pool = socketpool.SocketPool(wifi.radio) |
| 38 | +pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) |
| 39 | +ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) |
37 | 40 | ntp = adafruit_ntp.NTP(pool, tz_offset=-4) |
38 | 41 | rtc.RTC().datetime = ntp.datetime |
39 | 42 |
|
|
97 | 100 | esp = None |
98 | 101 | pool = socketpool.SocketPool(wifi.radio) |
99 | 102 | device = IoTCentralDevice( |
100 | | - pool, esp, secrets["id_scope"], secrets["device_id"], secrets["device_primary_key"] |
| 103 | + pool, ssl_context, secrets["id_scope"], secrets["device_id"], secrets["device_primary_key"] |
101 | 104 | ) |
102 | 105 |
|
103 | 106 | print("Connecting to Azure IoT Central...") |
|
108 | 111 | temperature_offset = -5 |
109 | 112 |
|
110 | 113 | # Create sensor object, using the board's default I2C bus. |
111 | | -battery_monitor = LC709203F(i2c) |
| 114 | +#battery_monitor = LC709203F(i2c) |
| 115 | +battery_monitor = adafruit_max1704x.MAX17048(board.I2C()) |
112 | 116 |
|
113 | 117 | # Update to match the mAh of your battery for more accurate readings. |
114 | 118 | # Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000. |
115 | 119 | # Choose the closest match. Include "PackSize." before it, as shown. |
116 | | -battery_monitor.pack_size = PackSize.MAH2000 |
| 120 | +#battery_monitor.pack_size = PackSize.MAH2000 |
117 | 121 |
|
118 | 122 | temp = int((bme680.temperature * 9/5) + (32 + temperature_offset)) |
119 | 123 | humidity = int(bme680.relative_humidity) |
|
161 | 165 |
|
162 | 166 | while True: |
163 | 167 | try: |
164 | | - # read BME sensor |
| 168 | + # read BME sensor |
165 | 169 | temp = int((bme680.temperature * 9/5) + (32 + temperature_offset)) |
166 | 170 | humidity = int(bme680.relative_humidity) |
167 | 171 | pressure = int(bme680.pressure) |
168 | | - # log battery % |
| 172 | + # log battery % |
169 | 173 | battery = battery_monitor.cell_percent |
170 | | - # map range of battery charge to rectangle size on screen |
| 174 | + # map range of battery charge to rectangle size on screen |
171 | 175 | battery_display = round(simpleio.map_range(battery, 0, 100, 0, 22)) |
172 | | - # update rectangle to reflect battery charge |
| 176 | + # update rectangle to reflect battery charge |
173 | 177 | rect.width = int(battery_display) |
174 | | - # if below 20%, change rectangle color to red |
| 178 | + # if below 20%, change rectangle color to red |
175 | 179 | if battery_monitor.cell_percent < 20: |
176 | 180 | rect.color_index = 1 |
177 | | - # when the azure clock runs out |
| 181 | + # when the azure clock runs out |
178 | 182 | if azure_clock > 500: |
179 | 183 | print("getting ntp date/time") |
180 | 184 | cal = ntp.datetime |
|
185 | 189 | minute = cal[4] |
186 | 190 | time.sleep(2) |
187 | 191 | print("getting msg") |
188 | | - # pack message |
| 192 | + # pack message |
189 | 193 | message = {"Temperature": temp, |
190 | 194 | "Humidity": humidity, |
191 | 195 | "Pressure": pressure, |
|
199 | 203 | print("updating time text") |
200 | 204 | time_text.text="\n".join(wrap_text_to_lines |
201 | 205 | ("Data sent on %s/%s/%s at %s" % (mon,day,year,clock_view), 20)) |
202 | | - # reset azure clock |
| 206 | + # reset azure clock |
203 | 207 | azure_clock = 0 |
204 | | - # when the feather clock runs out |
| 208 | + # when the feather clock runs out |
205 | 209 | if feather_clock > 30: |
206 | 210 | print("updating screen") |
207 | 211 | temp_text.text = "%0.1f° F" % temp |
208 | 212 | humid_text.text = "%0.1f %%" % humidity |
209 | 213 | press_text.text = "%0.2f" % pressure |
210 | | - # reset feather clock |
| 214 | + # reset feather clock |
211 | 215 | feather_clock = 0 |
212 | | - # if no clocks are running out |
213 | | - # increase counts by 1 |
| 216 | + # if no clocks are running out |
| 217 | + # increase counts by 1 |
214 | 218 | else: |
215 | 219 | feather_clock += 1 |
216 | 220 | azure_clock += 1 |
217 | | - # ping azure |
| 221 | + # ping azure |
218 | 222 | device.loop() |
219 | | - # if something disrupts the loop, reconnect |
| 223 | + # if something disrupts the loop, reconnect |
220 | 224 | # pylint: disable=broad-except |
221 | 225 | except (ValueError, RuntimeError, OSError, ConnectionError) as e: |
222 | 226 | print("Network error, reconnecting\n", str(e)) |
223 | 227 | supervisor.reload() |
224 | 228 | continue |
225 | | - # delay |
| 229 | + # delay |
226 | 230 | time.sleep(1) |
227 | 231 | print(azure_clock) |
| 232 | + |
0 commit comments