Raspberry Pi – Networking Script Change

Made some modifications to my script – made a function for writing to the file, which saved a lot of reused code, and instead of assuming that it could ping Google if the result wasn’t ‘unknown host www.google.co.uk’ – changed it to detect the words ‘ bytes from’ – from ’64 bytes from x’.

Find the code at: http://www.matthewrobbins.co.uk/files/connectiontest.py

Next up – try and make a script that detects if mjpg streamer is actually broadcasting video – if not, restart the service (this can fix issues that arise if the webcam is plugged in after boot)

Matt

On the BBC News! – Raspberry Pi Cambridge Event

A few weeks ago, I shot up to Cambridge to help out with a Raspberry Pi event at Chesterton Community College where Raspberry Pi founder Eben Upton took great pleasure in announcing that they would be providing 15,000 Raspberry Pi kits to school children in the UK, through a grant from Google.

Me and a Pi

Chatting to a group of primary school children about what the Pi is.

I think this is a fantastic initiative, and the RPi is the perfect, standard, versatile and powerful bit of kit to get people learning real computer modules – from Java and Python programming, use of Unix operating systems and their services, to lower level component control with the GPIO pins and SPI and I2C interfaces. I really do hope that schools adopt the Pi, and get students introduced to programming and real computer science so that they can become interested in the subject area if they wish, and start pursuing cool projects.

Matt

Raspberry Pi Endeavors 4 – Hardware

A week or so ago, I decided to my own version of Google Glass by selotaping the Pi, webcam and Nick’s battery pack to my hat. Here is the result:

Glass Hat

I also received my Mr.Basic 4wd chassis, which I discovered was faulty, after I had built it.

The kit:

Basic Kit

Partially built:

Half built

Built with motors:

Built Mr Basic

It looks like a really promising design, so I have ordered another one from Bizoner in China, they seem quite hard to get hold of around here. Also ordered a slightly larger one for Nick. Pictures to be posted when they arrive!

We also grabbed a couple of RC cars from Argos, and I proceeded to take mine apart to discover what kind of voltage the motors were using etc:

RC Car being held by third hands, being measured by a multimeter

Matt

Raspberry Pi Endeavors 3 – Battery and Other Components

My networking script has only restarted my networking adapter once since I got my new Edimax adapter, the log shows (I save the print statements to a text file now) it tried to ping Google twice, couldn’t, restarted the networking service, then was fine.

Networking restart script showing 1 restart

I also have my webcam displaying a live stream to a website, along with Nick’s too:

The webcams output on a webpage

The webcams output on a webpage

I have also got my Pi running on a mobile 12000mAh phone portable charger, which seems to power it for about 8 hours on its first run. The charger has a 1 and 2 amp output which is useful, and it charged by its own micro USB cable – the same one that I connect to the Pi to power it.

Mobile phone battery charger

Mobile phone battery charger

Also got some superbright LEDs – blue and white – for when our 4wd chassis’ eventually come, as well as some smaller breadboards and some resistors.

Just got an email this morning that Bizoner.com has finally processed our order of 4wd chassis’ so hopefully they will come next week.

Matt

Remote Control Car Powered By Raspberry Pi Project – Raspberry Pi Endeavors

Update on the networking issues: I made a log of whenever my networking service had to be restarted, and it was very random – sometimes 12 times an hour, sometimes never in a night. In the end I got a new (not ancient) Edimax WiFi adapter, and this has been working great so far. I can also recommend this £10 Edimax one too. I also got a Logitech C910 webcam off eBay for £45 off eBay, which is much better than the ancient Microsoft one that I was using.

Pi with USB WiFi antenna, battery pack and webcam

Pi with USB WiFi antenna, battery pack and webcam

After buying some components off Maplin and Ebay, me and my housemate Nick have been able to light up LEDs and spin motors connected to a breadboard via first a header extension cable connected to some of the Pi’s GPIO pins, and then using the Adafruit Raspberry Pi Cobler – which transports all the pins functionality to a breadboard where you can start adding resistors, wires and LEDs easily.

We have been using ‘motion’ for Linux to export our webcam stream, however it wasn’t all that happy with higher resolution webcams, and froze very often for me, especially at night. Nick endured slightly better luck with a lower quality, older, Microsoft webcam, which froze less. We were prepared to settle with Motion, but then found that mjpg-streamer, that I had previously used at work to look at the current snowfall out of the window, provided a MUCH better quality, framerate, and hasn’t froze once yet.

I have made it start on boot by adding a new cron job that runs a shell scipt that contains the command to start mjpg streamer, and an ampersand to run it in the background:

Cron jon: @reboot /home/pi/Documents/code/mjpg-start.sh

mjpg-start.sh:

/home/pi/Documents/mjpg-streamer/mjpg-streamer/mjpg_streamer -i “/home/pi/Documents/mjpg-streamer/mjpg-streamer/input_uvc.so -n -f 25 -r 320×240″ -o “/home/pi/Documents/mjpg-streamer/mjpg-streamer/output_http.so -n -w “/home/pi/Documents/mjpg-streamer/mjpg-streamer/www” -p 8082″&

The mjpg-start command uses a definable resolution size, but be warned this is of set sizes, and an output port too – I have used 8082.

I followed this guide to get mjpg-streamer set up, be sure to run ‘make’ as sudo!

To get LEDs lighting up, I had a look at the Adafruit learning system, who have some brilliant guides from how to prepare and SD card with a Raspberry Pi image to how to make LEDs light up when you have a new Gmail message, to how to control a motor. Follow these great guides, grab a Raspberry Pi cobbler and breadboard and break out the old electronics and maths textbooks – be sure not to fry your Pi by putting too much power through it – be careful with the GPIO pins.

Happy Pi-ing!

Matt

Raspberry Pi Endeavors – Networking restart script

After popping to Maplin today to buy some electrical components to connect up to my Pi, I decided to write a script to reconnect my Pi to the internet if its WiFi connection fails, which it seems to do fairly regularly, and I haven’t nailed down completely whats causing the USB Belkin adapter to stop working.

Basically, my script pings Google, if it gets a response – great. If not, try once more, and if no luck, restart the networking service. If still no luck, pull down and put back up again the wlan0 (wireless lan) adapter.

Script is below if you wish to nick it. Also I modified my /etc/resolf.conf to match the one on Adafruit’s Pi learning site, whose tutorials I am following.

Matt

Code [click here to download the properly indented version]:

#!/usr/bin/python
import commands

def can_ping_google():
ping_once = “ping www.google.co.uk -c 1″
output = commands.getstatusoutput(ping_once)
if output[1] == ‘ping: unknown host www.google.co.uk’:
print “Ping failed”
return False
else:
return True

def connection_test():
if can_ping_google():
print “Pinged fine”
return
else:
if can_ping_google():
print “Pinged fine second time, first was a fail”
return
else:
print “Restarting networking service”
restart_output = commands.getstatusoutput(“sudo service networking restart”)
print restart_output
if can_ping_google():
print “Restarting worked, can ping now”
return
else:
print “Restarting didn’t work. Will force wlan0 down and up, and restart networking after”
print commands.getstatusoutput(“sudo ifdown –force wlan0″)
print commands.getstatusoutput(“sudo ifup wlan0″)
print commands.getstatusoutput(“sudo service networking restart”)
if can_ping_google():
print “Down, up and restart worked. Can ping.”
return
else:
print “Couldn’t ping Google after up, down and restarting.”

connection_test()