|
8 | 8 | """ |
9 | 9 | import time |
10 | 10 | import json |
11 | | -import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
12 | 11 | import board |
13 | 12 | import busio |
14 | | -import digitalio |
15 | 13 | import gcp_gfx_helper |
16 | 14 | import neopixel |
17 | 15 | from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
| 16 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
18 | 17 | from adafruit_gc_iot_core import MQTT_API, Cloud_Core |
19 | 18 | from adafruit_minimqtt import MQTT |
20 | 19 | from adafruit_seesaw.seesaw import Seesaw |
21 | | -from digitalio import DigitalInOut |
| 20 | +import digitalio |
22 | 21 |
|
23 | 22 | # Delay before reading the sensors, in minutes |
24 | | -# TODO: switch to 10min |
25 | | -SENSOR_DELAY = 1 |
| 23 | +SENSOR_DELAY = 10 |
26 | 24 |
|
27 | 25 | # Get wifi details and more from a secrets.py file |
28 | 26 | try: |
|
32 | 30 | raise |
33 | 31 |
|
34 | 32 | # PyPortal ESP32 Setup |
35 | | -esp32_cs = DigitalInOut(board.ESP_CS) |
36 | | -esp32_ready = DigitalInOut(board.ESP_BUSY) |
37 | | -esp32_reset = DigitalInOut(board.ESP_RESET) |
| 33 | +esp32_cs = digitalio.DigitalInOut(board.ESP_CS) |
| 34 | +esp32_ready = digitalio.DigitalInOut(board.ESP_BUSY) |
| 35 | +esp32_reset = digitalio.DigitalInOut(board.ESP_RESET) |
38 | 36 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
39 | 37 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
40 | 38 | status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
41 | 39 | wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( |
42 | 40 | esp, secrets, status_light) |
43 | 41 |
|
| 42 | +# Connect to WiFi |
| 43 | +print("Connecting to WiFi...") |
| 44 | +wifi.connect() |
| 45 | +print("Connected!") |
| 46 | + |
44 | 47 | # Soil Sensor Setup |
45 | 48 | i2c_bus = busio.I2C(board.SCL, board.SDA) |
46 | 49 | ss = Seesaw(i2c_bus, addr=0x36) |
|
54 | 57 | gfx = gcp_gfx_helper.Google_GFX() |
55 | 58 | print("Graphics loaded!") |
56 | 59 |
|
57 | | -# Connect to WiFi |
58 | | -print("Connecting to WiFi...") |
59 | | -wifi.connect() |
60 | | -print("Connected!") |
61 | 60 |
|
62 | 61 | # Define callback methods which are called when events occur |
63 | 62 | # pylint: disable=unused-argument, redefined-outer-name |
64 | | - |
65 | | - |
66 | 63 | def connect(client, userdata, flags, rc): |
67 | 64 | # This function will be called when the client is connected |
68 | 65 | # successfully to the broker. |
@@ -96,12 +93,12 @@ def publish(client, userdata, topic, pid): |
96 | 93 | def message(client, topic, msg): |
97 | 94 | # This method is called when the client receives data from a topic. |
98 | 95 | try: |
99 | | - # Attempt to load a JSON command |
| 96 | + # Attempt to decode a JSON command |
100 | 97 | msg_dict = json.loads(msg) |
101 | 98 | # Handle water-pump commands |
102 | 99 | if msg_dict['pump_time']: |
103 | 100 | handle_pump(msg_dict) |
104 | | - except: |
| 101 | + except TypeError: |
105 | 102 | # Non-JSON command, print normally |
106 | 103 | print("Message from {}: {}".format(topic, msg)) |
107 | 104 |
|
@@ -135,16 +132,16 @@ def handle_pump(command): |
135 | 132 | # Initialize Google Cloud IoT Core interface |
136 | 133 | google_iot = Cloud_Core(esp, secrets) |
137 | 134 |
|
138 | | -# Optional JSON-Web-Token (JWT) Generation |
139 | | -# print("Generating JWT...") |
140 | | -# jwt = google_iot.generate_jwt() |
141 | | -# print("Your JWT is: ", jwt) |
| 135 | +# JSON-Web-Token (JWT) Generation |
| 136 | +print("Generating JWT...") |
| 137 | +jwt = google_iot.generate_jwt() |
| 138 | +print("Your JWT is: ", jwt) |
142 | 139 |
|
143 | 140 | # Set up a new MiniMQTT Client |
144 | 141 | client = MQTT(socket, |
145 | 142 | broker=google_iot.broker, |
146 | 143 | username=google_iot.username, |
147 | | - password=secrets['jwt'], |
| 144 | + password=jwt, |
148 | 145 | client_id=google_iot.cid, |
149 | 146 | network_manager=wifi) |
150 | 147 |
|
|
0 commit comments