How To Monitor Your Houseplants With Raspberry Pi Pico W and Telegram

Houseplants need attention, but sometimes we forget to take care of them while sitting at our desks. Our patient is an IKEA succulent Gasteria ‘Gary’ who needs very little care. Which means I forgot to water him.so how do i Raspberry Pi Pico W Can you help me take care of Gary?
A soil moisture sensor measures the conductivity of the soil and sends that value as a voltage to the Raspberry Pi Pico W (or other microcontroller). This voltage is read by an analog GPIO pin and converted to a value between 0 and 65535. The lower the number, the drier the soil. This can be used as a trigger to send a message.
In this how-to, learn how to take a series of sensor readings, calculate their median value, and use it to determine if Gary needs a drink. Gary then sends a message via his Telegram bot.
What you need for this project
circuit construction
The circuit is very simple. A soil moisture sensor is connected to his 3 pins of her GPIO. The first two pins, 3V3 power and GND, provide the power required by the sensor.
The third pin, Signal, is an output pin that sends a value to the Raspberry Pi Pico. The output is a voltage that the corresponding GPIO reads as a value between 0 and 65535. This value is used in code to determine if the plant is thirsty.
Connect the soil moisture sensor as follows:
colour | soil moisture sensor | raspberry pi pico |
---|---|---|
red | + / VCC | 3V3 (physical pin 36) |
yellow | S/signal | GP26 |
black | -/GND | Any ground/GND pin |
Telegram bot setup
Telegram is an easy-to-use instant messaging service with a very simple means of communicating from your Raspberry Pi Pico W. With bots, you can create channels dedicated to houseplants and home security.
Creating a Telegram bot is a simple process, and fortunately The BotFather is here to help.
1. Sign in to your Telegram account. We used the Windows client for ease of workflow.
2. create a chat bot father. BotFather is a bot used to create and manage bots.
3. Create a new bot using the /newbot command and press Enter. BotFather creates wizards to guide you through the bot creation process.
/newbot
Four. Make a note of your API key, required for the project. Without this, your code cannot communicate with the bot.
Five. On Telegram, create a new chat with IDBot and request your ID. Make a note of the ID.
/getid
write code
All project code is written in MicroPython using the latest MicroPython release for Raspberry Pi Pico W. Use Thonny to code and test directly on the Raspberry Pi Pico W.
1. Do the following Download the latest version of MicroPython for Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and set up your Thonny. The rest are optional.
2. open tony and Click the “Stop” button Refresh the connection. This will ensure that the Python shell will open and work properly.
3. create a new file, Paste the contents of this link. Save the file as statistics.py in the root of the Raspberry Pi Pico W. This file is a module that contains all the statistical functions needed to select the median from a list of data. The project was created by Roberto Collistete Jr..
Four. create a new file and there Create four objects: SSID, PASSWORD, API, and ID.
Five. Assign the name of the Wi-Fi access point/router to the SSID object.
SSID = “YOUR WI-FI AP NAME HERE”
6. For Password, assign your Wi-Fi password.
PASSWORD = “YOUR SECRET PASSWORD”
7. For API, assign a Telegram Bot API key.. Make sure the key is within ” “.
API = "YOUR TELEGRAM BOT API KEY"
8. For ID, assign your Telegram user ID.. Make sure the ID is in ” ” as the value is set as a string.
ID = “YOUR USER ID HERE”
9. Save the file as Secrets.py on your Raspberry Pi Pico W.
SSID = “YOUR WI-FI SSID”
PASSWORD “YOUR WI-FI PASSWORD”
API = “YOUR TELEGRAM BOT API KEY”
ID = “YOUR TELEGRAM USER ID”
Ten. Create a new file and import a set of Python modules.
a. machine Contains the functions and classes necessary to use GPIOs (pins) and analog inputs (ADCs).
b. time Used to add delays to your code.
c. Communication network Make a Wi-Fi connection.
d. request (micro request) is the MicroPython version of the request used to send and receive data over the network.
e. statisticsa module containing functions to perform statistical analysis of data.
f. secreta module that contains all Wi-Fi details and API keys.
from machine import Pin, ADC
import time
import network
import urequests
import statistics
import secrets
11. Create an object and sensor, and create a connection to the soil moisture sensor on GPIO 26. This object sets the sensor input as analog with a value between 0 and 65535 representing the conductivity of the soil. The higher the number, the better the conductivity.
sensor = ADC(Pin(26))
12. Create an object wlan and use it to establish a connection to a Wi-Fi access point. The connection will activate and connect to the access point using the SSID and password stored in secrets.py. A 5 second pause stabilizes the connection and prints the current connection status. Displays TRUE if connected, FALSE if failed.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID, secrets.PASSWORD)
time.sleep(5)
print(wlan.isconnected())
13. Create an empty list called “readings”. Lists, also known as arrays, are used to store multiple values in an easy-to-read format.
readings = []
14. Create the first part of the Try,Except statement and set the code to run the while True loop. Try, Except are error handling statements. The code will try to execute whatever is contained within it, but if an exception or error occurs, the section of code that handles it will be used by default.
try:
while True:
15. Use a for loop to collect 5 readings from the soil moisture sensor and add each reading to the reading list at 1 second intervals. This section of code reads a GPIO pin connected to a sensor, gets its value and saves it in a list. Printing the measurements will help you debug the problem.
for i in range(5):
reading = sensor.read_u16()
readings.append(reading)
print(readings)
time.sleep(1)
16. Outside the for loop, create an object median_value. And store there the median (midpoint) value from the collection of measurements.
median_value = statistics.median(readings)
17. Use a conditional statement to check the median_value against a hardcoded value. For our tests, we chose 400 as the point at which the Gasteria plant needs water. Adjust this value to suit your plant’s needs.
if median_value < 400:
18. If the plant needs water, send a message to Telegram using urequests and print the message to the Python shell. Note that we use Secrets.API to insert the API key and Secrets.ID to send the user ID. Here is the actual message. sendMessage?text=Gary is thirsty.
urequests.get("https://api.telegram.org/bot"+secrets.API+"/sendMessage?text=Gary is thirsty&chat_id="+secrets.ID)
print("Message Sent")
19. With else the plant has enough water and decides it doesn’t need water at the moment. This will cause a 1 hour delay in your code.
else:
print("Gary has enough water")
time.sleep(3600)
20. Create an exception handler designed to handle OS errors (no Wi-Fi connection) that print a message to the Python shell. The print(“@”*68) line creates decorative borders above and below the message.
except OSError:
print("@"*68)
print("@ Cannot connect to the Wi-Fi, please check your SSID and PASSWORD @")
print("@"*68)
twenty one. Save the code on your Raspberry Pi Pico W as main.py. This will force the Pico W to load the code every time it is power cycled.
twenty two. Power cycle Pico W Then wait for the houseplant to request water.
full code list
from machine import Pin, ADC
import time
import network
import urequests
import statistics
import secrets
sensor = ADC(Pin(26))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(secrets.SSID, secrets.PASSWORD)
time.sleep(5)
print(wlan.isconnected())
readings = []
try:
while True:
for i in range(5):
reading = sensor.read_u16()
readings.append(reading)
print(readings)
time.sleep(1)
median_value = statistics.median(readings)
if median_value < 400:
urequests.get("https://api.telegram.org/bot"+secrets.API+"/sendMessage?text=Gary is thirsty&chat_id="+secrets.ID)
print("Message Sent")
else:
print("Gary has enough water")
time.sleep(3600)
except OSError:
print("@"*68)
print("@ Cannot connect to the Wi-Fi, please check your SSID and PASSWORD @")
print("@"*68)