Skip to content

Commit cc19eef

Browse files
authored
Merge pull request #2871 from adafruit/azure-ssl-max17048
Azure ESP32-S2 TFT SSL and MAX17048
2 parents 440b1ba + 51ddbd3 commit cc19eef

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

Feather_ESP32-S2_TFT_Azure/code.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import wifi
1414
import displayio
1515
import adafruit_ntp
16+
import adafruit_connection_manager
1617
from adafruit_display_text import bitmap_label, wrap_text_to_lines
1718
from adafruit_bitmap_font import bitmap_font
1819
from adafruit_azureiot import IoTCentralDevice
1920
import adafruit_bme680
20-
from adafruit_lc709203f import LC709203F, PackSize
21+
import adafruit_max1704x
22+
#from adafruit_lc709203f import LC709203F, PackSize
2123

2224

2325
# Get wifi details and more from a secrets.py file
@@ -33,7 +35,8 @@
3335
print("Connected to WiFi!")
3436

3537
# 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)
3740
ntp = adafruit_ntp.NTP(pool, tz_offset=-4)
3841
rtc.RTC().datetime = ntp.datetime
3942

@@ -97,7 +100,7 @@
97100
esp = None
98101
pool = socketpool.SocketPool(wifi.radio)
99102
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"]
101104
)
102105

103106
print("Connecting to Azure IoT Central...")
@@ -108,12 +111,13 @@
108111
temperature_offset = -5
109112

110113
# 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())
112116

113117
# Update to match the mAh of your battery for more accurate readings.
114118
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
115119
# Choose the closest match. Include "PackSize." before it, as shown.
116-
battery_monitor.pack_size = PackSize.MAH2000
120+
#battery_monitor.pack_size = PackSize.MAH2000
117121

118122
temp = int((bme680.temperature * 9/5) + (32 + temperature_offset))
119123
humidity = int(bme680.relative_humidity)
@@ -161,20 +165,20 @@
161165

162166
while True:
163167
try:
164-
# read BME sensor
168+
# read BME sensor
165169
temp = int((bme680.temperature * 9/5) + (32 + temperature_offset))
166170
humidity = int(bme680.relative_humidity)
167171
pressure = int(bme680.pressure)
168-
# log battery %
172+
# log battery %
169173
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
171175
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
173177
rect.width = int(battery_display)
174-
# if below 20%, change rectangle color to red
178+
# if below 20%, change rectangle color to red
175179
if battery_monitor.cell_percent < 20:
176180
rect.color_index = 1
177-
# when the azure clock runs out
181+
# when the azure clock runs out
178182
if azure_clock > 500:
179183
print("getting ntp date/time")
180184
cal = ntp.datetime
@@ -185,7 +189,7 @@
185189
minute = cal[4]
186190
time.sleep(2)
187191
print("getting msg")
188-
# pack message
192+
# pack message
189193
message = {"Temperature": temp,
190194
"Humidity": humidity,
191195
"Pressure": pressure,
@@ -199,29 +203,29 @@
199203
print("updating time text")
200204
time_text.text="\n".join(wrap_text_to_lines
201205
("Data sent on %s/%s/%s at %s" % (mon,day,year,clock_view), 20))
202-
# reset azure clock
206+
# reset azure clock
203207
azure_clock = 0
204-
# when the feather clock runs out
208+
# when the feather clock runs out
205209
if feather_clock > 30:
206210
print("updating screen")
207211
temp_text.text = "%0.1f° F" % temp
208212
humid_text.text = "%0.1f %%" % humidity
209213
press_text.text = "%0.2f" % pressure
210-
# reset feather clock
214+
# reset feather clock
211215
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
214218
else:
215219
feather_clock += 1
216220
azure_clock += 1
217-
# ping azure
221+
# ping azure
218222
device.loop()
219-
# if something disrupts the loop, reconnect
223+
# if something disrupts the loop, reconnect
220224
# pylint: disable=broad-except
221225
except (ValueError, RuntimeError, OSError, ConnectionError) as e:
222226
print("Network error, reconnecting\n", str(e))
223227
supervisor.reload()
224228
continue
225-
# delay
229+
# delay
226230
time.sleep(1)
227231
print(azure_clock)

0 commit comments

Comments
 (0)