Ethernet control for my office “traffic lights”

A couple of years ago, I wrote about the “traffic lights” I’d created for my home office (and subsequent “upgrade”). Shortly after that, I updated the solution to use three LEDs (as shown below) but it still wasn’t quite what I needed. Walking to the door to push a button and change the light defeated the object somewhat.  I needed to enable this device with a “web service”!

The solution comes in the form of an Arduino Ethernet Shield, allowing pin control over a standard Ethernet connection.  I didn’t use the official shield though – cheap imports can be found on eBay for around £7.  Following that, I amended my code using the Instructables Arduino Ethernet Shield Tutorial and another Instructables post on controlling an LED over the Internet using an Arduino.

The end result is below (or on Github) – and the Arduino uses DHCP to obtain an IP address (reservations can be used to control which one – or DHCP logs can be analysed) and a simple query string is read to set the light using:

  • http://xxx.xxx.xxx.xxx/?r for red.
  • http://xxx.xxx.xxx.xxx/?a for amber.
  • http://xxx.xxx.xxx.xxx/?g for green.

Anything else fails back to red.  

/*
Red/green LED indicator with pushbutton control
Based on http://www.makeuseof.com/tag/arduino-traffic-light-controller/

USE_GITHUB_USERNAME=mark-wilson

*/

// Setup Ethernet Shield
#include <SPI.h>
#include <Ethernet.h>
boolean reading = false;

// Enter a MAC address and IP address for your controller below.
// The IP details will be dependent on your local network (commented out if DHCP in use):
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// byte ip[] = { 192, 168, 0, 112 }; //Manual setup only
// byte gateway[] = { 192, 168, 0, 1 }; //Manual setup only
// byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

// Pins for coloured LEDs
int red = 3;
int amber = 4;
int green = 5;
int light = 0;

int button = 2; // Pushbutton on pin 2
int buttonValue = 0; // Button defaults to 0 (LOW)

void setup(){
Serial.begin(9600);

// Set up pins with LEDs as output devices and switch for input
// Pins 10,11,12 & 13 are used by the Ethernet Shield
pinMode(red,OUTPUT);
pinMode(amber,OUTPUT);
pinMode(green,OUTPUT);
pinMode(button,INPUT);

// start the Ethernet connection and the server:
Ethernet.begin(mac);
//Ethernet.begin(mac, ip, gateway, subnet); //for manual setup

server.begin();
Serial.println(Ethernet.localIP());
}

void loop(){

// listen for incoming clients, and process qequest.
checkForClient();

// Read the value of the pushbutton switch
buttonValue = digitalRead(button);
if (buttonValue == HIGH){
changeLights();
delay(1000); // Wait 1 second before reading again
}
}

void changeLights(){
// Change the lights based on current value: 0 is not set; 1 is red; 2 is amber; 3 is green
switch (light) {
case 1:
turnLightAmber();
break;
case 2:
turnLightGreen();
break;
case 3:
turnLightRed();
default:
turnLightRed();
}
}

void turnLightGreen(){
// Turn off the red/amber and turn on the green
digitalWrite(red,LOW);
digitalWrite(amber,LOW);
digitalWrite(green,HIGH);
light = 3;
}

void turnLightAmber(){
// Turn off the green/amber and turn on the red
digitalWrite(green,LOW);
digitalWrite(amber,HIGH);
digitalWrite(red,LOW);
light = 2;
}

void turnLightRed(){
// Turn off the green/amber and turn on the red
digitalWrite(green,LOW);
digitalWrite(amber,LOW);
digitalWrite(red,HIGH);
light = 1;
}

void checkForClient(){

EthernetClient client = server.available();

if (client) {

// An http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;

while (client.connected()) {
  if (client.available()) {

    if(!sentHeader){
      // Send a standard http response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println();
      sentHeader = true;
    }

    char c = client.read();

    if(reading && c == ' ') reading = false;
    if(c == '?') reading = true; //found the ?, begin reading the info

    if(reading){
      Serial.print(c);

       switch (c) {
         case 'a':
           turnLightAmber();
           break;
         case 'g':
           turnLightGreen();
           break;
         case 'r':
           turnLightRed();
           break;
       }
    }

    if (c == '\n' && currentLineIsBlank)  break;

    if (c == '\n') {
      currentLineIsBlank = true;
    }else if (c != '\r') {
      currentLineIsBlank = false;
    }

  }
}

delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:

}

}

All it needs now is another service to read my Lync status and call the Arduino accordingly, although, having got this far, I have to admit the form factor is not exactly brilliant and I probably should spend the money on a Busylight or a Blynclight instead so that my Arduino can be repurposed for a new project!

Or, of course, there’s Garry Martin (@GarryMartin)’s beautifully simple approach:

Geeking out at Microsoft Research

