Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions PyPortal_Azure_Plant_Monitor/azure_gfx_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,48 @@
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font

cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]

# Fonts within /fonts folder
main_font = cwd+"/fonts/EarthHeart-26.bdf"
data_font = cwd+"/fonts/Collegiate-50.bdf"
main_font = cwd + "/fonts/EarthHeart-26.bdf"
data_font = cwd + "/fonts/Collegiate-50.bdf"


class Azure_GFX(displayio.Group):
def __init__(self, is_celsius):
"""Creates an Azure_GFX object.
:param bool is_celsius: Temperature displayed in Celsius.
"""
# root displayio group
root_group = displayio.Group(max_size=23)
root_group = displayio.Group()
board.DISPLAY.show(root_group)
super().__init__(max_size=15)
super().__init__()

# temperature display option
self._is_celsius = is_celsius

# create background icon group
self._icon_group = displayio.Group(max_size=3)
self._icon_group = displayio.Group()
board.DISPLAY.show(self._icon_group)
# create text object group
self._text_group = displayio.Group(max_size=9)
self._text_group = displayio.Group()

self._icon_sprite = None
self._icon_file = None
self._cwd = cwd
self.set_icon(self._cwd+"/images/azure_splash.bmp")
self.set_icon(self._cwd + "/images/azure_splash.bmp")

print('loading fonts...')
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: '
data_glyphs = b'012345678-,.:/FC'
print("loading fonts...")
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.: "
data_glyphs = b"012345678-,.:/FC"
self.main_font = bitmap_font.load_font(main_font)
self.main_font.load_glyphs(glyphs)
self.data_font = bitmap_font.load_font(data_font)
self.data_font.load_glyphs(data_glyphs)
self.data_font.load_glyphs(('°',)) # extra glyph for temperature font
self.data_font.load_glyphs(("°",)) # extra glyph for temperature font

print('setting up labels...')
print("setting up labels...")
self.title_text = Label(self.main_font, text="Azure Plant Monitor")
self.title_text.x = 35
self.title_text.y = 25
Expand All @@ -56,7 +58,7 @@ def __init__(self, is_celsius):
self.temp_label.y = 65
self._text_group.append(self.temp_label)

self.temp_text = Label(self.data_font, max_glyphs=10)
self.temp_text = Label(self.data_font)
self.temp_text.x = 200
self.temp_text.y = 85
self._text_group.append(self.temp_text)
Expand All @@ -66,12 +68,12 @@ def __init__(self, is_celsius):
self.moisture_label.y = 135
self._text_group.append(self.moisture_label)

self.moisture_text = Label(self.data_font, max_glyphs=10)
self.moisture_text = Label(self.data_font)
self.moisture_text.x = 200
self.moisture_text.y = 175
self._text_group.append(self.moisture_text)

self.azure_status_text = Label(self.main_font, max_glyphs=15)
self.azure_status_text = Label(self.main_font)
self.azure_status_text.x = 65
self.azure_status_text.y = 225
self._text_group.append(self.azure_status_text)
Expand All @@ -89,7 +91,7 @@ def display_moisture(self, moisture_data):
"""Displays the moisture from the Stemma Soil Sensor.
:param int moisture_data: Moisture value
"""
print('Moisture Level: ', moisture_data)
print("Moisture Level: ", moisture_data)
self.moisture_text.text = str(moisture_data)

