Skip to content

Commit 9807caf

Browse files
brentrubrentru
authored andcommitted
replace cert.py with cert loading, gcp references with aws, fix crashes
1 parent 98bdbe0 commit 9807caf

3 files changed

Lines changed: 52 additions & 33 deletions

File tree

PyPortal_AWS_IOT_Planter/aws_gfx_helper.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
GFX Helper for PyPortal GCP IoT Plant Monitor
2+
GFX Helper for PyPortal AWS IoT Plant Monitor
33
"""
44
import board
55
import displayio
@@ -12,10 +12,10 @@
1212
# GFX Font
1313
font = terminalio.FONT
1414

15-
class Google_GFX(displayio.Group):
15+
class AWS_GFX(displayio.Group):
1616
def __init__(self, is_celsius=False):
17-
"""Creates an Google_GFX object for displaying plant
18-
and Google Cloud IoT Core status.
17+
"""Creates an AWS_GFX object for displaying plant
18+
and AWS IoT status.
1919
:param bool is_celsius: Temperature displayed in Celsius.
2020
"""
2121
# root displayio group
@@ -39,18 +39,18 @@ def __init__(self, is_celsius=False):
3939
self._icon_sprite = None
4040
self._icon_file = None
4141
self._cwd = cwd
42-
self.set_icon(self._cwd+"/images/gcp_splash.bmp")
42+
self.set_icon(self._cwd+"/images/aws_splash.bmp")
4343