A couple of weeks ago, I was invited to join the Microsoft Technical Community Council (MTCC), which is described as “a group of external IT professionals influential in the IT Pro world, who are engaged and interested in sharing their opinions and meet once a month via a Lync call”.  Basically, the Council is an opportunity for Microsoft to gain feedback from IT Pros with real-world experience of implementing Microsoft technologies and for those involved to understand a little more about the road ahead.

After some frantic NDA-signing, I was privileged to join the MTCC for a face-to-face meeting yesterday at Microsoft Research in Cambridge.  Last time I visited Microsoft Research, they were on a different site, on the outskirts of the city, and some of the stuff I heard about, quite frankly, blew my mind.  I was under NDA than (as an MVP) and under NDA again yesterday, so still can’t talk about what was discussed but the Microsoft Research website showcases some of the projects that have made it from the labs into the real world and describes the current areas of research.

We didn’t just learn about Microsoft’s Research operations though – there were other sessions – and the day also gave me a chance for me to meet with some of the people I’ve known for years but sort of lost touch with whilst my work was focused less on Microsoft and more on IT strategy – as well as to connect some faces to names – either from Twitter or, in once case, from my customers!

We also had rather a lot of fun, geeking out with Microsoft .NET Gadgeteer – a former Microsoft Research project described as:

“A rapid prototyping platform for small electronic gadgets and embedded hardware devices. It combines the advantages of object-oriented programming, solderless assembly of electronics using a kit of hardware modules, and quick physical enclosure fabrication using computer-aided design.”

Paul Foster (@PaulFo), whose antics I’ve written about before (on a home-made Surface table, among other things – and on PhotoSynth and Community Games) led us through an exercise more commonly carried out with school children, using Visual Studio with Visual C# Express and .NET Gadgeteer.  Using modular kits we were soon building simple digital cameras, before going on to add LED indicators, current sensors, motion detection, etc. – with a drag and drop design surface and a few lines of C#.  Even though I left it to the guys from Content and Code to crack out the code (I’ll do the infrastructure piece and plug things together!), I would confidently say that even I could have written the code that was required and it’s a very accessible way to get children doing something real with electronics.

Sadly, whilst the software is free, the hardware is a little on the pricey side, with an FEZ Spider starter kit coming in at around £200 (which is almost Lego Mindstorms EV3 money).  Compared with an Arduino and some raw electronics components, that’s quite a lot more money but it should be said that the graphical design surface provided in the Visual Studio IDE is easier to use and the modular electronic components do make the Gadgeteer-compatible kit easier to work with.  So, on balance, where the Arduino is great for “makers”, the Gadgeteer-compatible kit is probably a better solution for teaching kids the basics of controlling components with code.

Either way, it’s a lot of fun – and inspired me to start playing with electronics again… maybe I’ll even let my kids have a go…

Hardware lineup for 2014

For the last few years, I’ve written a post about my “hardware lineup” – the tech I use pretty much every day (2011, 2012, and 2013). This year, Dan Delaney reminded me when he borrowed the idea (and I originally stole it from someone else…) so here’s the belated 2014 line-up…

Car: Volkswagen Tiguan 2.0 TDI Sport

I’m still enjoying my current company car even as it approaches its 2 year anniversary and am actively working to keep the mileage down as I may buy it at the end of the lease. Whilst I might be able to get a deal on a second hand Q7 or Toureg, this was specced up the way I wanted it  including a retractable towbar and I’m more than happy. Verdict 8/10. Hold (tied into a 3-year lease).

Phones: Apple iPhone 4S and Samsung Galaxy S3 Mini

Windows Phone 7.8 was a disappointment and the lack of apps for the Windows Phone platform means I’ve gone back to iOS for my personal phone (second-hand from the SmartfoneStore), although I hope to jailbreak it to get some of the features that are missing for me in iOS 7. Meanwhile, my company iPhone 3GS has been replaced with an Android model (the Samsung Galaxy S3 Mini), which is infuriating in many ways but at least lets me get experience of working with the other dominant mobile platform. (iPhone) Verdict 7/10. Hold – something new is too expensive. (Galaxy Mini) Verdict 5/10. Not mine to sell!

Tablet: Apple iPad 3G 64GB

Apple iPadMy iPad never replaced a laptop as a primary computer but it’s still great as a Kindle, for catching up on social media content, and for casual gaming (read, occasional babysitter and childrens’ amusement on long car journeys). I was disappointed to have to pay to replace it after the screen developed a fault, but there’s no reason to trade up yet, especially since we bought a touch PC for the family (read on). If anything, I might consider a smaller tablet (maybe a Google Nexus 7 or a Tesco Hudl). Verdict 5/10. Hold, although it’s getting old now.

Everyday PC: Fujitsu Lifebook P702 (Intel Core i5 3210M 2.5GHz, 8GB RAM, 320GB hard disk)

