Thursday, March 5, 2015

Raspberry Pi Urban Agriculture TweetBot Monitoring System

We're using an Anitech IR 160 USB webcam. 
March 5, 2015 -- Helios Labs We're building an automated urban agricultural project, with at least one garden on the rooftop, and an indoor LED-lit project similar to our already successful indoor aquaponics system (recently just moved).

The project will include a means to monitor and even control it online using Twitter among other applications. It will include the following features:

1. TweetBot to automatically tweet pictures of the garden several times a day.
2. TweetBot to automatically tweet water level and soil moisture data at least once a day.
3. The ability to water plants and feed fish remotely.
4. The ability to take pictures on command remotely.

For now, we have the TweetBot automatically uploading pictures of the system several times a day. Here's how we did it. If you make it through this tutorial, you'll be able to add on other features. To make this TweetBot, we've used a Raspberry Pi B+ (RPi).

Making a TweetBot tweet pictures of the garden several times a day.

1. Setting up Raspberry Pi: We downloaded the Rasbian .zip file found on Raspberry Pi's official website, unzipped it, and used Win32 Disk Imager to put it on our mini SD card. We placed the SD card into our Raspberry Pi B+ and followed the instructions in setting up the desktop. Raspberry Pi's official website has a help section full of links showing you the absolute basics, in case you get stuck.

Follow this guide to help you download an OS and put it on your SD card for installing on your RPi.

We also did some practice using the terminal and even setting up a way to wirelessly control it using a laptop within the same network (same room using the same WiFi connection). A good tutorial showing these steps can be found here.


NOTE: We used a Wi-Pi wireless adapter and it worked with the Raspian OS with no extra steps. If you don't have a WiFi adapter, you can just connect your RPi via an Ethernet cable into your home or office's router. We've got a TP-Link 725n that doesn't work with Raspian (but does with other RPi OS') so do some research before getting a WiFi adapter. 
2. Making your Twitter Application and Automating Tweets: We followed this tutorial, How to Build a Raspberry Pi Twitter Bot, to set up our Twitter application, and got our first tweets working. You're probably going to want to make a new e-mail address and Twitter account for your project. You'll be doing a lot of testing and a lot of tweets your regular followers will probably get tired of real quick. Note that you'll need to verify your account with a phone number but some tutorials say you can delete the phone number from your account after you set up your application.

Using CRON: A few other things to note if you are new to Raspberry Pi, Linux, and messing around with computers in general. Setting up the CRON... CRON is a way to automate processes on your RPi. If you are using the terminal (which you should practice using while doing step 1.) type in sudo crontab -e and a sort of text editor will open. Use your arrow to get to the bottom of all the text in there and add some new lines to schedule things. Some examples found in this tutorial are included below:
01 * * * * echo "This command is run at one min past every hour"
17 8 * * * echo "This command is run daily at 8:17 am"
17 20 * * * echo "This command is run daily at 8:17 pm"
00 4 * * 0 echo "This command is run at 4 am every Sunday"
* 4 * * Sun echo "So is this" 42 4 1 * * echo "This command is run 4:42 am every 1st of the month" 01 * 19 07 * echo "This command is run hourly on the 19th of July"
Some more seasoned users might laugh, but when I first did this I included "echo" in with everything. Actually, if the script you are running is called TweetBot.py and you saved it in a folder named TweetBot, you would write a scheduled event like this:
17 8 * * * python /home/pi/TweetBot/TweetBot.py
This means that at 0817 in the morning, everyday, your Raspberry Pi will take a picture and upload it to Twitter. If you wanted to tweet several pictures throughout the day it would look something like this:
15 6 * * * python /home/pi/TweetBot/TweetBot.py
30 8 * * * python /home/pi/TweetBot/TweetBot.py
15 15 * * * python /home/pi/TweetBot/TweetBot.py
15 17 * * * python /home/pi/TweetBot/TweetBot.py
15 0 * * * python /home/pi/TweetBot/TweetBot.py
This would have your Raspberry Pi tweeting pictures at 6:15 am, 8:30 am, 3:15 pm, 5:15 pm, and 15 minutes past midnight.

If your camera has no issues, it may work perfectly without any further steps. If you have imaging issues that are not related to the driver or low power, try this next step.

3. Using Fswebcam Instead of Pygame.

We ran into some problems with our webcam and for the longest time could not figure out how to resolve it. Thanks to the people at MakerZoo, we eventually fixed it -- proving the value of having local makerspaces and their role in empowering people to create and take full ownership of technology.

Pygame is the library used by the author of the tutorial we followed. Pygame will basically control your camera. There might be ways to do this next technique using Pygame, but the way we fixed the issues we had with our webcam (pink and green tint, split frames, and overall terrible image quality) was by using fswebcam. Fswebcam is an application RaspberryPi.org itself recommends beginners to use when taking images in this tutorial.

The tutorial shows you how to install fswebcam and gives you some tips on how to use it to take and save pictures locally. We highly recommend doing this tutorial first before moving on to the steps below. If nothing else, you've got to install fswebcam before moving on.

The script in the Twitter application tutorial above uses pygame. You can erase pygame from the above tutorial's script and use fswebcam instead. The final script should looke like that pictured below.


Here is the code using fswebcam:
#!/usr/bin/env python
import sys
import time
from twython import Twython
import os
os.system("fswebcam -r 1280x960 -S 80 --no-banner webcam.jpg")
CONSUMER_KEY = 'Your Info Here'
CONSUMER_SECRET = 'Your Info Here'
ACCESS_KEY = 'Your Info Here'
ACCESS_SECRET = 'Your Info Here' 
timestamp = time.strftime("%d/%m/%Y at %H:%M")
photo = open('webcam.jpg','rb')
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

api.update_status_with_media(media=photo, status='UrbanAgBKK aquaponics prototype  on '+timestamp+'')
The '-S' basically tells your computer to give your webcam a few frames to adjust itself before taking a picture. We set it to skip 80 frames but as few as 5 might work for you. Experiment to see which setting works best under the conditions your webcam are under.

Before the fix, our cam was still adjusting each time Pygame took a picture, which is why the IR settings, lighting, focus, and image processing was always apparently "off" when the image was uploaded to Twitter. Here's a little before and after in the images below.


The webcam we're using, the Anitech IR 160 has a manual focus, so make sure that's set up properly too. The above images were taken on a very well lit balcony. So at mid day, the sun was too bright for the webcam even with the frame delay on.

Below are images taken from the project now, which you can follow on Twitter @UrbanAgBKK.


What's next? Taking pictures is nice, a 24/7 streaming video would be better. Also, incorporating a water level sensor, feedback on the automatic fish feeder would be nice, so would soil moisture levels and automated watering stats as we add those features to this project. We'll continue posting code and "how to's" for all of these steps. The entire point of this project is to encourage people to make a physical version of "Farmville" and get better connected with both food and technology.

Hopefully you've been able to follow the steps above, including the tutorials included in the links. If you have any problems, please contact us. We know what it's like to run into a dead end and we'd be happy to help. Below is a list of the tutorials we referenced.

1. Help, via RaspberryPi.org
2. Installing Operating System Images, via RaspberryPi.org
3. Remote Access Secure Shell (SSH), via RaspberryPi.org
4. How to Build a Raspberry Pi Twitter Bot, via MakeUseOf.com
5. Automate Your WordPress Backup With Simple Shell Scripting & CRON, via MakeUseOf.com
6. Using a Standard USB Webcam, via RaspberryPi.org

Follow Helios Labs on Twitter @HeliosLabs or find us on Facebook here
3DClass750