Environment sensors
How does our garden grow? With Raspberry Pis all in a row.
YOU’LL NEED
• RaspberryPi(anymodel)
• GrovePiorGrovePiZero • Adafruit1.3-inchBonnet (orotherdisplay) • GrovePiSensors (temperature,moisture,light)
WITH MORE EXTREME WEATHER conditions predicted in the future, the green-fingered amongst us may need to keep a closer eye on their plants. Temperature is one variable that can be displayed and graphed by a Pi with an LCD display, so for this project, we’ll take that idea a step further by measuring a range of environmental variables. Thermistors and most other components that measure such factors, are analog in nature, so they aren’t immediately suitable for the Pi, which only has digital (GPIO) inputs. However, thanks to HATs like the GrovePi, it’s easy to get your Pi talking to analog sensors. We used a high accuracy temperature and humidity sensor, a soil moisture meter, a light sensor, but more are available and getting them working with GrovePi is super easy.
We won’t cover setting up the Pi (any model will do), but you’ll probably want to get it set up for SSH and Wi-Fi access, especially if you’re planning on installing this in your greenhouse. You may have to coerce your Pi into using Python 3 by default (if it doesn’t already). This is a matter of
$ sudo update-alternatives --install /usr/ bin/ python python /usr/ bin/python2.7 1
$ sudo update-alternatives --install /usr/ bin/ python python /usr/ bin/python3.7 2
Now we can set up the GrovePi board, and Dexter Industries provide a script that will do this in one line: $ curl -kL dexterindustries.com/update_grovepi | bash
VISION ON
You’ll need to reboot once everything’s done, at which point we can start setting up our display. There are a huge range of TFT, OLED or LCD screens available for the Pi so this depends on your hardware. We used Adafruit’s 1.3-inch Pi Bonnet display and to set that up we had to install Pip and then install its Blinka library, which enables interfacing with their CircuitPython APIs:
$ sudo pip3 install --upgrade adafruit-pythonshellwget $ https://raw.githubusercontent.com/ adafruit/ Raspberry-Pi-Installer-Scripts/master/ raspi-blinka.pysudo python3 raspi-blinka.py
Then reboot and you should be ready to go. Reading from our combined temperature/humidity sensor requires copying and pasting from https:// github.com/ControlEverythingCommunity/ TH02, since the bit-banging is complex. But our moisture and light sensors can be read with one liners: import grovepi moisture = grovepi.analogRead(1) light = grovepi.analogRead(2)
Having got data from the sensors, we need to get it on the display. The Adafruit Python libraries enable images to be sent directly to the display, so we can use the Pillow libraries to help us. There is some boilerplate to initialize the display (which we’ll omit here, but you’ll find it at https://learn.adafruit.com/ adafruit-1-3- color-tft-bonnet-for-raspberry-pi/ python-stats-example): from digitalio import DigitalInOut, Direction from PIL import Image, ImageDraw, ImageFont import adafruit_rgb_display.st7789 as st7789
Then you can use regular PIL image and text commands to draw the info on screen height = disp.height width = disp.width image = Image.new(“RGB”, (width, height)) rotation = 90 image.draw = ImageDraw.Draw(image)
LOOP THE LOOPS
For this project to be useful, we need the display to continuously update. We make a loop, which clears the screen, updates readings, pauses and repeats fnt = ImageFont.truetype(“/usr/share/fonts/ truetype/dejavu/ DejaVuSans.ttf”, 30) while True: moisture = grovepi.analogRead(1) light = grovepi.analogRead(2) draw.rectangle((0, 0, width, height), outline=0, fill=0)
draw.text((20,120), “Light {}”.format(l), font= fnt, fill=”#FFFF00”)
draw.text((20,210), “Moisture {}”.format(m), font= fnt, fill=”#00FFFF”) disp.image(image) time.sleep(0.5)
You can go further with this project, such as logging and graphing temperatures with Gnuplot. Or connect a watering system via a relay and never have to worry about your peonies drying out again!