def display_temp(self, temp_data):
Expand All @@ -98,22 +100,22 @@ def display_temp(self, temp_data):
"""
if not self._is_celsius:
temp_data = (temp_data * 9 / 5) + 32 - 15
print('Temperature: %0.0f°F'%temp_data)
print("Temperature: %0.0f°F" % temp_data)
if temp_data >= 212:
self.temp_text.color = 0xFD2EE
elif temp_data <= 32:
self.temp_text.color = 0xFF0000
self.temp_text.text = '%0.0f°F'%temp_data
temp_data = '%0.0f'%temp_data
self.temp_text.text = "%0.0f°F" % temp_data
temp_data = "%0.0f" % temp_data
return int(temp_data)
else:
print('Temperature: %0.0f°C'%temp_data)
print("Temperature: %0.0f°C" % temp_data)
if temp_data <= 0:
self.temp_text.color = 0xFD2EE
elif temp_data >= 100:
self.temp_text.color = 0xFF0000
self.temp_text.text = '%0.0f°C'%temp_data
temp_data = '%0.0f'%temp_data
self.temp_text.text = "%0.0f°C" % temp_data
temp_data = "%0.0f" % temp_data
return int(temp_data)

def set_icon(self, filename):
Expand All @@ -127,16 +129,19 @@ def set_icon(self, filename):

if not filename:
return # we're done, no icon desired

# CircuitPython 6 & 7 compatible
if self._icon_file:
self._icon_file.close()
self._icon_file = open(filename, "rb")
icon = displayio.OnDiskBitmap(self._icon_file)
try:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
except TypeError:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()),
position=(0,0))
self._icon_sprite = displayio.TileGrid(
icon, pixel_shader=getattr(icon, "pixel_shader", displayio.ColorConverter())
)

# CircuitPython 7 compatible
# # Remove self._icon_file - it is no longer used
# icon = displayio.OnDiskBitmap(filename)
# self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)

self._icon_group.append(self._icon_sprite)
19 changes: 6 additions & 13 deletions PyPortal_Azure_Plant_Monitor/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@

# Create an instance of the Azure IoT Central device
device = IoTCentralDevice(
socket,
esp,
secrets["id_scope"],
secrets["device_id"],
secrets["key"]
socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]
)

# Connect to Azure IoT Central
Expand All @@ -82,19 +78,16 @@
gfx.display_moisture(moisture_level)
gfx.display_temp(temperature)

print('Sending data to Azure')
gfx.display_azure_status('Sending data...')
print("Sending data to Azure")
gfx.display_azure_status("Sending data...")

# send the temperature and moisture level to Azure
message = {
"Temperature": temperature,
"MoistureLevel": moisture_level
}
message = {"Temperature": temperature, "MoistureLevel": moisture_level}
device.send_telemetry(json.dumps(message))
device.loop()

gfx.display_azure_status('Data sent!')
print('Data sent!')
gfx.display_azure_status("Data sent!")
print("Data sent!")
except (ValueError, RuntimeError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
Expand Down
55 changes: 30 additions & 25 deletions azure_gfx_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,60 @@
from adafruit_display_text.label import Label
from adafruit_bitmap_font import bitmap_font

cwd = ("/"+__file__).rsplit('/', 1)[0] # the current working directory (where this file is)
# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]

# Fonts within /fonts folder
info_font = cwd+"/fonts/Nunito-Black-17.bdf"
temperature_font = cwd+"/fonts/Nunito-Light-75.bdf"
info_font = cwd + "/fonts/Nunito-Black-17.bdf"
temperature_font = cwd + "/fonts/Nunito-Light-75.bdf"


class Azure_GFX(displayio.Group):
def __init__(self, celsius=False):
"""Creates an Azure_GFX object.
:param bool celsius: Temperature displayed as F or C
"""
# root displayio group
root_group = displayio.Group(max_size=20)
root_group = displayio.Group()
board.DISPLAY.show(root_group)
super().__init__(max_size=20)
super().__init__()

self._celsius = celsius

# create background icon group
self._icon_group = displayio.Group(max_size=1)
self._icon_group = displayio.Group()
self.append(self._icon_group)
board.DISPLAY.show(self._icon_group)
# create text object group
self._text_group = displayio.Group(max_size=6)
self._text_group = displayio.Group()
self.append(self._text_group)

self._icon_sprite = None
self._icon_file = None
self._cwd = cwd
self.set_icon(self._cwd+"/images/azure_splash.bmp")
self.set_icon(self._cwd + "/images/azure_splash.bmp")

print('Loading Fonts...')
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ '
print("Loading Fonts...")
glyphs = b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:/ "
self.info_font = bitmap_font.load_font(info_font)
self.info_font.load_glyphs(glyphs)

self.c_font = bitmap_font.load_font(temperature_font)
self.c_font.load_glyphs(glyphs)
self.c_font.load_glyphs(('°',)) # extra glyph for temperature font
self.c_font.load_glyphs(("°",)) # extra glyph for temperature font

print('setting up labels...')
print("setting up labels...")
self.title_text = Label(self.info_font, text="Azure IoT Temperature Logger")
self.title_text.x = 55
self.title_text.y = 15
self._text_group.append(self.title_text)

self.temp_text = Label(self.c_font, max_glyphs=8)
self.temp_text = Label(self.c_font)
self.temp_text.x = 25
self.temp_text.y = 110
self._text_group.append(self.temp_text)

self.azure_status_text = Label(self.info_font, max_glyphs=40)
self.azure_status_text = Label(self.info_font)
self.azure_status_text.x = 100
self.azure_status_text.y = 220
self._text_group.append(self.azure_status_text)
Expand All @@ -76,19 +78,19 @@ def display_temp(self, adt_data):
"""
if not self._celsius:
adt_data = (adt_data * 9 / 5) + 32
print('Temperature: %0.2f°F'%adt_data)
print("Temperature: %0.2f°F" % adt_data)
if adt_data >= 212:
self.temp_text.color = 0xFD2EE
elif adt_data <= 32:
self.temp_text.color = 0xFF0000
self.temp_text.text = '%0.2f°F'%adt_data
self.temp_text.text = "%0.2f°F" % adt_data
else:
print('Temperature: %0.2f°C'%adt_data)
print("Temperature: %0.2f°C" % adt_data)
if adt_data <= 0:
self.temp_text.color = 0xFD2EE
elif adt_data >= 100:
self.temp_text.color = 0xFF0000
self.temp_text.text = '%0.2f°C'%adt_data
self.temp_text.text = "%0.2f°C" % adt_data

def set_icon(self, filename):
"""Sets the background image to a bitmap file.
Expand All @@ -101,17 +103,20 @@ def set_icon(self, filename):

if not filename:
return # we're done, no icon desired

# CircuitPython 6 & 7 compatible
if self._icon_file:
self._icon_file.close()
self._icon_file = open(filename, "rb")
icon = displayio.OnDiskBitmap(self._icon_file)
try:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()))
except TypeError:
self._icon_sprite = displayio.TileGrid(icon,
pixel_shader=getattr(icon, 'pixel_shader', displayio.ColorConverter()),
position=(0,0))
self._icon_sprite = displayio.TileGrid(
icon, pixel_shader=getattr(icon, "pixel_shader", displayio.ColorConverter())
)

# CircuitPython 7 compatible
# # Remove self._icon_file - it is no longer used
# icon = displayio.OnDiskBitmap(filename)
# self._icon_sprite = displayio.TileGrid(icon, pixel_shader=icon.pixel_shader)

self._icon_group.append(self._icon_sprite)
try:
Expand Down