Monday 4 November 2013

A Raspberry Pi Security Camera
2013-11-04

I'd been looking for uses for my raspberry pi, and it finally hit me:  a security camera!

Given I already had the pi, and a charger, all else I needed was a webcam.  For this, I chose the Nexxtech HD Webcam; it's a decent little unit, with a 1280x720 CMOS sensor and plenty of low-level light pickup, and a relative bargain for $29 at The Source.

I rigged up a case for the pi from an empty cigarette pack; the new 25s are perfect for this.  With that, I just had to rig a little shelf, in the window beside the door, on top of which went the webcam, and away I went.  Perhaps the most technically challenging moment came in figuring out a way to run power to the downstairs entrance!

Now, you know me; I don't do things the easy way; I do them the efficient way.  I didn't want some GUI setup to worry about; everything would have to be done from the command line.  With that in mind...

The preliminaries:  I had to install a few things that weren't in the default Raspbian setup:
- Apache2
- fswebcam
- ImageMagick (just in case)
- Possibly others

The capture portion was easy enough to set up, with the proviso that the brightness be adjusted for day and night.  The image should be in two sizes, and should be saved to /var/www.  I put it into a shell script:

nano /home/pi/webcam.sh

#!/bin/bash
time=$(date "+%H%M")
if ((630 < 10#$time && 10#$time < 1700)); then 
    fswebcam -D 1 -f 2 --set brightness=20% -r 1280x720 --jpeg 95 /var/www/door.jpg --scale 320x180 --jpeg 95 /var/www/door-sm.jpg
else
    fswebcam -D 1 -f 2 --set brightness=100% -r 1280x720 --jpeg 95 /var/www/door.jpg --scale 320x180 --jpeg 95 /var/www/door-sm.jpg
fi

chmod +x /home/pi/webcam.sh


I wanted to capture four frames per minute from the camera (the relevant modules were already installed in Raspbian).

The other issue was the with the WiFi dongle setup, the pi was unstable; so I'd throw in a reboot every hour; it takes just seconds.  The easiest way to do all this was through cron jobs, as follows:

crontab -e

# m h   dom mon dow          command
0 * * * *                             /sbin/reboot
* * * * *                             /home/pi/webcam.sh
* * * * *                             sleep 15; /home/pi/webcam.sh
* * * * *                             sleep 30; /home/pi/webcam.sh
* * * * *                             sleep 45; /home/pi/webcam.sh


And that, literally, is all I needed to do.

The rig handles day- and nighttime illumination levels very well; attached is a jpeg.  I access it locally through the network at http://pi/door.jpg.  Works like a charm!



Monday 2 September 2013

Records
2013-09-02

Seems the all-time records are getting screwed up.

It's not a tough one to catch; everything happens in just two routines.  Problem is, everything looks okay to me.  Every programmer goes through this eventually and, after much pulling of hair and gnashing of teeth, fixes it.  I will, too - it's just a matter of seeing how the code performs in real life.

I'm really pleased with the rest of the system; it's running really well, now, which is why I've been dragging my heels on fixing it.

All in good time.

-Bill

Tuesday 13 August 2013

24/7!
2013-08-13

The weather system is now operating 24/7 with no pulldowns during the day.

For the longest time, it was crapping out after about four hours, with a 'too many open files' error.   I finally got that sorted out, and away she goes!

-Bill

Sunday 2 June 2013

Beaten!

(2013-06-02)

Solved.

Turns out I had forked the build on the stats-capture program, and been careless with my code.  I hang my head in shame.

Not only is it now delivering forecasts acceptably, the forecast is also now appearing on the main page!

Everything's ticking along at about 95%.

-Bill

Sunday 19 May 2013

Compiler Bug
2013-05-19

My weather system has had difficulty in recent days with delivering regional stats.  The capture program was to read through a list of .ini data and process accordingly.  Unfortunately, it was running about five iterations and then crapping out.

Turns out it was a compiler bug.


At times, it just worked; at other times, it wouldn't work at all.  The compiler itself was generating bad code.


In the end, I unravelled the loop, so to speak:  the program now accepts command-line parameters and iterates just once for each location.  It works!


Now I can go back to figuring out why the forecast isn't appearing for CTO.


-Bill


Friday 12 April 2013

Chewin' Gum and Bailin' Twine
2013-04-12


My upgraded weather system has gone live.  It took some work to get there.

To summarize, my new home weather station includes a wireless LCD display, like the old one, but with more data fields (wind speed and direction, and precipitation).  Second, our cats frequently knock the data-capture camera out of position, forcing time-consuming recalibration.  The new version of the capture software makes it much easier to realign onscreen elements within the GUI environment.

It took trial, error, and plenty of arithmetic, but I'm getting the capture program fine-tuned, and the data are looking fairly reliable.  I do have to rejig the Humidex and Wind Chill calculation routines.  No biggie.  In the process, I've learned a lot about image processing.  For example, the webcam delivers a colour image, and I find that the green channel is the sharpest; so I use only the green values from the image.

The optical part of the data-capture process was the most time-consuming part.  I completely revamped how the system processes and validates the data from the LCD.  It now compares the average brightness for each character-segment against the brightness of a pre-selected control area for that character.  An adjustable darkness threshold allows tuning between false-negatives and false-positives.  One field is a differential value (current reading minus last reading), and another is a comparitive (select only the darkest of an array of segments).

The sky-observing software now outputs its results directly to the data-capture program, for integration into the data feed to the weather system itself.  Minutes of sunshine are imputed and recorded.

I've been tinkering with the weather software, to ensure it's properly recording and reporting the readings given it.

Looks good, so far.

-Bill

Wednesday 10 April 2013

My Babies
2013-04-10

I love cats.  They're fun, bright, cuddly and mostly self-maintaining.

Teeka is a sphynx cat--she's hairless and a bit on the smallish side.  Elroy just joined the family a few weeks ago.  He, too, is a sphynx, but full-sized.  He's a total lovebug, and the two have hit it off famously.

Here's a shot:


Elroy (pink; foreground) and Teeka
Sphynxes are extra-bright and engaging; there's always one hanging out or looking for lap space, or play... or treats... or nip--or, most importantly, warmth!

Teeka--the little blue-gray one in the pic--loves to play, either with toys, or one-on-one.  At random, she'll run up and start roughing-up my leg, making nasty-faces and generally looking for trouble.  That's my cue to jump up, raise my arms, roar like a monster and give chase.  She flees up the stairs, then comes charging back once I turn around.  In her previous home, she'd spent entire winters huddled up to the impotent baseboard heaters.  In summer, she's a total sun slut.  She baked away last summer, on the balcony, while I looked for work.  Now she's got spring fever and has taken to charging defiantly out onto the front step for a sniff and a shiver, when someone comes home.

Both like their spankings--it's a peculiarity of the breed.  Teeka's favourite spanking pose is left-cheek-up, clinging to the end of the arm of the chesterfield; for Elroy, it's on the bed.  Flop-"'Pank me!"

Because their faces are bare, Sphynxes broadcast their emotions more readily.  I know at a glance what kind of mood Teeka's in, and usually what she wants.  She gives 'the look' better than Mother; and when she's agitating for a tussle, her little Shatner-aping grimace telegraphs "Khaaaaaaaan!".  And, boy, smiles are special!

This post is mostly for the benefit of my friend Natasha, who's breaking into Northern life on her own and whose three little doggies are still waiting to join Mommy up North.  I know this picture will bring her a smile.

Have a good week.

-Bill

Tuesday 9 April 2013

C'mawn, April Showers!
2013-04-09


No, I'm not pining for a porn actress.  I'm waiting patiently for it to rain.

New Toys
About a month back, I ordered a new weather station, which arrived about ten days ago; the dr.Tech WA-1070-T.  It's wireless.  It includes wind and precipitation.  It includes a solar recharger.

One unexpected omission from the package:  USB capture.  Dammit.  Chumped by an assumption on my part.

So, I'll be back to OCR'ing an LCD display.


Back to the Fixture

(If that statement mystifies you: I capture my 'local' weather data from the LCD display of a cheap home weather station.  A webcam snaps a picture of the display, and custom software quite literally interprets what's on the screen and passes the info along to the weather software proper.  Versus wiring in and programming the necessary circuitry to interface it electronically, it's the best/simplest/cheapest solution.  And I'm damned certain I'm infringing on some troll's patent, somewhere, in doing this.)

I've had the new station installed and in testing for about a week.  Since then, it's been bloody windy, causing me to suspect vibration of the 'transmitter' unit--not the rain gauge mechanism itself--as the source of spurious readings.  I'm supposed to get precipitation data in increments of 0.3 mm.  What I've been getting is a string of spurious readings, 15.2 mm at a time--and this after having secured the rain gauge to a flat, stable surface.  What I'm suspecting is a loose connection, in conjunction with resonant vibration in the sensor mast.

Making Ready
I've been upgrading the data-capture software, with two competing goals in mind:

Make it capture more data

Make it easier to use (GUI-squishy) and more configurable

Issue:  There'll be a number of new fields on the new display (wind speed and direction, and running-total precipitation).  The number of fields actually captured may vary and could change again in the future, when I try to upgrade to something even-better and chump myself again.  Honest-to-'Ghettimonster, I will be absolutely certain, next time, that it's got computer connectivity.  *makes coiling motions with left hand*

So, make the data fields configurable and more flexible

Issue:  We have a new, active, male feline addition to the households.  Prowling occurs, as do minor upsets, such as trying to perch on the Centretown Observatory Data Capture Stage, which began life as a microwave cart with a nordic moniker.  Disturbances to the webcam and/or the display panel (position and orientation) are a major pain, as viewpoints change, and the camera must be realigned simultaneously with the data-capture software. 

So, make it easier and faster to realign.

The revised version of the capture software now recognizes any number of user-definable data fields, in which the individual digits can be expressed as a box and the expected segments scanned automatically; or each individual segment can be defined to specific coordinates.  (E.g. on the new display, the leading digit for the barometric-pressure reading is either blank, or 1 (i.e. two segments.).)

The configuration file is so simple, yet flexible, it really deserves a full page of documentation.  The initial calibration is done by hand, field-by-field and character-by-character, and entered into the config file.  Following that, realignment is easier.  Once the camera has been levelled and roughly pointed in the right direction, the data search-boxes can be adjusted onscreen either globally, or by field.  A future version may use special markings for auto-calibration.Eventually, I'll add interface elements to manage configuration completely visually.

So, a few polishing touches to the data-capture software, and a one-time calibration, and I'll be collecting live data from the new station.  The only catch is that I need to see a little live action from the rain gauge; to see how real data are reported, before deciding how--or, indeed, whether--to handle rainfall readings.

Water, Water, Everywhere--And Not a Drop to Measure

It hasn't rained much, nor at the right times, since I set up the new equipment.  

It's supposed to rain tonight.  I've been following the front in on local radar.  It's... in the neighbourhood but kind-of regrouping.

To paraphrase The Simpsons' Apu Nahasapeemapetilon (big fan; love the blithe fatalism): 


Rain, rain
Go away
...and please come again!


(And that brings us to 410.4 millimetres of imaginary rain for today.)



Saturday 23 March 2013

Configuring WiFi on Raspberry Pi

Edimax WiFi Dongle

Static IP Address

No Encryption

2013-03-23


My Raspberry Pi, a tiny, ARM-based, single-board computer, has arrived.

Following a test boot and preliminary configuration, I needed to get it running on WiFi.  For this, I had an Edimax EW-7811Un Wireless-b/g/n USB dongle--very common among Pi owners.

Getting it going wasn't easy.  All the references online talk about WPA/WPA2 configuration.  Well, I don't use encryption, due to the menagerie of devices (of various ages and levels of technological sophistication) we use around here.  Instead, I rely on MAC filtering.  Yes--it's not the most-secure setup, as someone can always clone a MAC address and connect.  Personally, I feel that if they want to go to that much trouble, when there are several open access points on this street alone, they're welcome to some of my bandwidth.  So far, it hasn't been an issue.

Anyway.

After digging through man pages and reading others' comments, I got things set up.  When not using WPA, you needn't mess with wpa_supplicant; only the interfaces file must be configured properly.

1.  Don't worry about kernel modules; the appropriate module is pre-loaded.

2.  Login to your Pi and enter the following, leaving out the comments:
cd /etc/network                # Change Directory
sudo nano interfaces           # Edit file with nano text editor

In the nano editor, enter the following lines:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
wireless-essid your_ssid            # Router's SSID - no quotes
address dotted-IP-address           # IP addr you want for Pi
netmask 255.255.x.x                 # 255.0 or 0.0
network dotted-IP.0                 # i.e. x.x.x.0
broadcast dotted-IP.255             # i.e. x.x.x.255
gateway your-gateway-ip             # Usually your router's IP

If there's a line in there about "wpa", prefix it with '#' to comment it out.

Press Ctrl+O to save, then hit [Enter].
Ctrl+X to exit.

3. Logout.

4. Remove the Ethernet cable (if any) and insert the Edimax dongle.

5. Reboot

It should come up and connect automatically.

Cheers!



Tuesday 19 March 2013

Goodies

2013-03-19

I have new toys on the way:
- A dr.Tech wireless weather station (WA-1070T, very similar to the ubiquitous Fine Offset WH-1080PC, which I had before)--I'll be adding (flawed) wind and precip data to my weather site in the near future; and
- one Raspberry Pi single-board microcomputer.

The Pi runs a version of Debian Linux--my favourite distro.  The weather station, like my first one three years ago, has a wireless receiver with USB computer input.

My plan is to sorta bungie the Pi to the back of the receiver, hook in the USB input, use the pywws scripts to read in the live data, and pipe that to my weather software.  I'll need quickly to write a little conversion program, to change the pywws data to something my system can handle.  I can have that mostly written-up in advance.

The Pi is coming with an AC adapter and WiFi dongle.  This means I can place the receiver in an optimal location, and the Pi will come along for the ride, burdened only by its power cable.  Meh--I can live with that.  I have a portable DVD player, with composite input, that I can use as a sweet little mini-monitor, too.

Heck, as the weather system itself is deployed in Linux, I could move the whole-shootin'-match over to the Pi and let it chug along.

The weather software itself will require some tweaking to handle the live wind and precip data.  I can do that in a few idle hours.

At some point, I'll have to polish- and package-up my software and Open-Source it.

So, to my neighbours:  the instrument mast is coming back!