Skip to content

Commit 7325cb3

Browse files
brentrubrentru
authored andcommitted
add gfx helper and initial code
1 parent 44a6892 commit 7325cb3

2 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""
2+
GFX Helper for pyportal_azure_iot_temperature.py
3+
"""
4+
import board
5+
import displayio
6+
from adafruit_display_text.label import Label
7+
from adafruit_bitmap_font import bitmap_font
8+
9+
cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
10+
11+
# Fonts within /fonts folder
12+
info_font = cwd+"/fonts/Nunito-Black-17.bdf"
13+
temperature_font = cwd+"/fonts/Nunito-Light-75.bdf"
14+
15+
class Azure_GFX(displayio.Group):
16+
def __init__(self, celsius=False):
17+
"""Creates an Azure_GFX object.
18+
:param bool celsius: Temperature displayed as F or C
19+
"""
20+
# root displayio group
21+
root_group = displayio.Group(max_size=20)
22+
board.DISPLAY.show(root_group)
23+
super().__init__(max_size=20)
24+
25+
self._celsius = celsius
26+
27+
# create background icon group
28+
self._icon_group = displayio.Group(max_size=1)
29+
self.append(self._icon_group)
30+
board.DISPLAY.show(self._icon_group)
31+
# create text object group
32+
self._text_group = displayio.Group(max_size=6)
33+
self.append(self._text_group)
34+
35+
self._icon_sprite = None
36+
self._icon_file = None
37+
self._cwd = cwd
38+
self.set_icon(self._cwd+"/images/azure_splash.bmp")
39+
40+
print('Loading Fonts...')
41+
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
42+
self.info_font = bitmap_font.load_font(info_font)
43+
self.info_font.load_glyphs(glyphs)
44+
45+
self.c_font = bitmap_font.load_font(temperature_font)
46+
self.c_font.load_glyphs(glyphs)
47+
self.c_font.load_glyphs(('°',)) # extra glyph for temperature font
48+
49+
print('setting up labels...')
50+
self.title_text = Label(self.info_font, text="Azure IoT Temperature Logger")
51+
self.title_text.x = 55
52+
self.title_text.y = 15
53+
self._text_group.append(self.title_text)
54+
55+
self.temp_text = Label(self.c_font, max_glyphs=8)
56+
self.temp_text.x = 25
57+
self.temp_text.y = 110
58+
self._text_group.append(self.temp_text)
59+
60+
self.azure_status_text = Label(self.info_font, max_glyphs=40)
61+
self.azure_status_text.x = 100
62+
self.azure_status_text.y = 220
63+
self._text_group.append(self.azure_status_text)
64+
65+
board.DISPLAY.show(self._text_group)
66+
67+
def display_azure_status(self, status_text):
68+
"""Displays the current Azure IoT status.
69+
:param str status_text: Description of Azure IoT status
70+
"""
71+
self.azure_status_text.text = status_text
72+
73+
def display_temp(self, adt_data):
74+
"""Displays the data from the ADT7410 on the.
75+
:param float adt_data: Value from the ADT7410
76+
"""
77+
if not self._celsius:
78+
adt_data = (adt_data * 9 / 5) + 32
79+
print('Temperature: %0.2f°F'%adt_data)
80+
if adt_data >= 212:
81+
self.temp_text.color = 0xFD2EE
82+
elif adt_data <= 32:
83+
self.temp_text.color = 0xFF0000
84+
self.temp_text.text = '%0.2f°F'%adt_data
85+
else:
86+
print('Temperature: %0.2f°C'%adt_data)
87+
if adt_data <= 0:
88+
self.temp_text.color = 0xFD2EE
89+
elif adt_data >= 100:
90+
self.temp_text.color = 0xFF0000
91+
self.temp_text.text = '%0.2f°C'%adt_data
92+
93+
def set_icon(self, filename):
94+
"""Sets the background image to a bitmap file.
95+
96+
:param filename: The filename of the chosen icon
97+
"""
98+
print("Set icon to ", filename)
99+
if self._icon_group:
100+
self._icon_group.pop()
101+
102+
if not filename:
103+
return # we're done, no icon desired
104+
if self._icon_file:
105+
self._icon_file.close()
106+
self._icon_file = open(filename, "rb")
107+
icon = displayio.OnDiskBitmap(self._icon_file)
108+
try:
109+
self._icon_sprite = displayio.TileGrid(icon,
110+
pixel_shader=displayio.ColorConverter())
111+
except TypeError:
112+
self._icon_sprite = displayio.TileGrid(icon,
113+
pixel_shader=displayio.ColorConverter(),
114+
position=(0,0))
115+
116+
self._icon_group.append(self._icon_sprite)
117+
board.DISPLAY.refresh_soon()
118+
board.DISPLAY.wait_for_frame()
119+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
PyPortal Azure IoT Plant Monitor
3+
===================================================
4+
Log plant vitals to Microsoft Azure IoT with
5+
your PyPortal
6+
7+
Author: Brent Rubell for Adafruit Industries, 2019
8+
"""
9+
import board
10+
import busio
11+
import time
12+
from digitalio import DigitalInOut
13+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
14+
import neopixel
15+
from adafruit_azureiot import IOT_Hub
16+
from adafruit_seesaw.seesaw import Seesaw
17+
18+
# gfx helper
19+
import azure_gfx_helper
20+
21+
# Get wifi details and more from a secrets.py file
22+
try:
23+
from secrets import secrets
24+
except ImportError:
25+
print("WiFi secrets are kept in secrets.py, please add them there!")
26+
raise
27+
28+
# PyPortal ESP32 Setup
29+
esp32_cs = DigitalInOut(board.ESP_CS)
30+
esp32_ready = DigitalInOut(board.ESP_BUSY)
31+
esp32_reset = DigitalInOut(board.ESP_RESET)
32+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
33+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
34+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
35+
"""Uncomment below for ItsyBitsy M4"""
36+
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
37+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
38+
39+
# Soil Sensor Setup
40+
i2c_bus = busio.I2C(board.SCL, board.SDA)
41+
ss = Seesaw(i2c_bus, addr=0x36)
42+
43+
# Create an instance of the Azure IoT Hub
44+
hub = IOT_Hub(wifi, secrets['azure_iot_hub'], secrets['azure_iot_sas'], secrets['azure_device_id'])
45+
46+
# init. graphics helper
47+
gfx = azure_gfx_helper.Azure_GFX(is_celsius=False)
48+
49+
while True:
50+
try:
51+
# read moisture level
52+
moisture_level = ss.moisture_read()
53+
# read temperature
54+
temperature = ss.get_temp()
55+
# Todo: make this nicer looking
56+
print(moisture_level, temperature)
57+
# display soil sensor values on pyportal
58+
gfx.display_moisture(moisture_level)
59+
gfx.display_temp(temperature)
60+
print('Sending data to Azure')
61+
gfx.display_azure_status('Sending data...')
62+
hub.send_device_message(temperature)
63+
hub.send_device_message(moisture_level)
64+
gfx.display_azure_status('Data sent!')
65+
print('Data sent!')
66+
except (ValueError, RuntimeError) as e:
67+
print("Failed to get data, retrying\n", e)
68+
wifi.reset()
69+
continue
70+
time.sleep(15)

0 commit comments

Comments
 (0)