Category Archives: smallholding

Oxford Real Farming Conference 2015 – making a remote farm camera

This is a description of how to make a remote farm camera. Smallholders don’t always live on site, or you may have an island site somewhere without power. The simplest solution to get pictures from a remote site without power is to use a 3G trail camera and these work very well for tracking wildlife.

The trouble with this solution on a farm is that animals are meant to be on a farm all the time, Trail cameras look for warm-blooded critters so mammals and birds will set it off all the time, making this an expensive operation in MMS messages, which seems to be the preferred method. Even if you get a MMS bundle, trawling through the false alarms will bore you.

What we wanted of a remote farm camera

was to be able to check on how things were going, and whether something has been damaged by stormy weather. A CCTV camera on the farm would be fine, but the problem with this is the power drain, and getting the pictures back. If we had mains power this would be a lot easier, we could use a 3G CCTV DVR with remote access capability. You can easily get 12V CCTV gear, but the power drain of a typical DVR and camera is quite harsh – typically 1A or more. A typical leisure battery is 80Ah, but you should only use half of the capacity of a lead-acid battery that to avoid reducing the service life of the battery, and you must never fully discharge it. This gives you a battery life of less than two days.

Our remote farm camera uses a Raspberry Pi Model A and associated camera to take a picture every 15 minutes in the daytime and upload it to a website

1501_rpcam02

Example picture from the camera

Continue reading

A lower energy more expandable Pigcam network

Is covering a 12-acre farm with WiFi a reasonable idea? If so, I could run multiple cameras, say have one on the cows and one on the pigs, all connected to a central location, On the upside, the farm is roughly square and with a mild slope to the ridge at the top. Everything is pretty much line of sight. On the downside, there’s no good central location, which would be the obvious way to service the farm with 2.4GHz WiFi. Distances are long – the field is about 250m wide/long, and I could easily pick up a 300m path length feeding from the edge.

the MiFi node and power management

the MiFi node and power management

Earlier experiments showed that I could in theory use native WiFi, using a router to receive BTFon from a broadband connection in the town over a high-gain antenna and redistributing it from a WiFi AP. The trouble is I am desperately short of power – every extra piece of kit means more frequent battery changing. In the end I went with a more powerful MiFi access point – one that supported an external WiFi aerial. I used a 9dB TP-Link patch antenna

This feed the farm from one edge, as it happens the antenna is furthest away from the most likely camera sites but slightly higher than the target sites. The signal pattern fans out quite well, serving the likely points of interest. I was chuffed with the performance of the aerial – it gives the right balance of directionality, as I don’t need to bother to serve the field behind me, but it gives very useful gain in the wanted direction – I can just about get a wifi connection with the internal antenna of my iPod touch from the opposite corner of the field. As the forest garden and some of the windbreak trees grow I may experience problems, but that is for another day. By then perhaps we have a site with mains power 🙂

For the MiFi unit I used a TP-Link MR3220 – it’s surprisingly hard to find a MiFi box with an external WiFi aerial socket, because not unreasonably they anticipate you using this sort of thing as a personal cloud. I had to live with the 9V powering and used a Chinese Ebay 12V to 9V converter switchmode converter to efficiently turn the 12V battery power to 9VDC

The other part of improving range is to upgrade the camera end with a WiFi card with an external rather than internal antenna; since the Pice case has been withdrawn I need another solution for that. The PICE case also still exposes the Raspberry Pi camera lens to the elements which is Not a Good Thing leading to the lens haziness problem.

It doesn’t really matter how big the camera is, so I took the opportunity of using a much larger box – a  Hammond case 1599 to fit it all in.

If you’re going to put something outside then the fewer holes you can drill the better, hence the use of sticky pads and cable ties as mounts, and the single 2.1mm power socket on the base, so water could drain out that way if necessary, and could be standing 0.5cm without affecting the electronics.

The case has several mounting lugs in the lid, but in the end I will have to drill a hole for the camera. I placed an O ring on the case and a microscope slide pressed down by foam and the camera to make a watertight seal but keep the elements out of the lens; that way hopefully I get to either clean or replace the microscope slide after a season is out rather than the camera.

camera mounted in the lid

camera mounted in the lid. Reed switch is top left wit the yellow wires, the 1p glued to the case holds the magnet there because they are made of steel these days not copper.

The 12V to 5V converter is mounted in the case; that way any cable losses aren’t too bad and the current in the supply cable is reduced, it is about 200mA max.

Setting it up in the field

Pig Camera in service

