This project is based of Pimoroni's project, Pimoroni Plants. It monitors the conditions in a garden (I'm using an IKEA VÄXER, because it's cheap and hydroponic), and tweets them along with a picture. However, their project uses their Flotilla modules (not available to the general public at the time of writing, and kinda over-complex), and a Raspberry Pi camera module (much more expensive than that old USB webcam we all have laying around). They've just released the Enviro pHAT, so I thought that it would be perfect to set up my own version. You can see it tweeting right now here.
You don't really need any experience with electronics, Raspberry Pi, Python or anything else to do this project. There's no hardcore wiring (just a spot of light PCB soldering), and it's easy to copy and paste all the code, if you so wish.
How-toThis project is pretty easy. First, you'll need to solder a 2x20 header into your Pi Zero (not required if you're using a different Pi), and Enviro pHAT. There are already a great many tutorials available on how to do this. A good one is available here. Next, you'll need to set up your Pi Zero with a keyboard, mouse, monitor, and network connection. You will need a mini HDMI to HDMI/VGA (whatever your monitor uses) adapter, and the USB hub. You'll also need an Ethernet cable, or a WiFi dongle, your SD card, and of course a Micro USB power adapter. Plug everything in, and power up.
Beware! The power must go into the right most micro USB port on the Pi Zero, and the USB hub into the left most. Otherwise, things will fry!
It's also possible to use a different model of Raspberry Pi for this, and transfer your SD card later on. Once you have booted into the Raspbian desktop, then open up a terminal window by clicking the little icon:
Then, you'll need to install all the necessary software. If you're using WiFi, you should set this up now by following this tutorial. Next, you'll need to enter a few commands to install the software.
Install the fswebcam
package, which we'll use to take pictures with our USB webcam:
sudo apt-get install fswebcam
You can test this to make sure it's working properly, by asking it to take a test picture. Just plug in your webcam, and run the command:
fswebcam your_image_name_here.jpg
Then, open the file manager and double click the image you just made. You should see a picture of whatever the webcam was pointing at!
Now we need to install the software needed for the Enviro pHAT. Luckily, the nice folks from Pimoroni made a handy one-liner for this. Just enter:
curl -sS get.pimoroni.com/envirophat | bash
(That little line is a vertical bar by the way, not a capital I or lower case l).
You can run some tests on this to see if it's working too. These are described in this excellent tutorial. We need to install the tweepy module, which allows our Python program to talk to Twitter. Just run:
sudo pip install tweepy
That's all the software installs done. You'll need to make a copy of the code below to your Raspberry Pi. I put it on a memory stick and transferred it. Put this code on the desktop, and make doubly-sure it's called 'plants.py
'.
You will also need to set up your twitter account at this point. Create a new account, and verify it with your phone (or else you can't have API access, which we need). Then, go to and create a new app. Note down the Consumer Key, Consumer Secret, Access Token, and Access Token Secret.
Double-click the plants.py
file on the desktop of your Raspberry Pi, and click 'edit file' in the dialogue box which comes up. Then, enter your data into the spaces at the top of the program where it says to.
You can test to see if this is working, by running this command:
sudo python /Desktop/plants.py
It should output some readings, and tweet a picture at the next quarter-hour interval (xx:00
, xx:15
, xx:30
or xx:45
). Once that's all working, then you need to set up an auto-run script. This is so that you don't have to manually click the file yourself, and it'll run automatically when the Pi is turned on. Enter:
cd Desktop
nano launcher.sh
This should get you a blank text. Add this to the script:
#!/bin/sh
# launcher.sh
null
null
cd /
cd home/pi/bbt
sudo python bbt.py
cd /
This code is also available in the 'code' section of this project. Then hit Ctrl-X, Y and Enter to save it. Make the script an executable:
chmod 755 launcher.sh
And test it with this command:
sh launcher.sh
Then add a error log in case something goes wrong:
cd
mkdir logs
We now need to create the actual autorun script. Enter the command:
sudo crontab -e
You might need to tell it which text editor to use - if it asks, choose Nano. You should now have a file filled with comments (starting with a #). After these lines, add this one:
@reboot sh /home/pi/Desktop/launcher.sh >/home/pi/logs/cronlog 2>&1
Reboot the Pi to check it's all working. If it is, you should see the Enviro pHAT's LEDs (one either side of the light/colour sensor) flash on and off during the boot sequence.
You're done! Now, go and attach your Raspberry Pi to your IKEA VÄXER, or whatever you want to monitor with it. You could make a remote monitoring for your chickens' coop, dog kennel, kitchen or anything else. Just remember that as the images are on Twitter, they're publicly accessible so don't set it up to tweet anything you're not happy for anyone to see (like your bedroom, or anything to do with security).
I attached my Pi Zero with an awesome Pibow Zero case and a sticky foam pad, and mounted the webcam near the plants with tape.
Going furtherThere's no limit to how far you can go with this. Check out Sandy J Macdonald's tutorial on the Enviro pHAT to see what other data you could extract. All the code is commented, so you should be able to find your way around it easily enough. It's by no means pretty - just what I came up with. You could put the data into a spreadsheet, or attach some extra hardware (LEDs or a screen) to show it visually. You can attach four more sensors to the Enviro pHAT's analogue input pins to get more data, or use a USB sensor (there are two spare ports on that hub). Personally, I will be working on adding some more intelligent reactions (like the "it's hot in here"
one, but for other data), and randomly selecting them. I'd also like to have a flask page on my network to broadcast the code's status to me. Have fun!
Comments