• Arduino, an Ethernet shield and Phant - A quick howto

    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

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

    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);
        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:


    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.

  • Sparkfun - data storage


    SparkFun, everyone’s favourite electronics internet retailer, has released a really cool new service called Phant. Well, Phant is the engine behind https://data.sparkfun.com/, a website that allows the “Internet of Things” to store simple data online.

    For example, you’ve got a little thermometer connected to an ethernet enabled Arduino, once you’ve connected it up to the data service, you can send and store the sensor readings in Sparkfun’s cloud.

    As SparkFun says:

    What types of projects would benefit from this service?

    Almost anything. Your weather station in the backyard. A classroom of kids working on science experiments. A community concerned with pollution and crowdsourcing data collection. The weight of your pug’s food dish. You get the idea. If your project can send an http request, you can push data out to our servers.

    I have started writing a simple PHP class that connects to the Phant system.

    It’s called PHPePhant, and it’s hosted on GitHub. It’s still in its early days (it only POSTs data so far), but I hope to build it out soon. I haven’t written an open sourced PHP class before, so if you have any ideas or suggestions let me know. Better still, just dive right in and submit a pull request!

    I can already think of heaps of ways to use this lovely little (and really simple) service, so thanks SparkFun!

    Currently I’m collecting a few parts to create little sensors (which I’ll connect via a really simple cheap 433MHz radio system) that can detect the temperature in my house, if the light is on or not, when the door was opened, etc. All is fed back to the master Arduino, which will feed the data right into Phant. Perfect.

  • LFTP - some commands and references

    Great little FTP client, but a steeper learning curve from normal GUI based ones. Here are a few commands/references I occasionally use. I’ll try to keep this post up to date as I go.

    Connect to server:

    (If wanting to connect via SFTP, add it at the start like below)

    open sftp://host
    user USER

    It should then ask for a password. You can then ls and you should see the files.

    Download a directory:

    (A mirror command downloads multiple files at once and uses segmentation:)

    queue mirror -P 5 --use-pget-n=5 Directory

    One file

    pget -n 5 File.dmg


    To delete a transfer from your queue, add the -d switch:

    queue -d #

    To stop and delete a running transfer, use the kill command.

    If you set lftp to allow multiple transfers simultaneously, kill #, where # is the number listed by jobs.

    kill all will stop and delete all transfers.


    If you want your password to be saved when you create a bookmark, use the command:

    set bmk:save-passwords true

    Sample aliases

    Edit MAC OS X at: /usr/local/etc/lftp.conf

    set bmk:save-passwords true 
    alias dir !ls -Fsh --group-directories-first

    And a list of aliases:

    alias j jobs
    alias jv jobs -v
    alias q queue
    alias qs queue stop
    alias k kill
    alias ka kill all
    alias m mirror -c -P 2 --use-pget-n=5
    alias p pget -c -n 6

    Adding bookmarks

    To do this, use the “bookmark” command.

    Then these:

    add <name> [<loc>] 
    del <name> 

    Full example of the process

    1. Navigate in Terminal to the folder you want, then connect to FTP server from that terminal window
    2. After it connects, it should say cd ok
    3. List all files with ls
    4. To download a file from the FTP, type: q p FILENAME
    5. NOTE: You don’t have to type the whole file name. Just the start, then you can fill the rest by pushing the tab key.

    It’s an easy way to use the FTP server to upload and download files.

  • The Martian

    I finished reading Andy Weir’s The Martian last night.

    I started the day before. I love Mars!

    It’s an easy read. Check it out.

  • Moving from Wordpress to GitHub Pages

    Bye bye Wordpress. Hello GitHub Pages!

    I just decided to move my blogging platform away from the tried-and-true Wordpress to GitHub.


    Well, for a few reasons.

    • I can write posts in Markdown
    • I don’t have to worry about zero-day Wordpress vulnerabilities
    • Version tracking of blog posts!
    • Why not?

    It wasn’t as hard as I thought it would be.

    I followed this guide, which was really helpful in showing how to export comments, move them to Disqus, etc.

    Now I’m just waiting for the DNS changes to take place, and it’ll be all done (apart from learning the way Jekyll works!)