-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathcode.py
executable file
·108 lines (91 loc) · 3.22 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
# SPDX-FileCopyrightText: 2020 Jim Bennett for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
PyPortal Azure IoT Plant Monitor
====================================================
Log plant vitals to Microsoft Azure IoT Central with
your PyPortal
Authors: Brent Rubell for Adafruit Industries, 2019
: Jim Bennett for Microsoft, 2020
"""
import time
import json
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import neopixel
import rtc
from adafruit_azureiot import IoTCentralDevice
from adafruit_seesaw.seesaw import Seesaw
# gfx helper
import azure_gfx_helper
# init. graphics helper
gfx = azure_gfx_helper.Azure_GFX(is_celsius=True)
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# PyPortal ESP32 Setup
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
# Set up the WiFi manager with a status light to show the WiFi connection status
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
print("WiFi connecting...")
wifi.connect()
print("WiFi connected!")
# Time setup, needed to authenticate with Azure IoT Central
# get_time will raise ValueError if the time isn't available yet so loop until
# it works.
now_utc = None
while now_utc is None:
try:
now_utc = time.localtime(esp.get_time()[0])
except ValueError:
pass
rtc.RTC().datetime = now_utc
# Soil Sensor Setup
i2c_bus = busio.I2C(board.SCL, board.SDA)
ss = Seesaw(i2c_bus, addr=0x36)
# Create an instance of the Azure IoT Central device
device = IoTCentralDevice(
socket, esp, secrets["id_scope"], secrets["device_id"], secrets["key"]
)
# Connect to Azure IoT Central
device.connect()
# Hide the splash screen and show the telemetry values
gfx.show_text()
while True:
try:
# read moisture level
moisture_level = ss.moisture_read()
# read temperature
temperature = ss.get_temp()
# display soil sensor values on pyportal
gfx.display_moisture(moisture_level)
gfx.display_temp(temperature)
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}
device.send_telemetry(json.dumps(message))
device.loop()
gfx.display_azure_status("Data sent!")
print("Data sent!")
except (ValueError, RuntimeError, ConnectionError, OSError) as e:
print("Failed to get data, retrying\n", e)
wifi.reset()
wifi.connect()
device.reconnect()
continue
# Sleep for 10 minutes before getting the next value
time.sleep(600)