This PC is my main computing device and is a small form-factor replacement for the previous Lifebook I used.  I like it, but a BYOC scheme would be more likely to leave me buying a competitor’s PC. Just as well we only have CYOD! Verdict 7/10. Still hoping for a BYOC scheme at work but not holding my breath.

Family PC: Lenovo Flex 15 (Intel Core i5 4200U 1.6GHz, 4GB RAM, 500GB hard disk)

Lenovo Flex 15When it eventually arrived, I set this PC up with Windows 8.1, Office 2013 and an account for everyone in the family.  It’s been a huge hit – the kids love it and I find it really useful to have a PC in the kitchen/family room.  I’m glad I held out for a touch screen – Windows 8 is so much better with Touch – but I should possibly have got something with a bit more memory… Verdict 8/10. A bit underpowered but a good balance between price and form factor.

Netbook: Lenovo S10e (Intel Atom N270 1.6GHz, 2GB RAM, 160GB hard disk)

Lenovo IdeaPad S10Rarely taken out of the drawer – only used when I want to play with Linux (Ubuntu) or upload some new code to the Arduino. Verdict 2/10. Not worth selling, so keep for tech projects.

Digital Cameras: Nikon D700 and Coolpix P7100

Nikon D700Nikon P7100Although I’ve fallen out of love with photography, I’m sure I’ll get back on the wagon some time. A full-frame DSLR is still my favourite format and the D700 will be with me for a while yet. Indeed, it’s more likely that I would buy some new lenses and a flashgun before I replace my camera body.  Newer bodies offer video but I don’t miss that, and the low light performance on the D700 is pretty good. The P7100 continues to function as my carry-everywhere camera (it lives in the car), offering entry-level DSLR levels of control in a small package, although it’s not as responsive as I’d like and I increasingly tolerate using the iPhone instead (poor camera, but always with me). (D700) Verdict 9/10. Hold. (P7100) Verdict 6/10. Hold.

Photography PC: Apple MacBook MB062LL/B (Intel Core 2 Duo T7500 2.2GHz, 4GB RAM, 750GB hard disk)

Apple Macbook White (late 2007)My MacBook is getting old and, although I upgraded to a 750GB disk, I’m struggling with disk space whilst 4GB of RAM is starting to feel a bit light for big Photoshop jobs but new Macs are expensive. Still too expensive to replace, but as long as I’m not doing much photography, this will last a while longer… Verdict 4/10. Hold.

Media: Samsung UE37ES6300 Smart TV

Samsung UE37ES6300Our late-2012 technology purchase, this replaced an aging (c1998) Sony Trinitron 32″ widescreen CRT and Internet-connected television is now an integral part of my family’s media consumption habit with my children watching more iPlayer content than live.  The software is a little “buggy” but it does the job – as a half decent TV it’s more than adequate and I’m thinking of getting a 22″ version for the den (when we build one…) Verdict 9/10. Hold.

Media: Apple Mac Mini MA206LL/A (Intel Core Duo 1.66GHz, 2GB RAM, 120GB hard disk)

(+ iPad, iPhone 4S, various iPods, Altec Lansing iM7 iPod speakers, Samsung UE37ES6300) Apple Mac MiniNo change here since last year and I still haven’t re-ripped my CDs after the NAS failure a couple of years ago (although the Dell server I bought a few years ago has come out of retirement in preparation for that task). We bought a Yamaha PSR E-343 music keyboard for my son this Christmas so this PC may be brought back to life with Garage Band or as a media server as it takes up almost no space at all. Verdict 6/10. Hold.

Gaming: Microsoft Xbox 360 S 250GB with Kinect Sensor

Microsoft Xbox 360sI don’t play this as much as I should but my sons make more and more use of it, and bought me a copy of FIFA 2014 for Christmas, so the Xbox is starting to get a lot more use. No plans to replace it with a newer model though. Verdict 7/10. Hold.

Servers and Storage: Raspberry Pi, 2x Netgear ReadyNAS Duo, various USB HDDs

The Raspberry Pi has replaced my atom-based infrastructure PC, whilst one ReadyNAS is used to back up my work and the other has still not been recovered from its multiple disk failure a couple of years ago.  I still need to consolidate the various USB hard drives onto the  3GB Seagate Backup Plus Desktop drive and sort out the various cloud-based services that I use. (Raspberry Pi) Verdict 10/10. What’s not to like about a computer that costs just £25? (ReadyNAS Duo) Verdict 5/10. RAID failures mean I’ve lost confidence.

Other tech: Arduino Uno, Canon ImageFormula P-215 document scanner

I’m still occasionally playing around with electronics using an Arduino – although I need to do more with this. I’m also slowly regaining control over my filing using the document scanner (and it’s very cathartic shredding old documents!) (Arduino Uno) Verdict 10/10. Inexpensive, with loads of scope for electronic prototyping and a thriving community for support. (Canon P-215) Verdict 9/10. Impressive scanner, although a little on the expensive side.

