|
| 1 | +""" |
| 2 | +PyPortal Amazon AWS IoT Plant Monitor |
| 3 | +========================================================= |
| 4 | +Log your plant's vitals to AWS IoT and receive email |
| 5 | +notifications when it needs watering with your PyPortal. |
| 6 | +
|
| 7 | +Author: Brent Rubell for Adafruit Industries, 2019 |
| 8 | +""" |
| 9 | +import time |
| 10 | +import json |
| 11 | +import board |
| 12 | +import busio |
| 13 | +from digitalio import DigitalInOut |
| 14 | +import neopixel |
| 15 | +from adafruit_esp32spi import adafruit_esp32spi |
| 16 | +from adafruit_esp32spi import adafruit_esp32spi_wifimanager |
| 17 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 18 | +from adafruit_minimqtt import MQTT |
| 19 | +from adafruit_aws_iot import MQTT_CLIENT |
| 20 | +from adafruit_seesaw.seesaw import Seesaw |
| 21 | +import aws_gfx_helper |
| 22 | + |
| 23 | +# Time between polling the STEMMA, in minutes |
| 24 | +SENSOR_DELAY = 15 |
| 25 | + |
| 26 | +# Get wifi details and more from a secrets.py file |
| 27 | +try: |
| 28 | + from secrets import secrets |
| 29 | +except ImportError: |
| 30 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 31 | + raise |
| 32 | + |
| 33 | +# Get device certificate |
| 34 | +try: |
| 35 | + with open("aws_cert.pem.crt", "rb") as f: |
| 36 | + DEVICE_CERT = f.read() |
| 37 | +except ImportError: |
| 38 | + print("Certificate (aws_cert.pem.crt) not found on CIRCUITPY filesystem.") |
| 39 | + raise |
| 40 | + |
| 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 | + |
| 59 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 60 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 61 | +status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
| 62 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( |
| 63 | + esp, secrets, status_light) |
| 64 | + |
| 65 | +# Initialize the graphics helper |
| 66 | +print("Loading AWS IoT Graphics...") |
| 67 | +gfx = aws_gfx_helper.AWS_GFX() |
| 68 | +print("Graphics loaded!") |
| 69 | + |
| 70 | +# Set AWS Device Certificate |
| 71 | +esp.set_certificate(DEVICE_CERT) |
| 72 | + |
| 73 | +# Set AWS RSA Private Key |
| 74 | +esp.set_private_key(DEVICE_KEY) |
| 75 | + |
| 76 | +# Connect to WiFi |
| 77 | +print("Connecting to WiFi...") |
| 78 | +wifi.connect() |
| 79 | +print("Connected!") |
| 80 | + |
| 81 | +# Soil Sensor Setup |
| 82 | +i2c_bus = busio.I2C(board.SCL, board.SDA) |
| 83 | +ss = Seesaw(i2c_bus, addr=0x36) |
| 84 | + |
| 85 | +# Define callback methods which are called when events occur |
| 86 | +# pylint: disable=unused-argument, redefined-outer-name |
| 87 | +def connect(client, userdata, flags, rc): |
| 88 | + # This function will be called when the client is connected |
| 89 | + # successfully to the broker. |
| 90 | + print('Connected to AWS IoT!') |
| 91 | + print('Flags: {0}\nRC: {1}'.format(flags, rc)) |
| 92 | + |
| 93 | + # Subscribe client to all shadow updates |
| 94 | + print("Subscribing to shadow updates...") |
| 95 | + aws_iot.shadow_subscribe() |
| 96 | + |
| 97 | +def disconnect(client, userdata, rc): |
| 98 | + # This method is called when the client disconnects |
| 99 | + # from the broker. |
| 100 | + print('Disconnected from AWS IoT!') |
| 101 | + |
| 102 | +def subscribe(client, userdata, topic, granted_qos): |
| 103 | + # This method is called when the client subscribes to a new topic. |
| 104 | + print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos)) |
| 105 | + |
| 106 | +def unsubscribe(client, userdata, topic, pid): |
| 107 | + # This method is called when the client unsubscribes from a topic. |
| 108 | + print('Unsubscribed from {0} with PID {1}'.format(topic, pid)) |
| 109 | + |
| 110 | +def publish(client, userdata, topic, pid): |
| 111 | + # This method is called when the client publishes data to a topic. |
| 112 | + print('Published to {0} with PID {1}'.format(topic, pid)) |
| 113 | + |
| 114 | +def message(client, topic, msg): |
| 115 | + # This method is called when the client receives data from a topic. |
| 116 | + print("Message from {}: {}".format(topic, msg)) |
| 117 | + |
| 118 | +# Set up a new MiniMQTT Client |
| 119 | +client = MQTT(socket, |
| 120 | + broker = secrets['broker'], |
| 121 | + client_id = secrets['client_id'], |
| 122 | + network_manager = wifi) |
| 123 | + |
| 124 | +# Initialize AWS IoT MQTT API Client |
| 125 | +aws_iot = MQTT_CLIENT(client) |
| 126 | + |
| 127 | +# Connect callback handlers to AWS IoT MQTT Client |
| 128 | +aws_iot.on_connect = connect |
| 129 | +aws_iot.on_disconnect = disconnect |
| 130 | +aws_iot.on_subscribe = subscribe |
| 131 | +aws_iot.on_unsubscribe = unsubscribe |
| 132 | +aws_iot.on_publish = publish |
| 133 | +aws_iot.on_message = message |
| 134 | + |
| 135 | +print('Attempting to connect to %s'%client.broker) |
| 136 | +aws_iot.connect() |
| 137 | + |
| 138 | +# Time in seconds since power on |
| 139 | +initial = time.monotonic() |
| 140 | + |
| 141 | +while True: |
| 142 | + try: |
| 143 | + gfx.show_aws_status('Listening for msgs...') |
| 144 | + now = time.monotonic() |
| 145 | + if now - initial > (SENSOR_DELAY * 60): |
| 146 | + # read moisture level |
| 147 | + moisture = ss.moisture_read() |
| 148 | + print("Moisture Level: ", moisture) |
| 149 | + # read temperature |
| 150 | + temperature = ss.get_temp() |
| 151 | + # Display Soil Sensor values on pyportal |
| 152 | + temperature = gfx.show_temp(temperature) |
| 153 | + gfx.show_water_level(moisture) |
| 154 | + print('Sending data to AWS IoT...') |
| 155 | + gfx.show_aws_status('Publishing data...') |
| 156 | + # Create a json-formatted device payload |
| 157 | + payload = {"state":{"reported":{"moisture":str(moisture), |
| 158 | + "temp":str(temperature)}}} |
| 159 | + # Update device shadow |
| 160 | + aws_iot.shadow_update(json.dumps(payload)) |
| 161 | + gfx.show_aws_status('Data published!') |
| 162 | + print('Data sent!') |
| 163 | + # Reset timer |
| 164 | + initial = now |
| 165 | + aws_iot.loop() |
| 166 | + except (ValueError, RuntimeError) as e: |
| 167 | + print("Failed to get data, retrying", e) |
| 168 | + wifi.reset() |
0 commit comments