Build a Raspberry Pi Moisture Sensor to Monitor Your Plants
In this tutorial, I’m going to harness the awesomeness of Raspberry Pi to build a moisture sensor for a plant pot. You will be able to monitor the sensor locally on the LCD or remotely, via ControlMyPi.com, and receive daily emails if the moisture drops below a specified level.
Along the way I will:
- wire up and read a value from an analog sensor over SPI using a breadboard
- format the sensor reading nicely in the console
- display the sensor reading on an RGB LCD display
- have the Raspberry Pi send an email with the sensor reading
- easily monitor the sensor and some historic readings on the web



Hardware Supplies
In order to get the most out of this tutorial, it is recommended that you acquire the following:
- Raspberry Pi model B ($40)
- Wired or wireless Internet connection
- Half-size breadboard ($5)
- Six female-to-male jumper wires
- Male-to-male jumper wires (various lengths)
- MCP3008 ($3.75) - 8-Channel 10-Bit A/D Converter with SPI Serial Interface
- Octopus Soil Moisture Sensor Brick ($4.50)
- Adafruit RGB Negative 16x2 LCD+Keypad Kit for Raspberry Pi ($25) assembled with the Stacking Header for Raspberry Pi - 2x13 Extra Tall ($2)



Some Alternative Components
- Instead of the soil moisture sensor, you can use any type of varying voltage analog sensor or even just a variable resistor for testing.
- You can use a smaller MCP3004 (4-Channel ADC SPI) if you wish, but the wiring will be different.
- You can skip the RGB LCD or replace it with an alternative screen. You’ll have to remove or change a few lines in the final script if you do.
The Cost
After adding on a few dollars for the jumper wires the total project cost works out roughly to $55 without the LCD and $82 with. Remember, though, that you’re stocking your electronics kit – all these parts can be used again for other projects.
1. Wiring
If you are using the LCD with the stacking header then connect it to the Raspberry Pi now. The first two stages of the software don’t use the LCD but it will save you some re-wiring time later if you attach it now.
Warning: Incorrect wiring could cause damage to your Raspberry Pi. Make sure to double check all your connections carefully before powering up.
Step 1: Power and Ground Rails



On the left in the photo you can see the red and black jumpers going to the + and – rails on the breadboard. To help describe the connections from the header please refer to this wire colour table. Each cell in the table refers to a pin on the Raspberry Pi header. The colour of the cell corresponds to the colour of the jumper wire as seen in the photo:



Connect pin 1 to the positive rail and pin 6 to the ground rail.
Step 2: MCP3008
Important: The chip must be located over the valley in the breadboard with the pin 1 indicator, the indentation, top right as in the photo.



Refer to this wire colour table and the datasheet when wiring up this chip:



All the connections from the rails and the header to the MCP3008 chip go neatly along the bottom row of the chip in this orientation. First connect the power and ground as show in the photo above:
- Ground rail to DGND
- Ground rail to AGND
- Power rail to VREF
- Power rail to VDD
Next, connect the Raspberry Pi SPI header pins to the chip:
- Header 26 to CS
- Header 19 to DIN
- Header 21 to DOUT
- Header 23 to CLK



Step 3: Sensor



The sensor wiring is simple; there are three connections to make:
- Sensor yellow to CH5
- Sensor red to power rail
- Sensor black to ground rail



Finally, if you have your plant pot to hand you can insert the probe into the soil now. Make sure not to push it too deep, just cover the prongs:



