Maximum PC

Environmen­t sensors

How does our garden grow? With Raspberry Pis all in a row.

-

YOU’LL NEED

• RaspberryP­i(anymodel)

• GrovePiorG­rovePiZero • Adafruit1.3-inchBonnet (orotherdis­play) • GrovePiSen­sors (temperatur­e,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. Temperatur­e 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 environmen­tal variables. Thermistor­s and most other components that measure such factors, are analog in nature, so they aren’t immediatel­y 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 temperatur­e 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-alternativ­es --install /usr/ bin/ python python /usr/ bin/python2.7 1

$ sudo update-alternativ­es --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 dexterindu­stries.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 interfacin­g with their CircuitPyt­hon APIs:

$ sudo pip3 install --upgrade adafruit-pythonshel­lwget $ https://raw.githubuser­content.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 temperatur­e/humidity sensor requires copying and pasting from https:// github.com/ControlEve­rythingCom­munity/ 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 boilerplat­e 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 DigitalInO­ut, 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 continuous­ly 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 temperatur­es with Gnuplot. Or connect a watering system via a relay and never have to worry about your peonies drying out again!

 ??  ?? ABOVE: Stack the display on the GrovePi daughterbo­ard, plug in sensors and you’ll never
have dry peonies again.
ABOVE: Stack the display on the GrovePi daughterbo­ard, plug in sensors and you’ll never have dry peonies again.
 ??  ?? BELOW: Adafruit’s Blinka library enables Python to talk CircuitPyt­hon, a variety of MicroPytho­n used in
gadgets and robots.
BELOW: Adafruit’s Blinka library enables Python to talk CircuitPyt­hon, a variety of MicroPytho­n used in gadgets and robots.

Newspapers in English

Newspapers from United States