TimesCube – Arduino based standalone web-client cube thing

As if I was not geeky enough, I have signed up for News International’s weekly Arduino Lunch club… Yes I’m giving up my own lunch hour to play around with Microprocessors.

What is Arduino? A good question, I’m glad you asked. Arduio is an open-source signal-board microprocessor (Don’t know what a microprocessor is? Then your reading the wrong blog! but more info can be found here). It’s powered by 3.5 or 5 volts so is great for low power and USB based projects. It has it’s own SDK (as limited as it is) and is programed in a language similar to C++.

This project was developed using the Arduino Uno, which the most versatile of the Arduino rage. The Micro and the Mega are also available. The Micro is, as you may have guessed, is a much smaller unit, and conversely the Mega is a much bigger unit with a lot more inputs and outputs available.

When I was at collage I did an extra module on microprocessors, and studied for an A-Level in electronics but never took the exam due to a clash in exam times with my main course. So I feel at home with basic circuits and programming microprocessors.

So what have a been up to with Arduino?
One of the projects was to create a module to sit on people’s desks and dotted around in receptions that would display interesting information about the NI Titles.

Taking an Agile approach to developing it, I split the build up in to tasks:

1, Ethernet/wireless enable the Arduino board
2, Get the Arduino unit communicating with a web-server
3, Interpret the responses
4, Display the responses
5, Human interface

This is what it looks like now:
Arduino based web clisent

The first one was pretty straight forward. There are a lot of modules or Shields for Arduino Uno. Shields available include: Ethernet, WiFi 802.11g/n, Bluetooth and motor controllers to name a few. The Club already had some Ethernet Shields so I used one of them. If you developing on a budget there is a plane to make you own integrated Ethernet enabled Arduino board A credit card sized Ethernet Arduino compatable controller board.

With the Ethernet Sheild installed I connected it up to my laptop, and opened the Arduino SDK. Poking around in the net and in the Arduino manual I found that I should be able to do most of the hard work using Arduino Libraries.

To get the Arduino board talking to my MBP over Ethernet, I loaded in the Ethernet lib and the SPI lib. The SPI library is… well the manual can explain it better: “Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers.”.

The Ethernet library comes complete with a set of server and client calls. So talking to the web-server is pretty easy, you just make a GET request, it’s interpreting the response when it starts to get harder. Lets take a look at the Arduino code so far, a lot of this code is just a straight copy of the examples on the Arduino site:

#include <SPI.h>
#include <Ethernet.h>

byte mac[]       = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[]           = {10,0,0,1};
byte server[]   = {10,0,0,100};

Client client(server, 80); // Start client mode

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1500); // give the Ethernet a second

  // if you get a connection, report back via serial:
  if(client.connect()) {
    Serial.println("connected");
    client.println("GET / HTTP/1.0");
    client.println();
  }
}

void loop()
{
    // print out whatever you have been sent or wait before checking again.
    if(client.available()) {
      char c = client.read();
      Serial.print(c);
    } else {
      delay(1000);
    }

    // we have read all we need from the server stop now
    if(!client.connected()) {
      client.stop();
    }
}

Rather then assume that everyone knows what is going on with the Arduino, but to avoid turning this into an Arduino 101, I’ll try and weave in the basics. If you’re interested in me writing a 101 guide leave a comment below and if I get enough I’ll make one.

There are two default functions within an Arduino project, they are setup() and loop(). For the PHP developers out there the setup() function is like the __construct() function. It’s run once on start up. The loop() function is run constantly like a while loop with no end match. So every 16mhz I guess the loop code is run, excluding any delays you put in the code.

In the setup() of this code we initiate the Ethernet class and pass over the IP address details of the client and server. Then we set up the communication speed for the Serial library which is a default included library then enable you to respond back to the programing computer over USB.

Then after a brief delay to enable the Ethernet shield and the library to setup to, we make a basic get all to the remote servers IP address.

Then the loop takes over and listens to the response. This is where it all goes a bit strange. What the loop does is grabs a letter at a time from the response until it’s empty. It also passes over the header information.

Part three, interpreting the response. We need to fire all of the response into a buffer so we can then treat it as an entire string. The code goes something like this:

#include <SPI.h>
#include <Ethernet.h>

byte mac[]       = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[]           = {10,0,0,1};
byte server[]   = {10,0,0,100};

String messageBuffer = "";

Client client(server, 80); // Start client mode

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1500); // give the Ethernet a second

  // if you get a connection, report back via serial:
  if(client.connect()) {
    Serial.println("connected");
    client.println("GET / HTTP/1.0");
    client.println();
  }
}

void loop()
{
   // print out whatever you have been sent or wait before checking again.
   if(client.available()) {
     char c = client.read();
     messageBuffer.concat(c);
   }

   // we have read all we need from the server stop now
   if(!client.connected()) {
     client.stop();

     if(messageBuffer.length() > 0) {
       lcd.clear();
       sendToScreen("one");
       sendToScreen("two");
       messageBuffer = "";
     }
}

void sendToScreen(String find) {
 int from    = messageBuffer.indexOf("<line_"+find+">") + 10;
 int to      = messageBuffer.lastIndexOf("</line_"+find+">");
 String line = messageBuffer.substring(from, to);

 Serial.print(line);
}

As you can see we are pushing the response in to the variable messageBuffer, when the response buffer is empty, we proses the string and string out the details we need. To make life easy I have added in some pseudo XML in to the response to make striping of the header information. The code then separates the two strings and displays them on their respective lines.

A quick conclusion on the Arduino Uno: It’s a great little microprocessor, the learning curve on it is not that steep compared to others on the market. There is enough in/out pins to do most hobby projects. But the biggest win for the Arduino is that it’s high adoption in the market, meaning there are plenty of add-ons, libraries and code examples to help along your way, to creating crazy-ass project you never knew you wanted.

About This Post

22/09/2011 - 9:17 pm

Tech

,

Leave a Reply