Last night, my Arduino Uno Ethernet shield finally turned up.

I’ve been waiting for it to arrive for a while, as it’s the key part of my little system to log and store sensor values from around the house.

I was playing with the Ethernet Shield last night, and I found it a little hard to get the damn thing to send a simple POST request to the internet. It turned out the problem was that while I was assigning it an IP address, I didn’t tell it about the router’s gateway or DNS information - so it’s likely the requests weren’t getting though.

In any case, it works perfectly now and every 10 seconds a request is being sent - and stored - by Phant over at data.sparkfun.com.

It’s just random data at the moment, but it shouldn’t be too hard to connect up the sensors to the Ardunio and go from there.

The reason I’m writing this post is really just to log and document an early version of my code. I have a bad habit with the Arduino of writing code and solving my problem, only to lose the code and have to start again. I reckon it’s down to the way that the IDE forces you to store the files in certain places.

Here’s the receiver code, in a GitHub repo - you’ll need to include a public and private key of your own.

Here’s a quick walk-through of the code in case you’re trying to get the Ethernet working with Phant in a really simple way.

Include all the fun stuff, making sure to include your public and private keys.

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

char server[] = "data.sparkfun.com";
Phant phant("data.sparkfun.com", "public_key", "private_key");

Set your network details:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// the dns server ip
IPAddress dnServer(192, 168, 1, 254);

// the router's gateway address:
IPAddress gateway(192, 168, 1, 254);

// the subnet:
IPAddress subnet(255, 255, 255, 0);

//the IP address is dependent on your network
IPAddress ip(192, 168, 1, 23);

Then, you need to create an instance of the ethernet class. The “client” bit, as you’ll see below, is what the code uses to send data out.

EthernetClient client;

In the setup loop, you should start it all up. The bottom line in will display the IP address. In my case, it is 192.168.1.23.

//initialize the ethernet device
Ethernet.begin(mac, ip, dnServer, gateway, subnet);

Serial.println(Ethernet.localIP());

From here, I split the actual sending to data into another little function called “httppost”. I do this for the simple reason of keeping everything tidy.

The loop is:

void httppost()
{
  if (client.connect(server, 80))
  {
    phant.add("light", "Random Numbers:");
    phant.add("random1", 1);
    phant.add("random2", 2);
    phant.add("room", 3);
    phant.add("temp", 4);
    client.println(phant.post());
  }
  else
  {
    Serial.println("Nothing happening...");
  }
}

What this is doing is using the Phant class (provided by SparkFun) to collect the data and then in phant.post() create a sting that the Phant service uses to store/log the data.

In the example above, the fields are: light, random1, random2, room and temp. I’m just using random data at this point, but they could be anything - sensor values, strings, etc.

Finally, the bit that gets it all working is in the loop() function:

Serial.println("Starting...");
httppost();
Serial.println("disconnecting.");
client.stop();
delay(10000);

This will just display Starting... in the serial window, then it will call the httppost() function I talked about a little earlier.

The key bit I needed to add to make the whole thing work though was: client.stop();

That stopped the request, so after the 10 second delay and when the loop started running again, it would then start the client.connect(server, 80) process over again. In this case, the “server” part is defined at the top of the script - it’s data.sparkfun.com.

It was all a bit confusing at the time, but it mostly makes sense now. This may not be the best way to go about it though, so if you can see any errors (there’ll be many) or want to suggest improvements - let me know in the comments below.

Creating a network of sensors and transmitters

Now that’s done, the next step it to program the receiver to parse the incoming strings from the transmitters. They include a string (of sorts) which will include the data.

The radio payload looks a little like this: S:1#L:%i#T:%i#R:%i#C:%i

There’s lots of hashes, %i, and things in there, so to break it up, S is sensor number, L is light value (from a photo resistor), T is temperature, R is random (likely replaced down the line), C is count (to try to work out if I am missing packets).

In reality, the packet may look like this: S:2#L:555#T:234#R:123#C:232

So I don’t lose it all, here’s the early transmitter code, in a GitHub repo.