Social

Social

Social

Friday, February 26, 2016

Worm Bin Moisture Sensor with Arduino

Arduino connected to Rapsberry Pi with USB
Arduino Uno with analog moisture sensor and usb connected to Pi















I have my worm bin temperature sensors working very well with my Raspberry Pi.  I have now turned my attention to measuring soil moisture.

Moisture Sensing Choices:

  • Which soil moisture sensor should I use?
  • Which micro controller do I employ?
  • How many sensors are needed for practical use?

After a lot of searching for sensors, it seems that moisture sensing is not nearly as straightforward  as reading air or soil temperatures.  There are different ways of reading moisture.

I honestly do not know the ideal way to read moisture. It seems that measuring the electrical conductivity of the soil to look for moisture is a popular method so I will go with that.

Sensor Corrosion Problem

One significant problem with sensors using electrical resistance is that they do not last very long because they corrode due to the electricity and metal contacting wet soil.  To help minimize this issue, you can get them gold plated and only power the sensor before you take your reading and then turn it off to maximize sensor contact life.

Note: I am a noob regarding electrical things but it looks like I might be able to control power to the sensor with a transistor (It acts as an electronic switch).

I opted to go with a quick-and-dirty approach first.  The Raspberry Pi has no analog pins to read and write to.  Moisture sensors require analog connections to the microcontroller to work.  After all, 0 and 1 tell you wet or dry but no values in between.

The sensor I bought first is http://amzn.to/1Q68YSW

I happen to have an Arduino Uno that I played with a few years back, so its time to dust it off  and put to work.  Actually, my board is revision 3 which looks current.  

I proceed to install the Arduino ide and make sure it works with a few simple LED light blinking sketches.  All good so far.