Potential new toys: Nest learning thermostatLego Mindstorms

Just as last year, I still have my eyes on home automation and tech toys but budgets (and other hobbies) mean they are unlikely to become real for a while yet.  A smart watch is a possibility too… just waiting for the right one…

Hardware lineup for 2013

For the last couple of years, I’ve written a post about my “hardware lineup” – the tech I use pretty much every day (2011, 2012) and I thought I’d continue the theme as we enter 2013.

In these times of austerity, there’s not a lot of scope for new geek toys (some more camera lenses would be great, as would a new MacBook) but there’s no harm in a bit of aspiration, and it’s always interesting to take a look back and see how I thought things would work out and how that compares with reality.

So here’s the tech that I expect my life will revolve around this year…

Car: Volkswagen Tiguan 2.0 TDI Sport

My company car was replaced in April (a nice 40th birthday present) and the Volkswagen Tiguan I drive will be with me for at least 3 years. Whilst there are plenty of more capabile 4x4s and the space afforded by a 7-seater might be nice at times, “the Tig” has been great – my family all love the high riding position, my wife is happy swapping between this and her Golf (she should be – they are practically the same underneath the covers!) and, whilst I miss some of the refinement of my Audi, I get a lot more for my money with the Volkswagen.  Putting a retractable towbar on this car has created new possibilities too, allowing me to use a 4-bike towbar-attached carrier for family cycle trips.

Verdict 8/10. Hold (tied into a 3-year lease).

Phones: Nokia Lumia 800 and Apple iPhone 3GS

Apple iPhone 3GSNokia Lumia 800My initial enthusiasm for the Nokia Lumia 800 waned considerably, after Microsoft announced its Windows Phone 8 plans and the handset lost 60% of its value overnight.  That means I won’t be trading it in for a new model any time soon and, depending on whether Windows Phone 7.8 ever makes it out of the door, I might consider looking at options to run Android on the (rather nice) hardware instead.  Still, at least we got an update a few months ago that, finally, enables Internet Sharing on Lumias (Windows Phone 7.5 supported this capability, but the Lumia 800 firmware did not).

I still have an iPhone 3GS provided by my employer (and my iPad) to fall back on when apps are not available for Windows Phone (i.e. most of the time) and, whilst I’m unlikely to get another smartphone from the company, I am considering a second-hand 4S to replace this as the 3GS is getting a bit long in the tooth now…

(Lumia) Verdict 5/10. Hold, under duress.
(iPhone) Verdict 3/10. Not mine to sell!

Tablet: Apple iPad 3G 64GB

Apple iPadMy iPad never replaced a laptop as a primary computer but it’s still great as a Kindle, for catching up on social media content, and for casual gaming (read, occasional babysitter and childrens’ amusement on long car journeys). I was disappointed to have to pay to replace it after the screen developed a fault, but there’s no reason to trade up yet and there’s still nothing that comes close to the iPad from a media tablet perspective (except newer iPads).

If anything, I might consider a smaller tablet (maybe a Google Nexus 7 or an Amazon Kindle Fire) but and Apple’s decision to stick with a 4:3 screen ratio on the iPad Mini means I have little interest in that form factor (it’s almost the same hardware as my current iPad, albeit in a smaller package). If I were to get a new tablet, it’s more likely to be something that could really be a laptop replacement – perhaps a Microsoft Surface Pro? We’ll see…

Verdict 7/10. Hold, although it’s getting old now.

Everyday PC: Fujitsu Lifebook S7220 (Intel Core 2 Duo P8400 2.2GHz, 4GB RAM, 160GB hard disk)

Fujitsu Lifebook S7220This PC is my main computing device. I’d love a ThinkPad, but the Lifebook is a perfectly capable, solid, well-built notebook PC, although I frequently find myself running out of memory with the number of tabs I have open in a typical browsing session! A recent hard disk failure meant my free space dropped (my 250GB drive was replaced with a 160GB one) but it’s due for replacement soon.

I’ll be looking for a smaller form-factor device to reduce the weight of my work-bag – at least until BYOC becomes a possibility (an ultrabook, Surface Pro, or a MacBook Air would be nice, but not available to me on the company’s catalogue).

Verdict 6/10. Unlikely to be with me for much longer now, although still hoping for a BYOC scheme at work.

Netbook: Lenovo S10e (Intel Atom N270 1.6GHz, 2GB RAM, 160GB hard disk)

Lenovo IdeaPad S10Yet again, this device has hardly seen the light of day. Usurped by the iPad, it now runs Ubuntu and is only ever used for tech projects (e.g. uploading software to my Arduino). My kids have one too but even they are frustrated by the small screen and tend to use my wife’s notebook PC instead.