Pig Camera in service

The paving slab is there because the first version of the tripod ended up flat on its back in the morning. At least the construction can survive a fall of 2m. Perhaps the neoprene sunshade and the extra area at the top of the pole presents too much wind loading.

Camera close-up

Camera close-up

Controlling the Pi

I postulated all sorts of complex feedback when first considering this, letting the Pi tell the microcontroller to turn the Pi off with a GPIO pin, but it’s been massively simplified. A microcontroller powers up the Pi, and pulls the power after 5 minutes. Then it waits 10 minutes and does it again, provided that the 12V supply is enough (>11.5V) and it is daylight.

I use similar Python code to the first cut, but this time I start running the takepic.py camera code on startup. I look for a switch closed on the GPIO, and if so, abort uploading the picture because the Pi is in service mode 1. This switch is a reed switch mounted on the inside of the case and activated by sticking a magnet to it, this saves a hole. It lets me get onto the Pi and configure it. Normally the switch is open, in which case the Pi tells the system to do a shutdown in 4 minutes, which is enough to connect, get Wifi network DHCP and SFTP the picture to the website. 5 minutes after powerup, the microcontroller managing power pulls the power from the Pi.

The shutdown command on the Pi minimizes the chance of corrupting the SD card, and the picture is written to the /run/shm/ ramdisk prior to uploading, since there is no point using up SD card write cycles with ephemeral data like that.

#!/usr/bin/python
#$Id: takepic.py 58 2014-11-06 20:54:02Z ermine $
import time
import picamera
import paramiko
import os
import socket
import datetime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # USE Pi BOARD pins, not the BCM ver
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP) # 7 is next to gnd on pin 9, so set pull up

# defs
camerafail=False;
DIR='/run/shm/'
imagename=socket.gethostname()+'.jpg'
remotename='WEBSITE.COM' # assuming this is reachable by ssh and www
try :
    with picamera.PiCamera() as camera:
        #camera.resolution = (2592, 1944)
        # The following is equivalent
        #camera.resolution = camera.MAX_IMAGE_RESOLUTION
        # run half res to test out connectivity etc and save money
        #camera.led = False
        camera.resolution = camera.MAX_IMAGE_RESOLUTION
        #camera.resolution = (1296, 972) # do half real to eliminate Bayer softness and save TX bandwidth
        camera.exposure_mode='night'
        camera.meter_mode='matrix'
        camera.start_preview()
        time.sleep(2)
        camera.capture(DIR+imagename, resize=(1296,972), format='jpeg', quality=20)
except picamera.PiCameraError,e :
    print e
    camerafail=True
finally :
        camera.close()
time.sleep(10) # hopefully nw is up by now 
if(GPIO.input(7) ==1):
    #print "will shutdown"
    os.system("/usr/bin/sudo /sbin/shutdown -h +4 &")

    if not(camerafail) :


        timedout=False
        connected=False
        counter=0

        while (not timedout) and not connected :
            try :
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.connect((remotename,80))
                print(s.getsockname()[0])
                connected=True
            except socket.error,e :
                
                counter += 1
                print counter
            finally: 
                s.close()
            time.sleep(5)
            if counter >= 5:
                timedout=True
                print 'Failed to connect to ',remotename,' ',datetime.datetime.now().strftime("%y/%m/%d %H:%M")
        
        

         
        #upload
        if not timedout: 
            print 'ftp image starting ',datetime.datetime.now().strftime("%y/%m/%d %H:%M")
            try :
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(remotename, port=2222, username='USERNAME', password='PASSWORD')
                sftp = ssh.open_sftp();
                sftp.put(DIR+imagename, '/home/DIR/'+imagename)
                sftp.close()
                print "closed SFTP"
            except paramiko.AuthenticationException,e :
                print e
            except socket.error,e :
                print e
else :
    print "manually aborted by jumper 7 to 9"

Power savings

This has massively reduced the power drain of the camera – it is < 200mA for a third of the time, with an outage during the night of about 1/3 of the time, so about 1/3 × 2/3 × 200mA average, ie ~50mA. The original power drain was about 300mA 24×7.This power drain is much less than an electric fence which is usually about 150mA, so it could be used from the fence battery, which would then let us monitor the fence battery voltage as a bonus.

It also suits solar panel charging well, as the power drain is proportional to day length. The WiFi node draws more (a sustained 200mA during the day) but at least it is just one place where the battery needs changing more often – about once every two weeks. That’s livable with, but if I’d used a WiFi long-distance connect and a WiFi high power AP that would be shortened too much, particularly as logging into BT Fon would require another Pi to keep the connection open. GiffGaff run about £7.50 per Gb PAYG which isn’t bad.

