---

Friday, March 26, 2010

New Arduino book on the way

My book '30 Arduino Experiments for the Evil Genius' (McGraw Hill) is out the door now and I have nothing to do except wait for the proofs and play with the web page (www.arduinoevilgenius.com).

The book should be on the shelves in August!

Simplified Ethernet Library for 28J60 Shield










With an Ethernet shield you can transform your Arduino into a tiny little web server. Not only that but a web server with Inputs and Outputs. So you can use it to measure things on an analog input and then access those readings from anywhere on the Internet. Or, if you like, you can use it as a remote control to turn things on and off.

There are two flavours of ethernet shield for the Arduino. There is the official one, based on the Wiznet W5100 chip and the unofficial but significantly cheaper board based on the 28J60 chip.

Nuelectronics sell a board based on this chip and have also produced a library to use it. There are also clones of this to be found on eBay, some of which have a neat little prototying area (see above)

The Nuelectronics library is very thorough, but not the easiest library in the world to use, so I decided to write a library that wrapped up this library with a simpler interface.

You can download the library from here but you will also need to download the Nuelectronics library from here.

Install both libraries into your Arduino environment by unzipping them into a folder called 'libraries' in your sketches directory.

You can now create a simple sketch like this hello world web server example:










#include "etherShield.h"
#include "ETHER_28J60.h"
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};                                        
static uint8_t ip[4] = {192, 168, 1, 15};                         
static uint16_t port = 80;
ETHER_28J60 ethernet;

void setup()
  ethernet.setup(mac, ip, port);
}

void loop()
{
  if (ethernet.serviceRequest())
  {
    ethernet.print("<H1>Hello World</H1>");
    ethernet.respond();
  }
}

This example just displays Hello World when you connect to it in a browser.

I have included some other examples that display the values at the analog ports, set a digital output and also echo the request parameters.


This is all heavily influenced by a posting I saw and now cannot find by some who had done a very similar thing to this. If you are reading this, please let me know so that I can give you the credit you deserve. I just kind of did things a slightly different way, and put it in a library rather than a set of functions.


The API is documented below:


Setup.
void setup(uint8_t macAddress[], uint8_t ipAddress[], uint16_t port);


Example:

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};                                                        
static uint8_t ip[4] = {192, 168, 1, 15};                         
static uint16_t port = 80;
ETHER_28J60 ethernet;

void setup()
  ethernet.setup(mac, ip, port);
}
You will need to find an ipAddress that is both in a valid range and unused on your local network.

The mac address just has to be unique on your network, so unless you have two of these boards on the network, the value above will be fine.

Servicing Web Requests


char* serviceRequest();
Example:




void loop()
{
  char* params;
  if (params = e.serviceRequest())
  {
    e.print("<H1>Web Remote</H1>");
    if (strcmp(params, "?cmd=on") == 0)
    {
      digitalWrite(outputPin, HIGH);
      e.print("<A HREF='?cmd=off'>Turn off</A>");
    }
    else
    {
      digitalWrite(outputPin, LOW);
      e.print("<A HREF='?cmd=on'>Turn on</A>");
    }
    e.respond();
  }
}

serviceRequest either returns null if there has not been an incoming web request to service, or it returns a string containing the request parameter string for the incoming request. The request string includes the '?' character.

In the example above, if the request parameter string is '?cmd=on' then the digital output is set high and the hyperlink to turn the output off is rendered. Otherwise, the digital output is set low and the hyperlink to turn it on is rendered.

This also shows how to write the response and make the final response to the browser's request.

Writing the Response

void print(char* text)
- adds a string of HTML to the response to be returned.

void print(int value);
- adds an integer displayed in decimal form to the response.

void respond();
- commits the response back to the requesting browser.

Some things to watch.
The board consumes about 250mA which seems a lot to me. When driven from a power adaptor rather than USB, that makes the Arduino board get pretty hot around the voltage regulator. So just be careful, particularly if you plan to put your project in a box.

If you start to get strange things happening with the code, and you are using a 168 Arduino, you may well be running out of memory. The examples in the library will work with a 168, but to do anything more substantial use a 328.


About the Author
These are my books. Click on the image below to find out more about them.


                                                                                                                           

Tuesday, March 23, 2010

Apple Rant

I have been using Macs since the days of the 'Multi-Finder' anyone else remember that? When you could actually have more than one application running at a time!

After a time away from Macs I was first in the queue to get a Mac when OS X first came out. I had to put up with a lot of grief from my co-workers and the highly non-standard nature of my computer. Gradually one by one they took to Macs and there is not really any going back to Windows after you have used one.

iPod's - fantastic again very happy to be an early adopter. iPhone - yup I need on of them, and its great.

So, you may be surprised to discover that I am considering trading in my iPhone for an Android phone. The reason for this change of heart, is that Apple are deliberately making it hard for anyone like me to make any kind of hardware that works with the dock connector, without having to buy special licensing chips and give Apple money!

All I want to do is to get at the line-out pins on the dock connector to wire my iPod into my car stereo and have a nice neat single plug dock for my iPod when its in the car, without having to plug a lead into the top for audio.

But you can't! Apple have forbidden it, so my iPod breakout board will be going onto eBay and if it wasn't so damned nice, it would be followed shortly by my iPhone!

Apple, you are supposed to be the good guys, just take a leaf out of Google's book and be nice.