4444
print('Setting up labels...')
4545
header_group = displayio.Group(scale=3)
46-
header_label = Label(font, text="Google Cloud IoT\n Planter")
46+
header_label = Label(font, text="AWS IoT\n Planter")
4747
header_label.x = (self.display.width // 2) // 22
4848
header_label.y = 15
4949
header_group.append(header_label)
5050
self._text_group.append(header_group)
5151

5252
# Temperature Display
53-
temp_group = displayio.Group(scale=2, max_size=3)
53+
temp_group = displayio.Group(scale=2, max_size=400)
5454
temp_label = Label(font, text="Temperature: ")
5555
temp_label.x = (self.display.width//2) // 11
5656
temp_label.y = 55
@@ -75,27 +75,26 @@ def __init__(self, is_celsius=False):
7575
temp_group.append(self.water_lvl_label)
7676
self._text_group.append(water_group)
7777

78-
# GCP Status
78+
# AWS Status
7979
status_group = displayio.Group()
80-
self.gcp_status_label = Label(font, text="Connecting to GCP IoT Core...")
81-
self.gcp_status_label.x = self.display.width//4
82-
self.gcp_status_label.y = 200
83-
status_group.append(self.gcp_status_label)
80+
self.aws_status_label = Label(font, text="Connecting to AWS IoT...")
81+
self.aws_status_label.x = self.display.width//4
82+
self.aws_status_label.y = 200
83+
status_group.append(self.aws_status_label)
8484
self._text_group.append(status_group)
8585

8686
self.display.show(self._text_group)
8787

88-
def show_gcp_status(self, status_text):
88+
def show_aws_status(self, status_text):
8989
"""Displays the system status on the PyPortal
90-
:param str status_text: Description of current GCP IoT status
90+
:param str status_text: Description of current AWS IoT status.
9191
"""
92-
self.gcp_status_label.text = status_text
92+
self.aws_status_label.text = status_text
9393

9494
def show_water_level(self, water_data):
9595
"""Displays the water level from the Stemma Soil Sensor.
9696
:param int water_data: water value
9797
"""
98-
print('Water Level: ', water_data)
9998
self.water_lvl_label.text = str(water_data)
10099

101100
def show_temp(self, temp_data):
225 KB
Binary file not shown.

PyPortal_AWS_IOT_Planter/code.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
Author: Brent Rubell for Adafruit Industries, 2019
88
"""
9+
import time
910
import json
1011
import board
1112
import busio
@@ -17,7 +18,10 @@
1718
from adafruit_minimqtt import MQTT
1819
from adafruit_aws_iot import MQTT_CLIENT
1920
from adafruit_seesaw.seesaw import Seesaw
21+
import aws_gfx_helper
2022

23+
# Time between polling the STEMMA, in minutes
24+
SENSOR_DELAY = 15
2125

2226
# Get wifi details and more from a secrets.py file
2327
try:
@@ -26,18 +30,32 @@
2630
print("WiFi secrets are kept in secrets.py, please add them there!")
2731
raise
2832

29-
# Get device certificate and private key from a certificates.py file
33+
# Get device certificate
3034
try:
31-
from certificates import DEVICE_CERT, DEVICE_KEY
35+
with open("aws_cert.pem.crt", "rb") as f:
36+
DEVICE_CERT = f.read()
3237
except ImportError:
33-
print("Certificate and private key data is kept in certificates.py, \
34-
please add them there!")
38+
print("Certificate (aws_cert.pem.crt) not found on CIRCUITPY filesystem.")
3539
raise
3640

37-
# PyPortal ESP32 Setup
38-
esp32_cs = digitalio.DigitalInOut(board.ESP_CS)
39-
esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY)
40-
esp32_reset = digitalio.DigitalInOut(board.ESP_RESET)
41+
# Get device private key
42+
try:
43+
with open("private.pem.key", "rb") as f:
44+
DEVICE_KEY = f.read()
45+
except ImportError:
46+
print("Key (private.pem.key) not found on CIRCUITPY filesystem.")
47+
raise
48+
49+
# If you are using a board with pre-defined ESP32 Pins:
50+
esp32_cs = DigitalInOut(board.ESP_CS)
51+
esp32_ready = DigitalInOut(board.ESP_BUSY)
52+
esp32_reset = DigitalInOut(board.ESP_RESET)
53+
54+
# If you have an externally connected ESP32:
55+
# esp32_cs = DigitalInOut(board.D9)
56+
# esp32_ready = DigitalInOut(board.D10)
57+
# esp32_reset = DigitalInOut(board.D5)
58+
4159
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4260
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
4361
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
@@ -60,8 +78,8 @@
6078
ss = Seesaw(i2c_bus, addr=0x36)
6179

6280
# Initialize the graphics helper
63-
print("Loading GCP Graphics...")
64-
gfx = gcp_gfx_helper.Google_GFX()
81+
print("Loading AWS IoT Graphics...")
82+
gfx = aws_gfx_helper.AWS_GFX()
6583
print("Graphics loaded!")
6684

6785

@@ -98,14 +116,14 @@ def publish(client, userdata, topic, pid):
98116

99117
def message(client, topic, msg):
100118
# This method is called when the client receives data from a topic.
101-
# TODO
119+
print("Message from {}: {}".format(topic, msg))
120+
102121

103122
# Set up a new MiniMQTT Client
104123
client = MQTT(socket,
105124
broker = secrets['broker'],
106125
client_id = secrets['client_id'],
107-
network_manager = wifi,
108-
log=True)
126+
network_manager = wifi)
109127

110128
# Initialize AWS IoT MQTT API Client
111129
aws_iot = MQTT_CLIENT(client)
@@ -126,25 +144,27 @@ def message(client, topic, msg):
126144

127145
while True:
128146
try:
129-
gfx.show_gcp_status('Listening for new messages...')
147+
gfx.show_aws_status('Listening for msgs...')
130148
now = time.monotonic()
131-
if now - initial > (SENSOR_DELAY * 60):
149+
if now - initial > (1 * 60):
132150
# read moisture level
133151
moisture = ss.moisture_read()
152+
print("Moisture Level: ", moisture)
134153
# read temperature
135154
temperature = ss.get_temp()
155+
print("Temperature:{}F".format(temperature))
136156
# Display Soil Sensor values on pyportal
137157
temperature = gfx.show_temp(temperature)
138158
gfx.show_water_level(moisture)
139159
print('Sending data to AWS IoT...')
140-
gfx.show_gcp_status('Publishing data...')
160+
gfx.show_aws_status('Publishing data...')
141161
# Create a json-formatted device payload
142162
payload = {"state":{"reported":
143163
{"moisture":str(moisture),
144164
"temp":str(temperature)}}}
145165
# Update device shadow
146166
aws_iot.shadow_update(json.dumps(payload))
147-
gfx.show_gcp_status('Data published!')
167+
gfx.show_aws_status('Data published!')
148168
print('Data sent!')
149169
# Reset timer
150170
initial = now

0 commit comments

Comments
 (0)