Got pigs

Got pigs

Got pigs, end of the day picture in lowish light, 4:30 pm November

So far so good. The new pigcam

  • works all over the likely sites on the farm
  • concentrates the data through one GiffGaff SIM 2
  • reduces power at camera sites to minimal
  • lets me add more than one camera to the system

which is a success compared to the single site version which had a very high power drain because it wasn’t being power-managed.

Pigs

Pigs, photo taken with a regular camera earlier in the day

Notes:

  1. In service mode I get to ssh into the Pi to configure it, and issue the shutdown command manually – I bypass the microcontroller shutdown for that
  2. data service gets cheaper with volume, so this is much better than each camera having a SIM

Remote Farm camera – experimenting with WiFi

Clive from the Raspberry Pi Foundation very kindly let us have a Pi Model A and camera, so it’s time to take this project forward a stage. At the moment the alpha test version is in service keeping an eye on our new piglets. Although the original purpose was to keep an eye on our newly arrived cattle pigs are more enterprising, so it’s good to know where they are

Our camera on the pigs

Our camera on the pigs

Development was stalled however since it’s not easy to work on a piece of kit in service.  Ideally one MiFi box could serve a number of cameras. This would reduce the camera power drain and consolidate.

The trouble is that the site is on the edge of town, a couple of hundred metres from the nearest houses, and I want to use BT Wi-Fi

Yagi wifi antenna

Yagi wifi antenna

I acquired one of these from ebay I didn’t have much hope for the integrity of the device, and to gain height I needed to go 250m further way from any likely source of WiFi. But it worked a treat, as a WISP client, I’m amazed. I was able to see 18 access points, five of which were BT WiFi , 3 of which were 5dB or above. The TP-Link gives a decent link at more than 5 and nothing at 2. I streamed a video to confirm the stability of the link.

TP Link signal strength with yagi

TP Link signal strength with yagi

I use it with a TP-Link TL-WA5110G which has the advantage of having a sma antenna socket and a native power supply of 12V. Although the adaptor is specced at 1A I measured the power drain at about 210 mA (~2.5W @12V). Which isn’t great but not terrible

A WISP client is not a  repeater

The trouble with using the tP link as a WISP client is the signal is presented as Ethernet, and I’d then want to re-radiate the signal on a different Wi-Fi network.  I could set it as a repeater, but then every Raspberry Pi would have the hurt of logging into BT WiFi. Whereas if I use a Raspberry Pi connected via ethernet to the TP-link and a WiPi dongle to connect to the farm I can collect pictures from the cameras on the farm network and perhaps power-manage the TP Link to only update every so often.

So an unexpected win, and I can look at using a Model B as a concentrator

Remote Farm camera using Raspberry Pi – alpha test version

At The Oak Tree we have acquired two bullocks/steers, and it would be good to be able to monitor these guys to check they are okay.

cattle

cattle

The obvious way is a GSM trail camera, which has got some merit. However, these are typically tripped by movement, and you get a lot of that outside. It might work to use a trail camera with a 15 minute trigger lockout – it’d be tripped all the time but wouldn’t spam you senseless with the 15 min delay. But it’s going to be dear to run as they tend to use MMS – giffgaff charge 16p per MMS for instance, whereas their data rates are 20p per 20Mb . CCTV solutions have the same problem – they are event triggered, whereas we want to be able to see a picture taken sometime in the last 15 minutes. Well, obviously we’d really like realtime streaming video by a pic at most 15 minutes old is a decent second best 🙂

What we ideally want is a camera you can send a message to to take a picture.

I used to have a Youthnet V900 which you could send an SMS to take a picture, but it was the most maddening hunk of junk I have ever purchased. Configuring the settings was done by SMS, and I only ever got it to work with Virgin mobile once. Then Virgin must have changed something and I never got this to work again, either with Virgin or with Giffgaff. In the end I put a hammer through it so it wouldn’t waste any more of my time and money. The satisfaction of doing that was the most value I got out of it.

I took a look at this trail cam and recognised the same commands as the Youthnet . I’m just not going to go there again if it uses the same software. Plus there’s the fact that it doesn’t really suit what I want it to do.

The Pi’s USB subsystem isn’t good enough to run a 3g dongle