Verdict 2/10. Not worth selling, so keep for tech projects.

Digital Cameras: Nikon D700 and Coolpix P7100

Nikon D700Nikon P7100I still love my DSLR and the D700 will be with me for a while yet. Indeed, it’s more likely that I would buy some new lenses and a flashgun before I replace my camera body.  Newer bodies offer video but I don’t miss that, and the low light performance on the D700 is pretty good, even 2 years after launch.

The P7100 continues to function as my carry-everywhere camera (it lives in the car), offering entry-level DSLR levels of control in a small package, although it’s not as responsive as I’d like.

(D700) Verdict 9/10. Hold.
(P7100) Verdict 7/10. Hold.

Photography PC: Apple MacBook MB062LL/B (Intel Core 2 Duo T7500 2.2GHz, 4GB RAM, 750GB hard disk)

Apple Macbook White (late 2007)My MacBook is getting old and, although I upgraded to a 750GB disk, I’m struggling with disk space whilst 4GB of RAM is starting to feel a bit light for big Photoshop jobs but new Macs are expensive.

Still too expensive to replace, I think this will last another year, at least…

Verdict 4/10. Hold.

Media: Samsung UE37ES6300 Smart TV

Samsung UE37ES6300My most recent technology purchase, this replaced an aging (c1998) Sony Trinitron 32″ widescreen CRT and has given us back a lot of space in the living room! I’ve been really impressed with the Smart TV functionality (more on that over the next few days) and Internet-connected television is now an integral part of my media consumption habit.

In time, it may be joined by a sound bar (to improve the experience when watching films) but at the moment the TV’s built in speakers will have to make do.

Verdict 9/10. Hold.

Media: Apple Mac Mini MA206LL/A (Intel Core Duo 1.66GHz, 2GB RAM, 120GB hard disk)

(+ iPad, Lumia 800, iPhone 3GS, various iPods, Altec Lansing iM7 iPod speakers, Samsung UE37ES6300)

Apple Mac MiniNo change here since last year – except for the addition of a Smart TV – and I still haven’t re-ripped my CDs after the NAS failure a couple of years ago. I still haven’t bought the music keyboard and this PC’s role as a multimedia PC for the office with Spotify, iPlayer, etc. has been replaced by a Smart TV in the living room.

It may not be the most powerful of my PCs but it may be brought back to life as a media server as it takes up almost no space at all.

Verdict 6/10. Hold.

Gaming: Microsoft Xbox 360 S 250GB with Kinect Sensor

Microsoft Xbox 360sI don’t play this as much as I should to make full use of it but the arrival of BBC iPlayer and the death of our DVD player promoted the Xbox to be our living room  media centre, at least until the Smart TV arrived (and the two still complement each other). My sons are reaching the age where they play games too now, so the Xbox is starting to get a lot more use.

Verdict 9/10. Hold.

Servers and Storage: Atom-based PC, 2x Netgear ReadyNAS Duo, various USB HDDs

The Atom-based PC still provides infrastructure services for the home, whilst one ReadyNAS is used to back up my work and the other has still not been recovered from its multiple disk failure a couple of years ago. I recently bought a 3GB Seagate Backup Plus Desktop drive to replace an assortment of smaller USB hard disks and am preparing to supplement this with suitable cloud storage as we become more and more reliant on our digital assets.

Verdict 6/10. Hold.

New toys from 2012: Arduino Uno, Raspberry Pi, Canon ImageFormula P-215 document scanner

At the end of my 2012 post, I mentioned a few potential purchases and I did pick up one of the first Raspberry Pi computers, which is a fantastic hobby/educational machine to use with or without my children.  I also started to play around with electronics using an Arduino – which is great fun – and I hope to be doing more with both of them this year (more Raspberry Pi postsmore Arduino posts).

I’m slowly regaining control over my filing with the aid of a dedicated document scanner. It doesn’t matter to me that it’s portable, but the fast duplex scanning to PDF and multiple sheet handling (with very few mis-feeds) is a huge step forward compared with the all-in-one printer/scanner/copier I have in my home office.  Mine was an “Amazon Warehouse Deals” purchase (which saved me a few pounds) and the advertised condition suggested it may have a scratch or two but it seems to be in perfect condition to me. It will certainly be a big part of my push to digitise much of my paperwork this year.

(Raspberry Pi) Verdict 10/10. What’s not to like about a computer that costs just £25?
(Arduino Uno) Verdict 10/10. Inexpensive, with loads of scope for electronic prototyping and a thriving community for support.
(Canon P-215) Verdict 9/10. Impressive scanner, although a little on the expensive side.

Potential new toys: Nest learning thermostat, Romotive Robot, Lego Mindstorms

