66
77Author: Brent Rubell for Adafruit Industries, 2019
88"""
9+ import time
910import json
1011import board
1112import busio
1718from adafruit_minimqtt import MQTT
1819from adafruit_aws_iot import MQTT_CLIENT
1920from 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
2327try :
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
3034try :
31- from certificates import DEVICE_CERT , DEVICE_KEY
35+ with open ("aws_cert.pem.crt" , "rb" ) as f :
36+ DEVICE_CERT = f .read ()
3237except 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+
4159spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
4260esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
4361status_light = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 )
6078ss = 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 ()
6583print ("Graphics loaded!" )
6684
6785
@@ -98,14 +116,14 @@ def publish(client, userdata, topic, pid):
98116
99117def 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
104123client = 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
111129aws_iot = MQTT_CLIENT (client )
@@ -126,25 +144,27 @@ def message(client, topic, msg):
126144
127145while 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