2. Preparing The Software Environment
Step 1: Operating System
This project was built using Occidentalis v0.2 from Adafruit, which comes with the hardware SPI driver ready to go. Follow the instructions on the Adafruit site to install it on your Raspberry Pi.
Tip: If you use the Wheezy download from the Raspberry Pi site, you will then need to find and follow some steps to enable the SPI driver yourself..
Step 2: Required Utilities
git – You should already have this (try typing git
) if not then install it with: sudo apt-get install git
pip – install this with: sudo apt-get install python-pip
Step 3: SPI Python Wrapper
All the code for this project is written in Python. We need a Python wrapper for the SPI driver so we can read the values from the sensor over SPI:
1 |
|
2 |
cd ~
|
3 |
git clone git://github.com/doceme/py-spidev |
4 |
cd py-spidev/
|
5 |
sudo python setup.py install |
Step 4: Adafruit Python Library
You will already have installed the Adafruit library when assembling and testing your LCD. Make sure you know the location of the library as this will be needed by the project code.
Step 5: ControlMyPi Library
ControlMyPi is a service to quickly and easily make control panels on the Internet from your Raspberry Pi Python scripts. Use pip to install it:
1 |
|
2 |
sudo pip install controlmypi |
Step 6: Project Code
All the code from this tutorial can be downloaded like so:
1 |
|
2 |
cd ~
|
3 |
mkdir py
|
4 |
cd py
|
5 |
git clone git://github.com/jerbly/tutorials |
You will need to edit some of these files to supply user names, passwords and file paths. This is explained as we go.
3. Reading a Value From the Sensor
The MCP3008 converts the input voltage to a number from 0 to 1023 (10 bits). You can then read this value over SPI (the green and white connections to the header on the Pi). The py-spidev package allows us to do this from Python.
There is some subtle complication with reading a 10-bit number in an 8-bit system. The SPI driver will return two 8-bit words and we are interested in the last 2 bits of the first word followed by all 8 bits of the next word. To turn this into a number we can work with we need to mask and then shift left by 8 the 2 bits from the first word before adding the second word.
You don’t need to worry about coding this though, as in the download I have included the mcp3008.py module to take care of it. You can use an interactive Python shell to test your SPI setup and your wiring like so:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python |
3 |
Python 2.7.3 (default, Jan 13 2013, 11:20:46) |
4 |
[GCC 4.6.3] on linux2
|
5 |
Type "help", "copyright", "credits" or "license" for more information. |
6 |
>>> import mcp3008
|
7 |
>>> mcp3008.readadc(5) |
8 |
444 |
Important: You have to run Python with sudo so you can access the SPI device driver.
If you are having problems, first double check your wiring. Then double check the software installation steps above. If you are using the Octopus moisture sensor, hold the prongs to make a connection with your hand. Otherwise, it will likely just read zero.
You can take the sensor out of the equation here by connecting a jumper from the power rail to, say, CH1 and then using readadc(1). This should return 1023 (or close to it). Likewise, a connection from the ground rail should return 0.
Programs
Program 1: Monitoring In the Console
The first program simply extends what we practiced in the interactive console to include a loop so the value from the sensor is printed out continuously:
1 |
|
2 |
from time import sleep |
3 |
import mcp3008 |
4 |
|
5 |
while True:
|
6 |
m = mcp3008.readadc(5) |
7 |
print "Moisture level: {:>5} ".format(m) |
8 |
sleep(.5) |
Notice that there is a half-second sleep in the loop. This is so the program yields; it gives way to other processes. Otherwise it would use up a lot of CPU time and other processes you are running would not perform so well. A reading twice per second is probably way too much anyway, certainly for a moisture sensor.
The output should be like so:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_cmd.py |
3 |
Moisture level: 452 |
4 |
Moisture level: 486 |
5 |
Moisture level: 485 |
6 |
Moisture level: 483 |
7 |
Moisture level: 489 |
8 |
Moisture level: 491 |
9 |
Moisture level: 490 |
10 |
^CTraceback (most recent call last): |
11 |
File "moist_cmd.py", line 7, in <module> |
12 |
sleep(.5) |
13 |
KeyboardInterrupt |
14 |
pi@raspberrypi ~/py/tutorials/moisture $
|
Just press Control-C when you’re finished and the program will exit as above.
Program 2: Better Console Monitoring
This program improves the display to the console. Rather than print the value out to the screen in a scrolling window, this script will create a dashboard effect. Each sensor reading is written over the top of the last so your window doesn’t scroll.
Also red, yellow and green backgrounds are used as traffic light highlights. When the soil is well watered the background will be green. When it is too dry it’ll show red.
To achieve this colouring, special ANSI escape codes are used to send commands to the console. Each escape code sequence starts with \x1b[ followed by the command codes to produce an effect.
1 |
|
2 |
from time import sleep |
3 |
import mcp3008 |
4 |
|
5 |
# ANSI escape codes
|
6 |
PREVIOUS_LINE="\x1b[1F" |
7 |
RED_BACK="\x1b[41;37m" |
8 |
GREEN_BACK="\x1b[42;30m" |
9 |
YELLOW_BACK="\x1b[43;30m" |
10 |
RESET="\x1b[0m" |
11 |
|
12 |
# Clear the screen and put the cursor at the top
|
13 |
print '\x1b[2J\x1b[H'
|
14 |
print 'Moisture sensor'
|
15 |
print '===============\n'
|
16 |
|
17 |
while True:
|
18 |
m = mcp3008.readadc(5) |
19 |
if m < 150:
|
20 |
background = RED_BACK
|
21 |
elif m < 500:
|
22 |
background = YELLOW_BACK
|
23 |
else:
|
24 |
background = GREEN_BACK
|
25 |
print PREVIOUS_LINE + background + "Moisture level: {:>5} ".format(m) + RESET |
26 |
sleep(.5) |
The program clears the screen and displays a title. It then goes into the perpetual loop again but this time uses thresholds to determine the background colour. Finally, the escape sequences and text are printed out followed by a RESET
sequence to switch the colouring off. The PREVIOUS_LINE
escape code is used to move the cursor back up one line so that we write over the top of the previous value each time.
Run this example like so:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_ansi.py |
The output should be something like this:



Program 3: LCD Monitoring
We’re now going to move away from the console and display the sensor data on the LCD instead. Before continuing with this tutorial, make sure you have built your Adafruit LCD with the stacking header and you’ve tested it by following the Adafruit tutorial.
Our program is going to use the Adafruit library so we need to know the full path to the Adafruit_CharLCDPlate
directory. I simply create a py directory under the pi user’s home to keep all the Python code in one place, so on my Raspberry Pi the path is:
1 |
|
2 |
/home/pi/py/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate |
You may need to adjust the following script if your path is different.
1 |
|
2 |
import sys |
3 |
sys.path.append('/home/pi/py/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate') |
4 |
|
5 |
from time import sleep |
6 |
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate |
7 |
import mcp3008 |
8 |
|
9 |
lcd = Adafruit_CharLCDPlate() |
10 |
|
11 |
while True:
|
12 |
m = mcp3008.readadc(5) |
13 |
try: |
14 |
lcd.home()
|
15 |
lcd.message("Moisture level:\n%d " % m) |
16 |
if m < 150:
|
17 |
lcd.backlight(lcd.RED) |
18 |
elif m < 500:
|
19 |
lcd.backlight(lcd.YELLOW) |
20 |
else:
|
21 |
lcd.backlight(lcd.GREEN) |
22 |
except IOError as e: |
23 |
print e |
24 |
sleep(.5) |
Run the program like so:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_lcd.py |
You should see the LCD version of the colour console program. This time the red, yellow and green threshold indicators make use of the RGB backlight.



Program 4: Remote Monitoring With ControlMyPi
In this section we’re going to make use of ControlMyPi to produce a dashboard on the Internet. Here’s how the service is described from the FAQ:
ControlMyPi provides a web based service to allow simple Python scripts to be controlled from a panel over the Internet. Your Python script defines a widget layout of labels, buttons, status indicators and more for ControlMyPi to display. When a button is clicked your script will get a message. If you have some status to report, your script can send that to ControlMyPi at any time and it’ll be pushed out to your web browser.
If you haven’t already installed the Python package, then do so now. You will also need an XMPP account. I would recommend a Google Gmail account since this works well and will also be used later when we want our script to send an email. Follow the instructions on the ControlMyPi website to get started and to test your connection.
The first iteration of the dashboard will simply show a single gauge with the sensor reading. We’ll push a new reading up to ControlMyPi every 30 seconds to produce this:



The script to produce this is still relatively straight-forward:
1 |
|
2 |
from time import sleep |
3 |
import mcp3008 |
4 |
from controlmypi import ControlMyPi |
5 |
import logging |
6 |
|
7 |
def on_msg(conn, key, value): |
8 |
pass |
9 |
|
10 |
logging.basicConfig(level=logging.INFO) |
11 |
|
12 |
p = [ |
13 |
[ ['G','moist','level',0,0,1023] ], |
14 |
]
|
15 |
|
16 |
conn = ControlMyPi('you@gmail.com', 'password', 'moisture', 'Moisture monitor', p, on_msg) |
17 |
if conn.start_control(): |
18 |
try: |
19 |
while True:
|
20 |
m = mcp3008.readadc(5) |
21 |
conn.update_status({'moist':m}) |
22 |
sleep(30) |
23 |
finally: |
24 |
conn.stop_control()
|
At the top, we’re now importing the ControlMyPi class and the logging module. If you need to debug any XMPP connection problems change the log level to DEBUG
.
The on_msg function
is what would be called if we defined any buttons or other inputs in our widget layout. Since we only have a gauge there is no input and so this function does nothing.
The list, p, is where the widget layout is defined. Each entry in the list defines a row of widgets to be displayed on the dashboard. We have a single row with a single widget on it, the gauge. Moist
is the name of the widget, level is the label to appear on the gauge, 0
is the initial level, 0
is the minimum level and 1023
is the maximum.
The script then creates a ControlMyPi object and starts up. Here you will have to supply your own Gmail address and password.
Tip: This is a secure connection to Google’s servers; the password is not shared with any other service. If you are at all concerned then simply set up another account. In fact, a separate account is quite handy, especially when you want to have automated emails sent from your Raspberry Pi.
Finally, the main loop takes the sensor reading as usual but instead of printing it to the console or the LCD it pushes the new value to ControlMyPi. Updates are sent in a map of widget name / value
.
Run the script using sudo as usual:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_cmp.py |
Some connection information will be printed to the console followed by “Registered with controlmypi”. Now go to ControlMyPi.com and enter your Gmail address in the form:



Click Find panels to show a list of control panels associated with your Gmail address. There should be one in the list, Moisture monitor. Click this to start your panel and see the live data pushed from your Raspberry Pi.
Every 30 seconds, when a new sensor reading is taken, this value is pushed through the XMPP connection to ControlMyPi. It then pushes this data to all viewers of your panel (just you at the moment). You’ll see the gauge move before your eyes! If you like, you can copy/paste the long url and send it to a friend to show off your moisture monitor. Now updates will be pushed to you and your friend.
Program 5: Adding a Line Chart
ControlMyPi has a number of widgets available for you to use on your dashboard. One of the most useful for visualising data is the line chart. The program in this section adds a simple line chart to plot the changes in moisture level over time. To begin, we’ll just plot a new point on the chart every thirty seconds when we take a reading.
1 |
|
2 |
from time import sleep |
3 |
import mcp3008 |
4 |
from controlmypi import ControlMyPi |
5 |
import logging |
6 |
import datetime |
7 |
|
8 |
def on_msg(conn, key, value): |
9 |
pass |
10 |
|
11 |
def append_chart_point(chart, point): |
12 |
if len(chart) >= 10: |
13 |
del chart[0] |
14 |
chart.append(point) |
15 |
return chart
|
16 |
|
17 |
logging.basicConfig(level=logging.INFO) |
18 |
|
19 |
p = [ |
20 |
[ ['G','moist','% level',0,0,100], ['LC','chart1','Time','Value',0,100] ], |
21 |
]
|
22 |
|
23 |
c1 = [] |
24 |
|
25 |
conn = ControlMyPi('you@gmail.com', 'password', 'moistcmp2', 'Moisture monitor 2', p, on_msg) |
26 |
if conn.start_control(): |
27 |
try: |
28 |
while True:
|
29 |
dt = datetime.datetime.now().strftime('%H:%M:%S') |
30 |
m = mcp3008.read_pct(5) |
31 |
c1 = append_chart_point(c1, [dt, m]) |
32 |
conn.update_status({'moist':m,'chart1':c1}) |
33 |
sleep(30) |
34 |
finally: |
35 |
conn.stop_control()
|
In the widget layout, p
, you can see the definition of the line chart widget after the gauge. The widget name is chart1
, the x axis
is Time, the y axis
is Value and the max
and min
values are set at 0 and 100.
Tip: I’ve changed the scale from the raw sensor range (0 to 1023) to a more meaningful percentage value. The mcp3008 module provides a function read_pct() to return an integer percentage point instead of the raw value.
To update the chart on the dashboard, you simply send a list of data points to ControlMyPi. The data points are held in the list c1 in the program and each point consists of a time and a value. The function append_chart_point()
is used to maintain a rolling list of the last ten points, so as each new value arrives the oldest one is deleted. Without this the chart would grow forever.
Run the script using sudo as usual:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_cmp2.py |



Program 6: Improving the Line Chart
This script improves on the previous version by:
- Re-introducing the LCD so we have both local and online monitoring.
- Recording data points on the line chart over 24 hours.
- Smoothing out the fluctuations on the chart by taking an average.
- Saving the chart data to a file so we don’t lose it if we need to restart the program.
1 |
|
2 |
import sys |
3 |
sys.path.append('/home/pi/py/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate') |
4 |
|
5 |
from time import sleep |
6 |
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate |
7 |
import mcp3008 |
8 |
from controlmypi import ControlMyPi |
9 |
import logging |
10 |
import datetime |
11 |
import pickle |
12 |
from genericpath import exists |
13 |
|
14 |
lcd = Adafruit_CharLCDPlate() |
15 |
|
16 |
PICKLE_FILE = '/home/pi/py/moisture/moist.pkl' |
17 |
|
18 |
def on_msg(conn, key, value): |
19 |
pass |
20 |
|
21 |
def append_chart_point(chart, point): |
22 |
if len(chart) >= 48: |
23 |
del chart[0] |
24 |
chart.append(point) |
25 |
return chart
|
26 |
|
27 |
def save(data): |
28 |
output = open(PICKLE_FILE, 'wb') |
29 |
pickle.dump(data, output) |
30 |
output.close()
|
31 |
|
32 |
def load(default): |
33 |
if not exists(PICKLE_FILE): |
34 |
return default
|
35 |
pkl_file = open(PICKLE_FILE, 'rb') |
36 |
data = pickle.load(pkl_file) |
37 |
pkl_file.close()
|
38 |
return data
|
39 |
|
40 |
def update_lcd(m): |
41 |
try: |
42 |
lcd.home()
|
43 |
lcd.message("Moisture level:\n%d%% " % m) |
44 |
if m < 15:
|
45 |
lcd.backlight(lcd.RED) |
46 |
elif m < 50:
|
47 |
lcd.backlight(lcd.YELLOW) |
48 |
else:
|
49 |
lcd.backlight(lcd.GREEN) |
50 |
except IOError as e: |
51 |
print e |
52 |
|
53 |
logging.basicConfig(level=logging.INFO) |
54 |
|
55 |
p = [ |
56 |
[ ['G','moist','% level',0,0,100], ['LC','chart1','Time','Value',0,100] ], |
57 |
]
|
58 |
|
59 |
c1 = load([]) |
60 |
|
61 |
readings = [] |
62 |
|
63 |
conn = ControlMyPi('you@gmail.com', 'password', 'moisture3', 'Moisture monitor 3', p, on_msg) |
64 |
|
65 |
delta = datetime.timedelta(minutes=30) |
66 |
next_time = datetime.datetime.now() |
67 |
|
68 |
if conn.start_control(): |
69 |
try: |
70 |
while True:
|
71 |
dt = datetime.datetime.now() |
72 |
m = mcp3008.read_pct(5) |
73 |
readings.append(m) |
74 |
update_lcd(m) |
75 |
to_update = {'moist':m} |
76 |
if dt > next_time: |
77 |
# Take the average from the readings list to smooth the graph a little
|
78 |
avg = int(round(sum(readings)/len(readings))) |
79 |
readings = [] |
80 |
c1 = append_chart_point(c1, [dt.strftime('%H:%M'), avg]) |
81 |
save(c1) |
82 |
next_time = dt + delta
|
83 |
to_update['chart1'] = c1 |
84 |
conn.update_status(to_update) |
85 |
sleep(30) |
86 |
finally: |
87 |
conn.stop_control()
|
You should recognise the content of the update_lcd()
function from the earlier program. This function is now called from the main loop to update the LCD on each iteration.
Tip: If you don’t have the LCD simply delete this function and the line that calls it. Also remove the path and import from the top and the line lcd = Adafruit_CharLCDPlate()
.
Recording Data
To record 24 hours of data on the line chart, we will take a reading every 30 minutes and show 48 points on the chart. The append_chart_point()
function has been updated to keep 48 data points. In the main loop we now hold a time 30 minutes in the future in the variable next_time
.
Each loop we check the current time against the next_time
. If we’ve passed next_time
a data point is appended to the chart and next_time
is moved forward by 30 minutes again. Using the clock is a neat way to perform actions at different time granularities without having to have multiple counters counting loops and so on. We’ll use this technique again in the final program to send a daily email.
As you may have noticed, the reading from the sensor fluctuates quite a bit. Other types of sensors won’t necessarily do this, but this one does. So, instead of taking a single reading and plotting that on the graph every half an hour, we’re going to plot the average of all the readings taken in the last half an hour. The readings list is used to hold all the readings, int(round(sum(readings)/len(readings)))
calculates the average to the nearest whole number. This is then plotted on the chart.
Python’s pickle module is used to save and load the chart data to a file. This simply stores the c1 list in case we have to stop the program and start it again. A reboot for a battery change for example. The save()
function is called every time we update the chart and the load()
function is called whenever the program starts.
Run the script using sudo as usual:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_cmp3_lcd.py |
The screenshot shows a much smoother chart now:



The Final Program
To finish this project off, we’re going add one last step: email. We can monitor the sensor from the LCD and look at the current reading and history on the web, but we might forget to check on it after a while. To cover this, we’re going to get the program to send an email once a day whenever the sensor reading is below a specified value.
1 |
|
2 |
import sys |
3 |
sys.path.append('/home/pi/py/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCDPlate') |
4 |
|
5 |
from time import sleep |
6 |
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate |
7 |
import mcp3008 |
8 |
from controlmypi import ControlMyPi |
9 |
import logging |
10 |
import datetime |
11 |
import pickle |
12 |
from genericpath import exists |
13 |
import smtplib |
14 |
|
15 |
lcd = Adafruit_CharLCDPlate() |
16 |
|
17 |
PICKLE_FILE = '/home/pi/py/moisture/moist.pkl' |
18 |
|
19 |
def on_msg(conn, key, value): |
20 |
pass |
21 |
|
22 |
def append_chart_point(chart, point): |
23 |
if len(chart) >= 48: |
24 |
del chart[0] |
25 |
chart.append(point) |
26 |
return chart
|
27 |
|
28 |
def save(data): |
29 |
output = open(PICKLE_FILE, 'wb') |
30 |
pickle.dump(data, output) |
31 |
output.close()
|
32 |
|
33 |
def load(default): |
34 |
if not exists(PICKLE_FILE): |
35 |
return default
|
36 |
pkl_file = open(PICKLE_FILE, 'rb') |
37 |
data = pickle.load(pkl_file) |
38 |
pkl_file.close()
|
39 |
return data
|
40 |
|
41 |
def update_lcd(m): |
42 |
try: |
43 |
lcd.home()
|
44 |
lcd.message("Moisture level:\n%d%% " % m) |
45 |
if m < 15:
|
46 |
lcd.backlight(lcd.RED) |
47 |
elif m < 50:
|
48 |
lcd.backlight(lcd.YELLOW) |
49 |
else:
|
50 |
lcd.backlight(lcd.GREEN) |
51 |
except IOError as e: |
52 |
print e |
53 |
|
54 |
def send_gmail(from_name, sender, password, recipient, subject, body): |
55 |
'''Send an email using a GMail account.'''
|
56 |
senddate=datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d') |
57 |
msg="Date: %s\r\nFrom: %s <%s>\r\nTo: %s\r\nSubject: %s\r\nX-Mailer: My-Mail\r\n\r\n" % (senddate, from_name, sender, recipient, subject) |
58 |
server = smtplib.SMTP('smtp.gmail.com:587') |
59 |
server.starttls()
|
60 |
server.login(sender, password) |
61 |
server.sendmail(sender, recipient, msg+body) |
62 |
server.quit()
|
63 |
|
64 |
logging.basicConfig(level=logging.INFO) |
65 |
|
66 |
p = [ |
67 |
[ ['G','moist','level',0,0,100], ['LC','chart1','Time','Value',0,100] ], |
68 |
]
|
69 |
|
70 |
c1 = load([]) |
71 |
|
72 |
readings = [] |
73 |
|
74 |
conn = ControlMyPi('you@gmail.com', 'password', 'moisture', 'Moisture monitor', p, on_msg) |
75 |
|
76 |
delta = datetime.timedelta(minutes=30) |
77 |
next_time = datetime.datetime.now() |
78 |
|
79 |
delta_email = datetime.timedelta(days=1) |
80 |
next_email_time = datetime.datetime.now() |
81 |
|
82 |
if conn.start_control(): |
83 |
try: |
84 |
while True:
|
85 |
dt = datetime.datetime.now() |
86 |
m = mcp3008.read_pct(5) |
87 |
readings.append(m) |
88 |
update_lcd(m) |
89 |
to_update = {'moist':m} |
90 |
|
91 |
# Update the chart?
|
92 |
if dt > next_time: |
93 |
# Take the average from the readings list to smooth the graph a little
|
94 |
avg = int(round(sum(readings)/len(readings))) |
95 |
readings = [] |
96 |
c1 = append_chart_point(c1, [dt.strftime('%H:%M'), avg]) |
97 |
save(c1) |
98 |
next_time = dt + delta
|
99 |
to_update['chart1'] = c1 |
100 |
conn.update_status(to_update) |
101 |
|
102 |
#Send an email?
|
103 |
if dt > next_email_time: |
104 |
next_email_time = dt + delta_email
|
105 |
if m < 40:
|
106 |
send_gmail('Your Name', 'you@gmail.com', 'password', 'recipient@email.com', 'Moisture sensor level', 'The level is now: %s' % m) |
107 |
|
108 |
sleep(30) |
109 |
finally: |
110 |
conn.stop_control()
|
The send_gmail()
function takes care of sending the email. In the main loop, we’re using the clock checking technique to determine whether a day has passed since the last time we sent out an email. If it has then we bump this time on by a day for the next check. Next, if the moisture value is below 40, we send the email.
That’s the program complete! Run it using sudo
as usual:
1 |
|
2 |
pi@raspberrypi ~/py/tutorials/moisture $ sudo python moist_final.py |
There’s one last thing to do: automatically run the program on start up. This will allow you to run your Raspberry Pi headless. To do this edit the /etc/rc.local/
file like this:
1 |
|
2 |
pi@raspberrypi ~ $ sudo pico /etc/rc.local |
Add this line to the file and save it:
1 |
|
2 |
python /home/pi/py/tutorials/moisture/moist_final.py &
|
Now you can shut down your Raspberry Pi, move it to the plant you’re monitoring, power it up and the program will start for you.
Conclusion
In this tutorial, you learned how to set up and use SPI on your Raspberry Pi for use with an Analog-to-Digital converter. You then used a sensor to monitor the moisture level of the soil in a plant pot. The software allows us to see this sensor reading on the console, on an LCD, on a gauge and chart over the Internet and through a daily email.
There was a lot to learn, but now you can use these techniques for all sorts of different sensors to measure temperature, humidity, light intensity and so on. You could hook it up to an accelerometer or infrared distance sensor too. Have fun!