So it’s time to break out the Raspberry Pi. Unfortunately the USB subsystem of the Pi means I couldn’t use Gammu directly with a Huawei E156G USB 3G dongle. It would work for a while and then conk out, and only come good again when the Pi was power cycled, not even a restart would fix it. That’s a real shame – this would save me a lot of power drain. Maybe it’s specific to this dongle, but there is always a world of hurt when it comes to the Pi and USB because of the power problem, and a 3G dongle has a high power usage when connected. Even running the E156G off an approved USB separately powered hub and the Pi off an approved PSU didn’t make it any better.

Go MiFi

Obviously if your site has WiFi you don’t need to wrangle with 3G, but there is a lively market in boxes to put a WiFi hotspot using 3G, called MiFi devices. You need to configure these to make use of your 3G dongle (and make sure they are compatible), the type I used is a TP-Link MR-3220

1501_TL-MR3020-V1-03

So I used a MICRONEXT  MN-WD552B WiFi dongle in the A’s single USB port, which seems reliable. Of course I now eat the power drain of the Pi, WiFi is never low power and the TP-Link 3020 MiFi device gives me another power hit. But it works, and I needed results fast rather than good, so it’ll do for now.

The Raspberry Pi Camera module is really rather excellent

The camera module that the Pi foundation sell for of the Pi is rather good – the larger version of the pic above is already scaled down by half in the camera to reduce the file size. And I compressed it a fair bit. You can control the camera in Python too, using Picamera. Python seems to be the default language for the RPi.

Although the Raspberry Pi is credit card sized, it’s remarkable how big it gets if you want to do anything with it. I used a box I already had – if I decide this works well enough for a permanent version then I will use a PICE waterproof case, but for the prototype a temporary rig is fine.

the Pi gets big fast when you hang enough onto it to make it useful

the Pi gets big fast when you hang enough onto it to make it useful

At the moment both are powered up all the time, and a cron job takes a picture and SFTPs it up every 15 minutes during the day. The Pi and the TP3020 are powered via one of those dual-output 12V car to USB converters, the power drain at 12V is 250mA which is horrible, meaning using 40Ah of a 80Ah leisure battery will take 40*4=160 hours, nearly a week. Which is nasty, but livable with. There is massive opportunity to cut the power drain by bringing up the TP-Link 30210 and the Pi only once every 15 minutes when it’s light – they can be up and do their stuff in 5 minutes so I’d get a 3x drop in power drain from that, and not running in the night would save me another third.

The installation is bush-league, but this is going to need a bit of optimisation anyway

the camera is at the top left of this IBC

the camera is at the top left of this IBC

and a close-up shows just how bad it is – waterproofing by electrical tape is living on borrowed time. But that’s where those nice people at PICE come in, so there’s no point in putting any real work in that.

best not let the IEE see that else they'll revoke C.Eng for disgraceful and outrageous bodgery of the first order ;)

disgraceful and outrageous bodgery of the first order 😉 The PICE case has solved most of the mechanical challenges kludged together here.

This shows one of the hazards of this location, in that the crows sit on the bar surveying their domain and crap on the IBC. There’s no crap on the bars though, so I might get away with it. Some anti-bird spikes might be in order.

 #!/usr/bin/python
#$Id: takepic.py 53 2014-06-26 16:30:17Z richard $
import time
import picamera
import paramiko
import os
import socket
import datetime
# import RPi.GPIO as GPIO

# GPIO.setmode(GPIO.BOARD) # USE Pi BOARD pins, not the BCM ver
# GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP) # 7 is next to gnd on pin 9, so set pull up

# defs
camerafail=False;
DIR='/home/pi/mine/'
imagename='cam01.jpg'
remotename='mysite.com' # assuming this is reachable by ssh and www



try :
    with picamera.PiCamera() as camera:
        #camera.resolution = (2592, 1944)
        # The following is equivalent
        #camera.resolution = camera.MAX_IMAGE_RESOLUTION
        # run half res to test out connectivity etc and save money
        #camera.led = False
        camera.resolution = camera.MAX_IMAGE_RESOLUTION
        #camera.resolution = (1296, 972) # do half real to eliminate Bayer softness and save TX bandwidth
        camera.start_preview()
        time.sleep(2)
        camera.capture(DIR+imagename, resize=(800,600), format='jpeg', quality=15)
except picamera.PiCameraError,e :
    print e
    camerafail=True
finally :
        camera.close()
time.sleep(10) # hopefully nw is up by now   