Of course, as a geek, I have my eye on a whole host of potential purchases and these were two that took my fancy in last year’s post, plus one more that I’ve had my eye on for a while (may be something for the kids to get and Dad to play with?).  In all honesty, I’m not sure that I’ll be buying much at all this year, but anything I do is likely to be in the general electronics, robotics and home automation field.

Storing Arduino code in the cloud

Earlier this week I blogged about some of the stuff I’d been doing with Arduino and I mentioned that my code is up on GitHub. What I didn’t mention is how it got there…

I use the Arduino IDE on a netbook running Ubuntu Linux (other development tools are available) and, a few weeks ago, I stumbed across an interesting-sounding hack to store sketches (Arduino code) in the cloud. The tool to make this happen is David Vondle’s Upload and Retrieve Source project. There’s a good description in Dave’s blog post about the project that clears up parts I struggled with (like: the location of the gistCredentials.txt file, used to store your GitHub credentials and which is found in the ~/.arduino folder on my system; and that you also need the username to be included in a comment inside the sketch).  Of course, you’ll need to create an account at GitHub first but you don’t need to know anything about the various git commands to manage source code once you have created the account.

The only downside I’ve found (aside from plain text passwords) is that there is only one project for each Arduino – if you re-use the Arduino with another circuit, the new sketch will be stored in the same gist (although the version control will let you retrieve old sketches, if you know which is which)

 

Upgraded the “traffic lights” in my office already

Last night I wrote a blog post about the “traffic lights” for my office. The trouble with the original setup was that the light was set to a particular state until I edited the code and uploaded a new version to the Arduino.

I was so excited about having something working that I hadn’t actually finished reading the Arduino Programming For Beginners: The Traffic Light Controller post that I referenced. Scroll down a bit further and James Bruce extends his traffic light sequence to simulate someone pressing a button to change the lights (e.g. to cross the road).

I worked on this to come up with something similar – using a variation on James’ wiring diagram (except with 2 LEDs rather than 3, and using pins 11 and 12 for my LEDs and 2 for the pushbutton switch), I now have a setup which waits for the button to be pressed, then sets the red LED, until the button is pressed again (when it goes green), etc.

My code is available on github but here’s the current version (just in case I lose that version as I get to grips with source control…):

/*
Red/green LED indicator with pushbutton control
Based on http://www.makeuseof.com/tag/arduino-traffic-light-controller/
*/

// Pins for coloured LEDs
int red = 11;
int green = 12;
int light = 0;

int button = 2; // Pushbutton on pin 2
int buttonValue = 0; // Button defaults to 0 (LOW)

void setup(){
// Set up pins with LEDs as output devices and switch for input
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(button,INPUT);
}

void loop(){
// Read the value of the pushbutton switch
buttonValue = digitalRead(button);
if (buttonValue == HIGH){
changeLights();
delay(15000); // Wait 15 seconds before reading again
}
}

void changeLights(){
// Change the lights based on current value: 0 is not set; 1 is green; 2 is red
switch (light) {
case 1:
turnLightRed();
break;
case 2:
turnLightGreen();
break;
default:
turnLightRed();
}
}

void turnLightGreen(){
// Turn off the red and turn on the green
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
light = 1;
}

void turnLightRed(){
// Turn off the green and turn on the red
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
light = 2;
}

Now I’m wondering how long it will be before the kids work out that they too can change the status (like pushing the button at a pedestrian crossing!)…

“Traffic lights” for my home office

It’s the school holidays, which means that working from home can be a little… noisy… at times.  Earlier today I saw a blog post from Scott Hanselman where he had hooked a light up to his instant messenger status to tell his family when he was on a call, or otherwise busy with work – and that gave me an idea…

Scott’s solution uses something called a Busylight but a) I’m too tight to spend €49 on this and b) the geek in me thinks “surely I can rig up something using a few LEDs?”. One of the comments on Scott’s post led me to an open source project called RealStatus but that uses an expensive USB HID for the LEDs so doesn’t really move me much further forward…

I decided that I should use my Arduino instead… with the added bonus that involving the children in the project might get them “onboard” too… the trouble is that my electronics prototyping skills are still fairly rudimentary.

As it happens, that’s not a problem – I found an Arduino traffic light program for beginners and, as I don’t have a yellow LED right now, I adapted it to what I do have – simple red/green status (my son and I had fun trying different resistors to adjust the brightness of the LEDs)

You can see the breadboard view here (generated with Fritzing – I’m still working out how to make this into a schematic) and below is my Arduino code (which is also available on github – although I might have worked on it a bit by the time you read this):


// Pins for coloured LEDs
int red = 12;
int green = 13;

void setup(){
// Set up pins as output devices
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
}

void loop(){
// Change the lights
// turnLightRed();
turnLightGreen();
}

void turnLightGreen(){
// Turn off the red and turn on the green
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
}

