Skip to content

Commit caffa6d

Browse files
committed
Added webhook plant moisture sensor code
1 parent 29a89eb commit caffa6d

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Webhook_Plant_Sensor/code.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# SPDX-FileCopyrightText: 2021 Dylan Herrada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
6+
import board
7+
from digitalio import DigitalInOut
8+
from adafruit_esp32spi import adafruit_esp32spi
9+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
10+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
11+
import neopixel
12+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
13+
from adafruit_io.adafruit_io import IO_MQTT
14+
from adafruit_seesaw.seesaw import Seesaw
15+
import busio
16+
17+
# Set up moisture sensor with seesaw
18+
i2c_bus = board.I2C()
19+
seesaw = Seesaw(i2c_bus, addr=0x36)
20+
21+
# Get wifi details and more from a secrets.py file
22+
try:
23+
from secrets import secrets
24+
except ImportError:
25+
print("WiFi secrets are kept in secrets.py, please add them there!")
26+
raise
27+
28+
# Set up WiFi
29+
esp32_cs = DigitalInOut(board.D13)
30+
esp32_ready = DigitalInOut(board.D11)
31+
esp32_reset = DigitalInOut(board.D12)
32+
33+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
34+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
36+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
37+
38+
# Define callback functions which will be called when certain events happen.
39+
# pylint: disable=unused-argument
40+
def connected(client):
41+
# This method is called when the client connects to Adafruit IO
42+
client.subscribe("plant")
43+
44+
45+
def subscribe(client, userdata, topic, granted_qos):
46+
# This method is called when the client subscribes to a new feed.
47+
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
48+
49+
50+
def message(client, feed_id, payload):
51+
# This method is called when a feed receives a new message
52+
print("Feed {0} received new value: {1}".format(feed_id, payload))
53+
54+
55+
# Connect to WiFi
56+
print("Connecting to WiFi...")
57+
wifi.connect()
58+
print("Connected!")
59+
60+
# Initialize MQTT interface with the esp interface
61+
MQTT.set_socket(socket, esp)
62+
63+
# Initialize a new MQTT Client object
64+
mqtt_client = MQTT.MQTT(
65+
broker="io.adafruit.com",
66+
username=secrets["aio_username"],
67+
password=secrets["aio_key"],
68+
)
69+
70+
71+
# Initialize an Adafruit IO MQTT Client
72+
io = IO_MQTT(mqtt_client)
73+
74+
# Connect the callback methods defined above to Adafruit IO
75+
io.on_connect = connected
76+
io.on_subscribe = subscribe
77+
io.on_message = message
78+
79+
# Connect to Adafruit IO
80+
print("Connecting to Adafruit IO...")
81+
io.connect()
82+
83+
START = 0
84+
LOW = False
85+
86+
MIN = 500
87+
88+
while True:
89+
# read moisture level through capacitive touch pad
90+
touch = seesaw.moisture_read()
91+
92+
# read temperature from the temperature sensor
93+
temp = seesaw.get_temp()
94+
95+
if touch < MIN:
96+
if not LOW:
97+
io.publish("plant", touch)
98+
print("published")
99+
LOW = True
100+
101+
elif touch >= MIN and time.time() - START > 10:
102+
io.publish("plant", touch)
103+
print("published to Adafruit IO")
104+
START = time.time()
105+
LOW = False
106+
107+
print("temp: " + str(temp) + " moisture: " + str(touch))
108+
time.sleep(1)

0 commit comments

Comments
 (0)