if not(camerafail) :


    timedout=False
    connected=False
    counter=0

    while (not timedout) and not connected :
        try :
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect((remotename,80))
            print(s.getsockname()[0])
            connected=True
        except socket.error,e :
            
            counter += 1
            print counter
        finally: 
            s.close()
        time.sleep(5)
        if counter >= 5:
            timedout=True
            print 'Failed to connect to ',remotename,' ',datetime.datetime.now().strftime("%y/%m/%d %H:%M")
    
    # Ideally, this gets called by cron once every quarter of an hour. 

     
    #upload
    if not timedout: 
        print 'ftp image starting ',datetime.datetime.now().strftime("%y/%m/%d %H:%M")
        try :
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(remotename, port=2222, username='username', password='password')
            sftp = ssh.open_sftp();
            sftp.put(DIR+imagename, '/my/remote/path/'+imagename)
            sftp.close()
            print "closed SFTP"
        except paramiko.AuthenticationException,e :
            print e
        except socket.error,e :
            print e

The cron job

This is then called by a cron job on the Raspberry Pi. You must to this with root privileges because you are accessing the hardware, so use

sudo crontab -e

and mine looks like

*/15 4-22 * * * /home/pi/takepic.py > /home/pi/log.txt

which means every 15 minutes between 4am and 10pm run takepic.py and save the error messages etc to log.txt. Before running this as a cron job you want to make sure that if you manually run

sudo /home/pi/takepic.py

you get a picture uploaded to where you want it 🙂

Goose Incubator Camera

Getting a camera in an incubator has a lot of the aah factor and lets you see what’s going on without opening the incubator up all the time, which is good for the incubator, the eggs and the reliability of hatching.

You have to take into account that the incubator is a fairly harsh environment. There are lots of ways of making an incubator camera in principle, but  the heat and humidity rule out a lot of them, like webcams and trail cams.

Five requirements of a Incubator camera

  • sealed against moisture
  • elevated temperature running
  • physically small
  • capable of shortening the close focus to ~ 5cm
  • externally powered

I used an outdoor bullet camera I got from RF Concepts – it’s an old variant of this one.

camera

camera

It used to be on top of a fence post looking at my bird feeder, and stood the test of time against the rain. I removed the square part on the front and tried replacing the lens with a wide angle lens, which stuck out too much and cracked the glass.Fortunately replacement mineral watch crystals are to be had – a 24.8mm was a suiable replacement. Replacing the original lens and unscrewing a bit to get the short focus closer worked a treat, and the replacement glass and O rings have held so far.

A single channel Vivotek 8100 CCTV webserver turns the analogue CCTV signal into a IP-compatible one and thei nbuilt FTP client uploads a picture every so often to make it publically viewable. The 8100 could also stream video but I don’t have a good enough connection for that.

Goose hatching camera (inactive most of the time!)

Real IP CCTV cameras are still on the expensive side and a little bit too big for this job.

It’s Goose incubation time again

Goose eggs – pure aggression in a small package. Apparently geese spew the eggs out, about 30 per goose per year, then collect them all up and sit on ’em. Last year we incubated some eggs, and because geese are waterfowl, getting the humidity right is key to improving the hit-rate.

look out, here comes trouble

look out, here comes trouble

Last year I used Xively for the display, but the Cosm/Xively TCP stack is ratty. Certainly for an Arduino, which has only just enough horsepower to run a TCP/IP stack, but the Xively linux library wasn’t that reliable on the Raspberry Pi that I now use as a concentrator.

goose eggs

goose eggs

Incubator temperature

I now use RRDtool, which can run on the Raspberry Pi,

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install rrdtool

and there is a Python library that can inject data into the RRD databases called python-rrdtool

I really didn’t get on with python-rrdtool, I invoke RRDtool explicity

getstatusoutput("/usr/bin/rrdtool update "+DIR+"AB N:1234")

where DIR is the full directory path

 

 

 

 

Humidity and Temperature sensor on Cosm IoT

The trouble with geese is that they are waterfowl, ie used to hatching near water, unlike chickens, so you tend to have to control the humidity of the incubator as well as the temperature. Humidity is terribly counterintuitive – unlike temperature humans have no real sense for it. So I was looking at how to make a humidity and temperature sensor.

Goslings

Goslings. Like most animals, geese don’t look so mean when young

The problem starts with our cheap Chinese import incubator. There’s no real calibration on the temperature dial, and you know it’s a bad sign when the manual tells you ‘when your small poultry is ready’. I was really sceptical about how well this would stabilise the temperature. Quite unfairly as it happens. Continue reading