My moisture sensor arrives from Amazon and I realize it has absolutely no documentation so I start googling what is printed on it "ywRobot mositure sensor" and come across some schematics and this helpful video... (it's actually wRobot I think)



I follow along with the video and within a couple of minutes I have it reading values 0.  I drop it into a shot glass of water and the reading changes to around 600 give or take.


Setup Raspberry Pi to Log Serial Data 

(That is send moisture readings via USB from your Arduino sketch to Pi)


Make sure Python is installed on Pi.  

You can attempt to install or check version.
install:
sudo apt-get install python 
or check python version:
python -V

Install moreutils... 

to get TS (time stamp) package.  moreutils is a basic linux grouping of hand features not included in standing linux distribution.

sudo apt-get install moreutils

Install grabserial

grabserial is a utility to read serial data from serial port of remote linux microcontroller and write to standard out (like a text file for later use).

wget https://github.com/tbird20d/grabserial/blob/master/grabserial


Test serial (USB) connection

lsusb

I see all the usb connected devices including my arduino:
Bus 001 Device 004: ID 2341:0043 Arduino SA Uno R3 (CDC ACM)

Get device id
This is a little tricky.  I used grep to get keyword related results since my results spanned multiple pages:

dmesg | grep tty
I looked through results and found my id of TTYACM0
You can just page through results with:
dmesg | more

Incidentally, the /dev directory is a listing of hardware devices and those prepended with 'tty' are serial devices.

NOTE: the symbol | is typed by using shift : on your keyboard.  It's called a pipe command.

Configure grabserial to use the correct serial port

use the text editor nano (You can us vi and others but vi has a steeper learning curve.):  
nano grabserial

within the file use the ctrl-w command to jump right to the lines to configure.  Search for 'sd.port' (which takes your down about 7 pages).

Change your setting to match your serial port id found in dmesg:

        sd.port="/dev/ttyACM0"
        sd.baudrate=9600

You are setting the baud rate to match what your arduino serial port was set to in the sketch you installed.

Within grabserial file save with CTRL-x to exit and Y to save changes.

Check that grabserial is working:

python grabserial

I get output:
  , 0
because my sensor is not in water so all works and I CTRL-c to stop.

to get the timestamp for each reading

python grabserial | ts
Feb 14 16:53:26  , 0


Permanently Log Moisture Readings

There are a lot of ways to process the data now.  You can save to database or save to text (csv) file.  You can then use a web page to display, chart, or export to your computer to use in excel or your favorite charting program.

To write the data to a csv file for use later with use:
python grabserial | ts  >  moisture.csv 

To add a new data file in our sensor directory (to chart with the other sensors):
python grabserial | ts  >  /home/pi/projects/temp-and-humidity/sensor-values/moisture.csv

or even better yet, run the command above with a cron job then edit the worm bin web page to read the data (/home/pi/projects/temp-and-humidity/public/index.html)   You can check this crontab tutorial for more info on doing this.

Using Raspberry Pi Only

In this post I am using the Arduino because the Raspberry Pi does not come with analog pins and I did not have a spare Raspberry Pi lying around.  However, there is a way to use analog devices with Raspberry Pi using an analog to digital controller (ADC).  I will explore that in another post.

Number of Sensors


I have heard suggestiongs to go with 1 sensor every foot.  In this case, my 3 x 4 ft bin could use 3 sensors.  Once I settle on a platform, I will pickup 2 more sensors, figure out the switchin them on and off method, solder up some long wires to a board, connect them to one of the controllers, and coat the electronics with heat shrink tubing and silicone conformal coating to prevent moisture damage.

Conclusion

You can use any micro controller to read analog inputs.  It may just come down to which platform you are confortable with.  There are major differences in Raspberry Pi (a full computer operating system)  and Arduino (real time microcontroller), but simple analog readings can be done with both platforms.


Saturday, February 20, 2016

Keeping Your Raspberry Pi Connected Via WiFi

Is Your Raspberry Pi Loosing Wifi Connection? 

When my local network's access point or router restarts my Raspberry Pi does not always reconnect to the network.   This can also happen if the Pi's wifi is flaky due to distance or other issues.

To fix this I created a shell script that runs at start up based on this post.

in /home/pi add file:
nano network-monitor.sh

#!/bin/sh -e
while true ; do
   if ifconfig wlan0 | grep -q "inet addr:" ; then
      sleep 60
   else
      echo "Network connection down! Attempting reconnection."
      ifup --force wlan0
      sleep 10
   fi
done
exit 0

I run this script in /etc/rc.local
#!/bin/sh -e
/home/pi/network-monitor.sh &
exit 0

sudo nano /etc/rc.local

Then made sure rc.local was executable:
sudo chown root /etc/rc.local
sudo chmod 755 /etc/rc.local

Make sure rc.local is executable:
sudo chmod +x /home/pi/network-monitor.sh
Check that setting change was made (look for x in permissions of file)
 ls -al /home/pi

Test if it will work in startup:
sudo /etc/init.d/rc.local start

It it works, restart your Pi:
CTRL-C to stop script if needed
sudo reboot

You can further test by restarting your access point or router.

Thursday, February 4, 2016

Worm Bin Monitor Live: Part 3

I am continuing to refine my flow-through worm bin temperature monitoring system.  In part 1 and part 2 I started prototyping what the system would do based on a couple of third party tutorials that I combined.

Worm Bin Up & Running

I am now have everything working in the real world.  To do that I needed

  • a decent wireless network signal outdoors to talk to the system from inside my home
  • more permanent electronic connections so things don't fall apart if moved
  • a bin full of worms
  • mild protection for electronic components


dust-resistant enclosure

Outdoor Wi-fi

I have an indoor wireless access point but it does not get me very far outdoors.  I needed to get good network access at least 250 feet though many large pine trees to monitor anything.  To do this I purchased and installed a wireless access point designed for outdoor use.

I mounted this AP on the outside wall of my second story.  I had my doubts it would work very well, but I was really surprised that it does.

I also boosted the reception of the Raspberry Pi at the worm bin with a USB wireless adapter with an external antenna.  The downside of this particular antenna is that it requires a powered USB hub to stay well connected.

I purchased the EnGenius ens202-ext Outdoor Wireless Access Point and found a powered usb hub on Amazon.  The connection is working great.


Stabilizing The Electronics

In the working photo above, the red circled area is the air temperature and humidity sensor.  The green area is the solder less breadboard I used in previous steps to setup the sensors temporarily.

I had to learn to solder to make it all stable.  I have never soldered so I checked out some how-tos on the subject.  This soldering Instructable got me going.  I was in a hurry so I went down to the local Radio Shack (mine is still in business but has limited supply).

I picked up some 0.35 leaded rosin core solder, a pack of cheap circuit boards, and some 22 gauge solid core wire.  I already had a 25 watt soldering iron I had used years ago for something else.

Soldering takes practice but I was able to recreate my two sensor circuits on a small board.   I removed all the wires and resistors off of the original white breadboard .  I left the breadboard in place so I can add new sensors in the future (soil moisture sensor?).  I soldered wires onto the air sensor (red circle) to extend it outside of the foam box.

My worm bin is outdoors but under cover.  I am not too worried about moisture and dirt but do want some modest protection from dust.  I opted to reuse a foam food container to house the electronics.

Incidentally, these foam boxes are a major sore spot for me because locally all the restaurants hand them out for take out and leftovers.  In California, where I am from, Styrofoam has been illegal for many years.  The southern US is way behind on environmental awareness, so it's nice to re purpose things when I can.

Adding The Worms 

Finally...the worms are added.  I ordered 10K red wigglers online.  They came plump and lively.  I added them to the bin 24 hours ago.  

10 lbs of Eisenia Foetida worms

Final Thoughts on the Benefits of Environmental Monitoring

Our recent North Carolina temperatures have been unseasonable.  In February, we are seeing highs in 70s F and lows in 20s F.  My initial monitoring is demonstrating that the insulation and warming cable is keeping the bin temperature stable.  It's currently in the 50s.  By tomorrow evening we will see temperatures in the lower 20s.  

steady soil temp so far

Since I am a relatively new worm owner with a lot invested in worms I want to react quickly to problems.  The worm bin monitoring system will tell me how far temperatures rise or fall and for how long.  I can see measurable results for my efforts to control temperature with more heat in the winter, ventilation changes, recent feedings, and the addition of ice bottles in the summer months.

Raspberry Pi Model 2

I decided to replace the Raspberry Pi B+ with a Model 2.  It's much faster loading the page now.  However, there was a problem with the DHT (soil) sensor not working with the Adafruit library as I had installed it.

I made sure this was installed:
sudo apt-get install build-essential python-dev

Then reinstalled the Adafruit library to make sure.
cd /home/pi/sources/Adafruit_Python_DHT
sudo python setup.py install 

Now all is working correctly.