void turnLightRed(){
// Turn off the green and turn on the red
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
}

It’s pretty simple really – I just call the turnLightRed() or turnLightGreen() function according to whether I am ready to accept visitors. In itself, that’s a bit limited but the next step will be to work out how to send commands to the Arduino over USB (for some integration with my instant messaging client, perhaps) or even using a messaging service (Twitter?) and some network connectivity… more research required!

Short takes: Kids coding in C (!); new car; and finally “fit at 40”!

Last week I kicked off my new initiative to actually get some blog posts out, despite not having time for all the details…

This week was less event-focused but nevertheless contained a few things that I thought were worthy of note.

Kids coding in C? (Our Arduino)

Last weekend, I was “playing” with my new Arduino proptotyping board, with my sons.  Understandably, my 5 year-old wasn’t too bothered (to be fair, he liked putting components onto the breadboard) but I was amazed to see just how my eldest (who is 7) grasped the programming side of things.  I’m not saying he’s writing C – but just using some example code to flash a set of LEDs in sequence, he asked why he was putting // in front of some lines.  I showed him that each was a function call and he was “turning on and off” different things that the program could do.  Before I knew it, he wanted to chain functions together, before then moving on changing the delay times on the lights.  I thought that the coding side of things would be an uphill struggle but I was really encouraged to see how quickly kids can start to adapt the examples. Hopefully our Raspberry Pi will arrive later this month – and then I’ll get him writing in Scratch or another child-friendly environment!

New toy for Mark

Last November, I wrote about ordering my new car and it arrived on Monday. No longer am I tarred with Top Gear-esque comments about Audi drivers (I did really like my A4 though) – I’m now a sensible, 40-something Volkswagen-driving type! The Tiguan (or “softroader” as my hardcore Range Rover-driving manager calls it) has a towbar too, so I should be able to load the family bikes on more easily and, hopefully, we’ll get out a bit more this spring/summer… which leads me on to the next feature…

Another decade on the clock – and my “Fit at 40” challenge draws to a close

Towards the end of the week I celebrated  my 40th birthday – which marks the end of my Fit at 40 challenge. Having hit my target weight a couple of weeks ago, I’ve managed to hold that off but haven’t managed to push any further yet.  The final numbers are not quite in, but it looks like I’ll have raised just under £2000 (plus gift aid) for The Prostate Cancer Charity – thanks again to everyone who has supported me and helped make me a happier, healthier husband and father to my wife and children!

Getting started with Arduino

Arduino UnoA couple of days ago, my new Arduino board arrived and, last night, I finally got around to having a play.

For those who don’t know what an Arduino is:

“Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.”

[Arduino website]

The first thing I noticed was how small the board is (smaller than a credit card) and how large the USB connector makes it… but, when I was working out what I needed to buy (I’ve always been interested in electronics, but never really got into it), I learned a few things about Arduino that might be useful to other newbies:

  • You can buy various Arduino boards, with a variety of processors. I picked up an Arduino Uno, which seems to be a good starting point. My Uno shipped with a USB cable that can also provide power (i.e. powering from a battery or other DC source is optional, as Dave Jacoby and Gareth Halfacree confirmed for me on Twitter – thanks guys). Note that some Uno boards have surface-mounted (SMB) chips – you might prefer to go for a DIP version instead (as I did) as this can be swapped out if necessary.
  • Shields are boards that sit on top of the Arduino and add additional functionality.
  • Whilst my Arduino has an LED on the board that I can interact with to get my head around the concept, you’ll need some more bits and bobs to do anything more complex. There are various starter kits on the interwebs (many or which include the board and a USB cable). I’ve just ordered an ARDX kit from .: oomlout :. (as recommended by Gareth) but one of the reasons for going for this one is that it’s also available without the board if you already have one…  when it arrives, I’ll have a breadboard, and a whole bunch of components to try out different ideas and get my head around controlling electronics programmatically.
  • I bought my board from Amazon (RS Components’ shipping charges were too high and Maplin’s stock levels are appalling) but, I’ve since been recommended a number of suppliers including the aforementioned .: oomlout :., Cool Components and SK Pang (thanks to Andy Piper).

The intention is that I’ll use one or more of these devices to control a model railway (yes, it’s geeky, but it could be a fun project) but, for now, I’ll start off by getting to grips with turning on lights and other similarly simple tasks, and I’ll probably try and get my sons interested too (they’ve already asked what it is… and seen some videos of other people’s projects).

The Arduino integrated developer environment (IDE) runs on Windows, OS X or Linux but, as this is a geek project and my netbook is basically redundant (and very portable), I decided to use that and install it on Ubuntu (11.10). Installation was very simple (I just followed the instructions on the Arduino Playground – a wiki for the Arduino community – and found v22 of the Arduino IDE in the Ubuntu software centre).

With the IDE installed, I set about writing code. Or, perhaps more accurately, I set about modifying other people’s code. There are a load of examples on the Arduino playground and a good getting started guide for Arduino at Bit-Tech but I found the Arduino Tutorials at Lady Ada really accessible for a newbie like me. Pretty soon I used lesson 1 and lesson 2 to change the 1 second blinking LED on my board to a variety of settings, and finally to just off (ready for when I have a go at this with my son).  Once my starter kit arrives with a bunch more components, I’ll try a do something a little more complicated! Maybe one day I’ll even get past the IDE and onto some real C programming (it’s been a while since I wrote any code, let alone in C).

Right… now it’s time to go off and find some cool projects for my new toy…

Image credit: Arduino Uno by Snootlab, on Flickr, used under the Creative Commons Attribution 2.0 Generic licence.

Starting to play with the Internet of things

Unlike some people, who find it invasive, I love the concept of the Internet of things. I’m truly excited by some of the possibilities that a world driven by data opens up. Sure, there are issues to overcome (primarily around privacy and connectivity) – but anyone who believes their data isn’t already being captured by service providers (even if those providers don’t yet know how to handle the massive volumes of data) is in for a shock. So why not embrace the possibilities and use our increasingly smart world to our collective advantage?

In my recent presentation to the BCS Internet Special Interest group, I referred to the Technology Strategy Board‘s Future Internet Report, which talks about [emphasis added by me]:

“An evolving convergent Internet of things and services that is available anywhere, anytime as part of an all-pervasive omnipresent socio–economic fabric, made up of converged services, shared data and an advanced wireless and fixed infrastructure linking people and machines to provide advanced services to business and citizens.”

The report also acknowledges the need for more than just “bigger pipes” to handle the explosion in data volumes. We do need a capable access mechanism but we also need infrastructure for the personalisation of cloud services and for machine to machine (M2M) transactions; and we also need convergence to enable a transformational change in both public and private service delivery.

That’s the big picture but scaling back down to a personal level, one of my colleagues, David Gentle (@davegentle – who happens to be the main author of Fujitsu’s Technology Perspectives microsite) highlighted a site called Pachube to me last week. I first came across Pachube a few months back but [partly because it used to be a chargeable service (it became free at the start of this month)] it got added to my “list-of-things-to-have-a-better-look-at-one-day” (that day rarely comes, by the way!). This time I had a better look and I found it to be pretty cool.

Pachube is basically a cloud-based broker for connected devices with a web service to manage real-time data and a growing ecosystem of applications to feed and consume data. That sounded like it might need some programming (i.e. could be difficult for me these days) but then I found a method to hook an energy monitor up to the web, with no coding required!

I’ve written before about the EnergyFit (Current Cost) power meter that E-ON sent me. I wasn’t a fan of E-ON’s software so I hooked it up to Google PowerMeter for a while, but that service has closed down (along with Microsoft’s Hohm service – which I don’t think even made it to the UK). Using a USB to serial driver and a companion application I now have one of my computers feeding data from my Current Cost meter to the Pachube website, where it gets transformed into JSON, XML or CSV format and “magic” can be performed. I used the Mac OS X software versions of the driver and the application but there are also Windows (driver/application) and Linux (driver/application) variants that I have not tested. The process of setting up a Pachube feed has also changed slightly since the original guidance was written but the basic steps are:

  1. Install the USB-serial drivers.
  2. Install the application
  3. Run the application and select the appropriate serial port (for me, on my Mac, that is /dev/tty.usb-serial).
  4. Create a feed (a push feed – and however many times I turn it private it seems to switch back to public…).
  5. Paste the XML version of the feed into the application.
  6. Set up a secure sharing (API) key (you probably don’t want to use the master key) and paste it into the application.
  7. Save preferences and wait for the application to start feeding data, at which point the feed should show as live

The application I used and the Pachube website seem to work together to configure the datastreams within the feed (one for temperature and one for power) and it’s all set to go.

Once the feed is live, there are a load of apps listed on the Pachube website with everything from graphs and visualisations to mapping tools and augmented reality. I decided to create a page to display some of these, starting out with a customisable PNG-based graph from my feed. That worked, so I added another, together with a PachuDial and a couple of PachuBlog gadgets (sadly, these are Flash-based, so don’t work on the iPad…). Next I created a second feed to consume the power usage from the first one and measure the associated carbon footprint.

Having played around with energy usage, I found that I could also use Pachube to monitor my Twitter account (a pull feed this time) – which might be useful too.

Now I’ve mastered the basics with my Current Cost meter, I might try some home automation using Arduino devices – although that looks to have quite a steep learning curve on the electronics front… In the meantime, you can see the Home electricity usage and Twitter statistics pages that I created using just the Pachube platform and some basic HTML.

[Update 30 November 2011: added comment about Pachube becoming free to use]