Probably the Cheapest Computer in the World (ESP8266+LUA)

tmpBE85That’s a grandiose title, for sure and one one that would have been invalid as far back as, oh, yesterday, but I think its’ fair to say that as of this morning we have a stunning new development.  That is not to detract from the many worthwhile firmware developments on-going for the ESP-01 and similar boards, but here I’m going to present you with all you need to get the cheapest computer in the world up and running.

What do I mean by a computer in this context? I mean a device capable of connecting to the outside world via WIFI and doing real things – like flashing a light or reading a sensor – and having a human interface installed…. I’m stretching this a little, but essentially right now as I write this I have in front of me a computer capable of responding to commands in a terminal window – for just under £3 – does that have your interest?

01:12PM – Note: you should refresh this regularly – I’m developing this page as I go along but I thought you’d like an early start on it.

One of the developments I’ve been following closely is that of the LUA firmware for ESP8266. That is, given the starting point of our sub-£3 ESP-01 boards, some firmware for it that actually works. We’ve had problems with the indigenous firmware (but they are slowly going away, I hope) and there are a couple of alternatives floating about – and I have NO doubt they will find their place and I will be using them and blogging about them. But the purpose of THIS entry is to detail this one fabulous attempt to make the ESP-01 not only act as it should, as a WIFI board- but to do it STAND-ALONE.

LUA is a high level language – and I’ll be honest despite knowing DOZENS of computer languages I’d never come across until last week and I am so glad I now DO know a little something about it. So before delving into explanations, lets cut the bull and see what we can expect for £3.

CooltermTake one £3 board  + FTDI (ie a USB to serial interface, cheap and very common in the Arduino world) – feed that into a PC with a suitable terminal program and VOILA. See the picture on the left. Does THAT get your interest up? You are looking at the BEST serial terminal I’ve found this week – having wasted a good day of my life with the horrible REALTERM – this free package could not be better suited to this application IF IT TRIED. 

So what we’re looking at on the right is the output from the little ESP-01 board, loaded up with the LUA firmware (this morning’s version). I’ve installed a couple of routines I put together and I’m talking to the board on my Windows PC, via Coolterm at 9600 baud. In this example I’ve given you the world-famous “Hello world” but there is so much more that you can do already. I envisage testing something like this – then lifting the EPS-01 off onto it’s own motherboard (yet to come into existence, we’re looking at this right now) and controlling something via WIFI.

What you see on the right is the board, connected to a 3v3 supply (should be able to handle maybe 250ma minimum so NOT the very tiniest of regulators) and the FTDI which gives it access to serial commands from my computer using this great Coolterm package.

To follow…. far more information and the high level software to get this far. I figured you’d want to set up a board, get the firmware and grab the terminal software. Refresh this page to see the latest version. I’ll show you examples of coding. If you want more information see my previous blog entries.

LUA beginners guide01:53PM – So, the basic LUA interpreter requires a startup function and that start up function can initialise variables and make the connection to your WIFI router – it’s all interpreted, it all sits in FLASH and hence can be remembered when the power is cycled. Actually creating a product out of this is beyond the scope of this article but the good thing is that armeed with the terminal software you can experiment, often the easiest way to learn. You need to get the software which has some instructions and an API guide. Also grab online a beginners guide to LUA – it really is easy if you’re used to C or similar languages and you’ll find yourself as you’re reading the guide thinking “oh THAT’s a good idea”.

You should be looking to learn about LUA 5.1 – not 5.0 and apparently not 5.2 – and beware the implementation of LUA used in the ESP8266 is not right now a full one – it does not (unless I missed something) handle floating point. But what is in there is support for things you’d never imagine in such a low-cost board – the writer has implemented (I’ve not tested these yet) i2C and some analog functions – he’s also implemented PWM on pins (ok so you’ve only got a maximum of 4 pins and only 2 if you want serial comms to play interactively… but think about it – port extender on those two pins!!!  To get there in the first place you’ll need one of the means of programming the firmware into the boards – I suggest checking previous blog items here as I’ve already mentioned this. I’m using for this firmware the simple esp8266_flasher.exe on my Windows based PC.

More to come.. more information generally including many links here.

Radio Remote Decoding

Do you need to turn mains lights on and off but want to do it from an Arduino-type setup instead of the standard remote controls you get with plug-in-the-wall mains switches? Then this might be for you.

This article is about decoding radio remote controls using Arduino or similar hardware and a cheap 433Mhz radio receiver (£2 on Ebay for receiver and transmitter pair) – and then use that data to control the remote switch units using a similarly cheap transmitter – and was written to serve a purpose: NONE of the existing libraries I could find out there managed to detect all of my radio remote controls.

Cheap radio remotesI’m interested in adding radio remote for mains control to my house control system which is basically an Atmel 1284 chip with Ethernet and radio network. You’ll find various articles on the subject at www.scargill.net

Although it is easy to make a remote switching unit that will be part of a network and control mains lights, it is overkill and it is also quite difficult to find dirt cheap mains to 5v supplies to go into one of the limited range of plug-in-the-wall project boxes along with a controller and a relay.

With that in mind I started to look at the many mains switch remotes available out there. These generally work using simple 433Mhz radio receivers and accepting a code which could be almost any length but is normally somewhere between 10 bits and 50 bits.. the encoding techniques vary. Generally speaking these do not use any kind of “rotating” encoding for security and simply use a block of data, repeated as long as you hold the remote button.

BUYER BEWARE: If you want to control lighting using plug-in-the-wall 433Mhz controllers you should be aware of two things…

[a] the cheap controllers you find in the likes of B&Q use a single frequency which is EASILY blocked by other, similar transmitters.  I found when experimenting with a radio link at 433Mhz that it was easily blocked just by holding a button down on a mains remote controller.

[b] I found a nice cheap set of these plug-in-the-wall mains switches at ALDI recently, 3 remotes and a handset for £10 – what a bargain – but then looking at the fine print – you “train” them by turning them on and pressing a handset button within a few seconds – and that’s fine.. but, I thought, what about power cuts? Sure enough – you have to re-train them every time. What idiot came up with that brilliant idea? Going around the house re-training remotes every time the power fails? Surely not?  But I tested them and that is how they work. They are boxed up ready to be returned – not “fit for purpose”. That is, to be fair, the first time I’ve come across this but if you order online – beware!

ReceiverOk, so what could be difficult the handset lets out a 433Mhz modulated signal when a button is pressed – and the mains blocks pick up this signal, decode it and turn the plug on or off – so all you have to do is read the signal coming off the handset and use Arduino (or PIC etc) existing libraries to record and play back, right? Wrong – I had at least two handsets that use a code which was so long the existing learning libraries could not handle them AT ALL.

Eventually after much reading and learning I decided to do my own thing.

Scope with noise from radio receiverSo, easy enough, (both the transmitter and receiver which I’ve linked to below have ground, power and signal leads – nothing else – the receiver appears to have two signal leads but they are connected together – you need only use either one) assuming the output of the simple receiver is LOW on standby, press the remote and look for the start bit – capture the length of each change of state until the end?  Right?

Well, no… the package is sent over and over – so you have to look for gaps between packages. Easy? Well yes except for AGC – automatic gain control. When the receivers are receiving “nothing” they get more sensitive and in my office the spikes coming out of the output were horrendous. I tried ignoring narrow pulses with limited results then I twigged.. if you run the remote control near the receiver – the noise goes away as the AGC (automatic gain control) kicks in…

433Mhz transmitterSo – press the remote control button – THEN start scanning – and store the gaps between pulses – say, 300 of them.  I did this with some REALLY simple code looking for high and low using the micros() function in Arduino (millis() is way too course) and keeping the code tight by simply storing the actual value of MICROS() in a long array.. ok not efficient but for temporary storage it works and I can get consistent results.

I discovered that all the controls send repeated packages with a short absence of signal in-between. Given the noise issue, the first package often gets scrambled – the breakthrough came when analysing the stored values and realising the solution is to search for the first repeat – i.e. after the first BREAK –hence ignoring the first package – previous attempts to use that had yielded varying results at the start of the package. By the time the second package kicks in the AGC has adjusted and the noise has all but gone.

So this would give me an output like this typical one from a handset.

tmpBFEC

They vary of course in length (not from key to key in that case just the order of bits varies) but from handset manufacture to another model or manufacturer).

What I did notice were pairs of zeros… ie pulses twice the width of others. On reading up this is called Manchester coding (or a variation) – I chose to ignore this – but knowing there will never be more than TWO different pulse widths helps a lot… in reality I realise that there is only one period in this type of encoding.. see this for pictures and explanation if you are interested. http://en.wikipedia.org/wiki/Manchester_code

As you can see from that link, the clock and data are encoded into the one signal.

But for my purposes it’s really NOT important to either understand or use this knowledge. All that is necessary is to send that stream out – fixed width per pulse as per the original and if there are two identical adjascent values (ie 00 or 11, detected by longer and shorter pulses compared to a fixed value indicated by the first pulse) then obviously you simply send the status quo to the output bit for twice the length of time.

But this uses up a lot of storage.  Looking at various handsets I discovered the largest signal seems to be around 40 or so bits… I made up a 12-byte array and wrote a routine to bit-shift the entire array by one bit – hence allowing me to convert the string above to a more sensible set of bytes.

image

In addition I added a byte at the start which represents the width of the basic pulse – as this could be as high as 1200us, I divided this by 10 to fit into a byte. In the example below you see 33 added to the start – ie 330us pulse width. On experimenting, that slight loss of accuracy seems unimportant and it saves a byte.

tmp4A7F

And so for any given button – we now have 11 bytes (in this case) to encode it. I decided that was GOOD ENOUGH.  A little routine to read from the end backwards, sending out pulses (or no pulse) for the duration specified in the first byte – VOILA I was accurately switching mains lamps on and off via my cheap transmitter.

Most of the code is unique, some bits are variations on stuff I found on the web (thanks to all who’ve taken the time to put their ideas up on the web) – and is provided here UTTERLY without support – no comments on styling please – if you’re having trouble reading radio remotes for your purpose – I hope this is useful.

The transmitter and receiver are here

http://www.ebay.co.uk/itm/433Mhz-RF-Wireless-Transmitter-Receiver-Set-NEW-/131093418464?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item1e85c6d1e0

Change input and output pins to suit your project – any pin will do, if you get nothing having pressed your remote then sent A to the board via the Arduino serial monitor, then try messing with INTER_CMS-MS and MIN_POS_WIDTH but I suggest not before you do that, you check with a scope that something is actually coming in.

Clearly in any end gadget you make for your use, you’d not include the learning part or it’s huge TBUFF array. When I get a minute I plan to come up with a means to simply store the data in EEPROM and read from that…  mind you – if you use a 1284 chip instead of the old 328, memory isn’t quite the problem.

If you need more range, you could consider putting the transmitter (NOT the receiver) on a higher voltage up to 12v. I tested mine at 12v and the signal from the 5v logic still worked perfectly. The increase in range is noticeable – I’d say 50% better coverage.  If you blow up your board however, that’s your responsibility – don’t come back to me…

This is all suitable for short bursts to control stuff – don’t get any ideas about continuous home control signalling unless you can guarantee there are no other 433Mhz transmitters in use! I went down this route before realising it’s a non-starter and am now looking at the slightly more expensive frequency hopping chips for more sophisticated use – but if all you want to do is turn something on and off – this could be for you.

 

// I used a simple 433Mhz tx and RX pair - 1 pin each needed. Output of radio assumed 1 when something there ie active high
// No interrupts or clever tricks used
// Using F() in strings merely to keep RAM down, and PROGMEM for arrays - that's the data you see on the serial display for one particular button press... 
// Could store this in EEPROM or whatever... for now could get a lot of these for a particular application into ROM without using up precious RAM
// None of the PROGMEM stuff is essential - just there to minimise RAM use
// Coding Peter Scargill June 2014. www.scargill.net

#define PIN_DATA_IN 2
#define PIN_DATA_OUT 11
#define SERIAL_SPEED 115200
#define INTER_CMD_MS 100
#define CMD_REPEATS 5
#define COMPRESSED_ARRAY 12 // assume that's enough
#define MIN_POS_WIDTH 200 // used before we have the period - assumed minimum acceptable PERIOD less than which we ignore - in Microseconds
#define MAX_NEG_WIDTH 2000 // assumed maximum acceptable ZERO state in uS above which is assumed a gap between blocks of data
#define READINGS 300 // For temporary storage while learning, need 4 bytes per reading... so WATCH OUT - 328 only has 2k of RAM
#define TIMEOUT_VALUE 3000000 // Number of microseconds before we give up the ghost - ie 3 seconds - likely won't happen due to noise

// Byron reports 39us*10 for period - didn't work - so I put in 59 - works a treat.

const byte g0[]PROGMEM={59,77,211,36,73,146,52,73,210,52,1}; // Byron D2on
const byte g1[]PROGMEM={59,217,150,109,217,178,45,91,18};
const byte g2[]PROGMEM={59,219,182,109,217,178,45,91,18};

byte tbuf;

byte arr[12];

// Used to set a timeout
unsigned long start_time;
unsigned long timeout;
unsigned long current_time;

unsigned long calc;

// Stores all the values read here
unsigned long readings[READINGS];
int current_reading = 0;

// Collect serial data into an array - or timeout as you prefer
void readRadioPulses()
{
    for (int a=0;a<COMPRESSED_ARRAY;a++) arr[a]=0; // wipe the eventual storage array
    for ( current_reading = 0;  current_reading <READINGS; current_reading++) // store enough values for 2 or more packages - as long values for speed
    {
        // Waits until the input goes HIGH 
            while (!digitalRead(PIN_DATA_IN))
            {
                if (micros() > timeout) return;
            }
        // When the first value comes, save that period
        readings[current_reading] = micros();
        digitalWrite(13, HIGH);
        // Wait until the input goes low

            while (digitalRead(PIN_DATA_IN))
            {
                if (micros() > timeout) return;
            }
        current_reading++;
        readings[current_reading] = micros();
    }
}

void shiftArray(int x) // move the entire array toward the highest offset by 1 bit and add least new significant bit to offset 0
{
  for (int a=sizeof(arr)-1; a>0;a--)
    {
      arr[a]<<=1; arr[a]|=(arr[a-1]&128)>>7; 
    } 
  arr[0]<<=1; arr[0]|=x;
}

// Sends the data to the serial port
void displayRadioPulses()
{
  int speed;
  byte pickone=0;
 
  for ( current_reading = 0;  current_reading < (sizeof(readings)-1); current_reading++)
    {    
      calc=readings[current_reading+1]-readings[current_reading]; 
      if (((current_reading&1)==0)&&(calc<MIN_POS_WIDTH)) { current_reading++; continue; }      
      if ((calc>MAX_NEG_WIDTH) &&((current_reading&1)==1)) { pickone++; if (pickone==1) speed=readings[current_reading+2]-readings[current_reading+1]; }
      if (pickone==1) // extract string here
          {
          if ((current_reading&1)==0) 
              { 
              if (calc>(speed+(speed/2))) 
                    {
                    Serial.print("1"); shiftArray(1);
                    }
                Serial.print("1"); shiftArray(1);
              } 
              else 
              {
                if (calc>(speed+(speed/2))) 
                  {
                    Serial.print("0"); shiftArray(0);
                  }
                  Serial.print("0");  shiftArray(0);              
              } 
          }
    }
  Serial.println(""); 
  Serial.print(speed/10); Serial.print(",");
  for (int a=0;a<sizeof(arr);a++) { if (arr[a]) { Serial.print(arr[a]); if (a<(sizeof(arr)-3)) Serial.print(",");  } }  Serial.println("");
}

void sendRadio(const byte *m, int sz) // can't do sizeof a PROGMEM array so just pass the size
{
   int scount;
   int offs;
   int period;
   byte tmp;
   period=(int)pgm_read_byte(m)*10; // get the first value, multiply by 10 and that is your period
   for (scount=0;scount<CMD_REPEATS;scount++)
     {         
       for (offs=sz-1;offs>0;offs--) // start at the end and work backwards not including offset zero which is the (period/10)
          {
          tmp=pgm_read_byte(m+offs); Serial.print(".");
           for (int r=0;r<8;r++) { if (tmp&128) digitalWrite(PIN_DATA_OUT,HIGH); else digitalWrite(PIN_DATA_OUT,LOW); start_time=micros()+period; while (start_time>micros()); tmp<<=1; }
          } 
          Serial.println("");
        digitalWrite(PIN_DATA_OUT,LOW); delay(INTER_CMD_MS);
     } 
}

// Listens to SERIAL for inputs A=read, B=write demo - when reading press and hold handset FIRST
void SerialRequest()
{
   char c;
   while (Serial.available()==0);
   c=Serial.read();
    if (c=='A')
      {
        start_time = micros();
        timeout = start_time+TIMEOUT_VALUE;
        readRadioPulses();
        displayRadioPulses();
        Serial.flush();
      }
      
      if (c=='B') // try the example compressed code which includes (period/10) as first byte - can be any length
      {
       sendRadio(g0,sizeof(g0));
      } 

     if (c=='C') // toggle 2 codes back and forth for range testing
      {
       Serial.print(F("Test toggling..."));
       for (int a=0;a<200;a++)
       {
         sendRadio(g1,sizeof(g1));
         delay(1000);
         sendRadio(g2,sizeof(g2));
         delay(1000);
       }
       Serial.println(F("Done"));
      } 
}

void setup() // set serial speed, set output for the radio output - and put something on screen to show it's working - use built-in F() to save RAM
{
    Serial.begin(SERIAL_SPEED);    
    pinMode(PIN_DATA_OUT, OUTPUT);    
    digitalWrite(PIN_DATA_OUT, LOW);
    Serial.println(F("Send A to check or B to send example in PROGMEM"));
}

void loop() 
{
    SerialRequest(); // I could have just put the lot in here really
}

ATTiny85 or Arduino Cricket Generator

For a bit of fun for Digistump fans – a cricket generator. 

This morning I decided I wanted cricket sounds for the garden having done some experiments last night with a friend  A couple of things are important – this needs to sound like an insect and not as most do, sound like an electronic buzzer. We did find one excellent generator on YouTube – but I think it was a con as the guy’s code simply bore no resemblance to the output – so we made our own.

The second thing that is important is power as presumably one would like this to run off solar or maybe charge off solar.The Narcoleptic library lets you have delays in which the processor is off – down to a few micro-amps. So we used that – BUT the settings in the library would not allow the use of the Digistump – threw up an error – so in the Narcoleptic.cpp file – simply change WDTSCR to WDTCR – 3 occurrences  OR make the change in a more sophisticated way as here  http://forum.arduino.cc/index.php?topic=108870.0

Here’s the Narcoleptic library: https://code.google.com/p/narcoleptic/ – my thanks to Aidan Ruff for bringing that one to my attention.

The Narcoleptic stuff above is ONLY to save power… you could just use DELAY if you wanted and forget the library.

So in this case – you need a piezo element (not a speaker – can’t handle the high frequency) across outputs P0 and P1 and I suggest once you’re happy  – snipping off the two surface mount LEDS as they use juice! 

The nice thing about the Digistump is that it’s cheap. Another way is just to program the ATTINY85 on it’s own – internal Xtal and fasten straight to a solar cell/battery arrangement capable of 3-5v output – an extension might be – read the voltage when set to internal reference by a VERY high value pair of resistors, measure the voltage of the solar cell (occasionally while sleeping the rest of the time) – if the cell is dead (ie nighttime) start chirping!.

I actually sat down and listened to a real recorded sample from one of the insects… firstly the main frequencies are around 5Khz, so that’s 200us. Usually around 125 of those followed by a 25ms break.. in groups of 5….  then there are maybe 3-4 of those bursts with something like 200ms between them- then a gap – anything from 2 seconds upwards.

I noted also that in each single burst of the high frequency, the level increases then drops off achieving soft edges to the waveform– I achieved that with PWM.

The gaps being a real creature are not AT ALL consistent – hence the random numbering to emulate what the real thing might do..

Finally I noticed in each little sound block the frequency actually increases towards the end – I used a loop variable to add a little extra delay near the start, decrementing over 5 blocks of sound.

The result – will depend on the piezo you use but on mine – compared to a live recording – it’s not bad – not bad AT ALL… here’s the code – feel free to experiment. Half a dozen of these in the garden…..

If you REALLY want to go to town  – a movement sensor to make the thing shut up for a while when it senses movement nearby?

 

Enjoy the code and feel free to mess with it if you thing I’m wrong about the sounds…

 

cricket

MQTT on Arduino

MQTT is essentially a simple format for sending small data packages back and forth. It can be used with or without password and the passwords can be simple or encrypted. The information out there is not easy to understand for beginners.

Essentially you have a BROKER (some may think of it as a server). This program accepts messages and dishes out messages.  It handles them in the form of topics and messages.

Imagine you wanted to turn a relay on and off remotely with your Arduino.  Let’s pick a topic name out of thin air… “listentome”.

So – your Arduino needs an Ethernet client connected an MQTT broker and the client needs to be “subscribed” to topic “listentome". In the simplest case with no passwords, if anyone sends a message such as “turnrelay1on” to the topic “listentome” on the broker – your Arduino will get that message.

Of course with a username and password, only those clients with that username and password will receive that message.

So let’s get a working example so you can see this. And this is where it gets painful unless you’re reading this – where do I find an MQTT “Broker” – what about clients?

Ok, so first things first – on Android there’s a simple server package called Servers Ultimate Pro. This does not YET have the passworded version of the broker but it does have a simple version.  Better – but more complicated – is Mosquitto.  Basically install this from here… open up a command line (yes, I know, how awful but hey, it’s free) and go to the Mosquitto directory and simply type “Mosquitto” in the command line – you’ll see the program is running but with no visible output. Actually it has installed a “service” and you could run it that way but lets stick with the simplest solution first.

Before we start messing with code that might or might not work first time you’ll want to make sure your Mosquitto or Android broker is working – and for that you might want to use an external test program… such as this… the only issue there being – how does it know what your internal IP address is?  Well, to test, I simply put a port redirect on my router.  The standard port used is 1883. Point that external port to the same port on your PC or WIFI-enabled phone or whatever is running the above.

tmp627CAssuming you know how to do that – and with Mosquitto running – you should be able to plug in your external IP address and connect using the tool at http://mqtt.io/ 

Assuming that works – at the bottom – send a message to topic, oh…. “mytest”.

tmpC87D

Won’t work – because no-one is listening. In the window called “subscribe to topics” – SUBSCRIBE to mytest and try again. You’ll see your message come in.

Ok, so how do we get from there to sending messages to and from your Arduino?

 

You will need your Arduino, the Ethernet shield (don’t use the cheaper ENC boards, they are just so much hard work and I’ve never managed to get them to run reliably – get proper Ethernet cards and if cost is an issue – try Ebay and China). You need to get this library and install it. https://github.com/knolleary/pubsubclient  unzip the file and there’s a pubsubclient INSIDE the outer one – you need to copy that into your LIBRARY directory for Arduino. Clean start the Arduino IDE for this to take effect.

Examples out there put the setup in the setup routine – great – but what if you lose the connection – so I’ve taken a fairly standard example and moved that setup into the main loop – so that should for any reason you lose the connection – it will reconnect automatically.

Once you get this up and running and understand what you’re doing – moving to the password protected version should be easy enough (read the Mosquitto docs) and actually using this to control relays etc and return status results should be equally easy.

/*
Basic MQTT example
 
  – connects to an MQTT server
  – publishes "hello world" to the topic "outTopic"
  – subscribes to the topic "inTopic"
*/
int sendit=0;
char msg[20];

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h> // only non-standard library – the rest come with Arduino IDE

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; // make this unique
// internal address of your MQTT broker… or see examples forusing address lookup etc.
byte server[] = { 192,168, 0, 24 };
// internal IP your want this board to be
byte ip[]     = { 192,168, 0, 100 };//

byte mydns[] = { 192, 168, 0, 138 };       // your router     
byte gateway[] = { 192, 168, 0, 138 };     // your router       
byte subnet[] = { 255,255,255,0 };          // your IP address mask – usually this value

// this routine is called automatically when something comes in. I’ve used serial.print to let you see it. Also use this to prepare a response.
void callback(char* topic, byte* payload, unsigned int length) {*(payload+length)=0;
  Serial.print((char *)topic); Serial.print(" : "); Serial.println((char *)payload);
  strcpy(msg,"Sorted"); sendit=1;
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
  Serial.begin(57600);
  Serial.println("Connecting");
  Ethernet.begin(mac, ip, dns,gateway,subnet);
  Serial.print("Got IP… ");
}

void loop()
{

// this checks to see if you’re connected and connects – will retry forever if need be.
  if (!client.connected())  {
   delay(1000);
   Serial.print("Connecting… ");
   if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
    Serial.println("Connected");
  }
}
 
  client.loop();
  if (sendit) { sendit=0;   Serial.println("Sending"); client.publish("outTopic",msg); }
}

Home Control over the Internet

Updated July 5, 2013

This article is about a practical home control system for the enthusiast and is being updated as new developments come along. Combining extremely low cost radio and inexpensive Ethernet, the system allows for mobile phone or tablet access to a range of items including lamp control (on/off/fade), temperature monitoring (and thermostatic control and general analog inputs)… and this is just the beginning:

tmp901ERecently I’ve turned to working with what are called “Arduino” chips and boards – actually the only “Arduino” bit I use is their boot loader routine – the boards I use are usually ATMEL chips on boards I’ve designed (or kluged)… the chip is a simple microcontroller (not powerful enough to call a “Microprocessor” but a hell of a lot nicer than the PIC chips we used a decade ago.

The Atmel chips are fun devices that are (relatively) easy to use and there are several boards available cheaply, mainly from China or Chinese companies in the UK that make this whole control thing worth looking at again. More’s the point there are lots of libraries – some working, some part working out there to save re-inventing the wheel.

The big issue for me has always been the wires. Much better to control stuff via, say, a mobile phone and have the units work by radio… but it has to be CHEAP to be worthwhile.

That is the basis of the project I’m working on right now. On the left you’ll see the first experimental screen of my Samsung S4 project (though it works seamlessly on any Android or iPhone-type device such as a tablet). For the purpose of experimentation, this screen is showing local 4 on/off type controls, 5 remote station on-off/off controls 4 remote on-off controls, time (from the base unit), temperature monitors from the base unit (internal/external) and temperature and humidity from a small unit at the end of the radio chain… but that’s just the start.

Note (July 5 2013) – I’ve made a decision, rather than continue to struggle with space on the master unit – and in order to use the same final circuit board on the master as will be used on the slaves – to eliminate IO control on the master – hence freeing up space. The master unit will concern itself with handling communication with the slaves, talking to the Internet and handling thermostatic calculations. After much thought this is the better way to go rather than having special cases all over the place for “local” controls. I did look at the MEGA boards with lots of extra room – but it makes sense to use one low cost board for everything – pointless jacking the price onwards and upwards.

The mobile DISPLAY is made possible by software which costs a pound or so called NETIO and is available for Android and iOS. It is a customisable App that works on the phone complete with web tools to develop such panels without programming as such.

tmp4692Essentially, the panel works by sending simple commands from the phone over WIFI or 3G to a web server running what is referred to as a WEB SOCKET – i.e. the basic underlying mechanism behind a web page but without all the extras – after all we’re talking simple commands here – it just needs to be very reliable.

And what and where is the “socket server”? It is software running on an Atmel chip of course – but the server is relatively simple (it’s actually complicated but we don’t need to know that as we just use a freely available library accepting commands and returning text responses). The rest of the code on the Arduino concerns itself with checking temperatures and firing off remote commands by radio to other boards. The App merely puts the end-user gloss on it. The radio is based on something I’ve written about before – RF24NETWORK which never worked properly in the past due to a minor issue in the underlying radio library and my own incompetence in not putting a smoothing capacitor on the radio board. But that’s all covered elsewhere.

Currently I have a board sitting on my bench talking to the App on my phone, over the Internet. On the top right you’ll see an image – I’m using a standard board for the purpose of experimenting – an Arduino Uno board at the back with an Ethernet “shield” sitting on top of it – together with a couple of test-wired Dallas 3-pin temperature-reading chips that look like simple transistors and actually only need 2 wires to work (but I’m using 3). In the foreground is a twin relay board of the type I’m using to control things such as lights or heating system thermostat replacement. I could just as easily use opto-coupled triacs. Note that the final unit has no wiring for LOCAL controls – these are all handled on the slave radio units.

In addition, there was enough room in the master board software to call an Internet TIME service and that keeps the local clock accurate – and also sends the time in the 4-byte time_t format to the slaves. As these have the real time clock software in them – they can be updated automatically by incoming packages – and hence use the time for whatever purpose…. providing time displays on household gadgets etc… one example – I have a SAD light in the bedroom which runs on a software clock only which in time becomes inaccurate – with the simple addition of the radio network and a little software, this when upgraded will keep perfect time.

Arduino with Ethernet shieldNote the yellow Internet lead and underneath (top picture) , a simple plug-in the wall power supply (5v) feeds the lot.

This is only for the purpose of testing and the hardware above probably comes to £30 – but an Arduino clone can be had for as little as £6 or so, while the  “Ethernet shield” can be purchased from Ebay for maybe £13.

So you can see it is possible to put together something for very little money. I have looked at the cheaper ENC boards but they use up a considerable amount of RAM memory and hence any savings there would mean moving to a larger processor – defeating the object.

imageThe radio unit at the end of the chain shown below right costs excluding case and USB power less than a tenner. That board is the one supplying temperature and humidity readings in the top display. The blue item underneath is the temperature sensor.

The second screen on the right above is merely a test page showing 6 local slider controls and analog inputs from 5 boards – the last one being located way down the radio network chain at the other side of the house.

tmp66A1So that gives us one card controlled by the Internet. How does that make a home control? The next stage is to have that card also talk to a wireless controller – to talk to other boards. The cost of wireless is similarly inexpensive but very limited.

The NRF boards will typically talk DIRECTLY to 5 other boards (they CAN talk to more but typical libraries out there limit themselves to 5) over the kind of range you’ll see for cheap home WIFI – i.e. not very far and definitely not through more than a couple of walls – what’s needed is a means to network them – and that’s where the software library RF24NETWORK comes in – a simple means to network dozens of these together. It may be in this case that the network is not needed – the fact that the main unit can talk to 5 other radio boards may well be sufficient and if not then simply relaying software from one to the next takes that up to 25 boards and way beyond.

The NETio designer is in touch and has been helpful. Next step is to fully test it on the really cheap Ethernet boards (done that but not yet happy about potential memory leaks). I’ve already had success with the radio boards and the rest is easy. Here are a couple of early blogs on the subject of the radio network… and with links to other resources.

Current prototypes feature:

2 12-bit analog inputs (had 4 but needed pins 0 and 1 to check serially…)

4 digital outputs

3 analog PWM outputs (ideal for LED LIGHTING by adding a MOSFET and 12v supply for the lighting – again a plug in the wall job)

2 DHT11/22 chip temperature inputs (I have implemented both humidity and temperature – could have used Dallas chips for temperature but this seemed like 2 for 1).

You can add as many radio boards as you need and each one has the same facility.

The THIRD screen (left, above) is the thermostat control page – I’ve added the ability to control a single remote relay as the thermostat chip -  with the kind of control (heat delay etc) you’d expect from a Thermostat.

Alongside this I’ve asked the NETio designers if they will consider implementing MQTT – as this adds security – we already have MQTT working on the ENC boards thanks to a Saturday session with friend Jonathan Farmer and a lot of follow up work. For now my security consists of sending a special character string to allow access to other functions – that times out after 60 seconds unless refreshed by the App. Hey – it works!

Current state of the art

Currently I have enough memory to do the job using the WIZ network card (hence saving RAM)… and ROM I’m up to 27out of 30.7k available.

The way this works (and I have a working setup on my desk)…. the units responds to commands such as x/OUT1=1  or x/OUT1=0  where x is the unit number in octal (base 8 – hence 0 1 2 3 4 5 6 7 10 11 12 etc)

tmp652BThe word OUT1 on it’s own returns the status of that output – and so on. So 2/OUT1=1 would turn on output 2 on the radio slave “2” for example.  In the picture above, lets say the radio board that has a blue arrow to it’s right is radio 2. To handle relaying of these short range radios, that radio can talk to radio “22” as well as 4 other siblings. – radio “22” can talk to 5 siblings – “122” through “522” etc., the further down the chain the more you add on the LEFT of the address. So  522/OUT1=1 would turn on the smallest board on the right, picture above, the 5th one.

So – at level 1 the base can talk to 5 units… next level in it can talk to 5+(5*5) = 30 units. At the next level 5+(5*5)+(5*5*5)=155 units – next level out increases dramatically to 780 units – ANY unit can talk to any other but the parents MUST be turned on (hey what do you want for a few pounds)… I think 4 levels (780 units) is likely sufficient (and polling such a large number would likely be impractical but it does give you a good hopping range given the limitations of the cheap radios) and in my prototype setup I can run a slider on the phone and vary the brightness of a LED on the farthest unit virtually in real time with only a tiny, fractional second delay.

Arduino clone plus radioFor an absolute minimal slave board you just need the radio and one of the small surface mount Arduino clones such as the one shown on the left here. We’re looking at no more than £6 plus power (5v – i.e. any old USB plug-in-the-wall supply available for less than £3) – this needs something like a relay to drive a lamp or heating – and that’s it – clearly if you want more – add more. That whole board is the size of my thumb!

And that’s it – the first fruits of this work will be to put temperature monitors and light/heat control in our rental cottage and our place in Spain so I have access to this and other information on the phone. Implementing a movement counter with a simple infra-red is another idea etc.… I need some boards first as right now this is all a bit too spaghetti. These are on the way.

Features include the ability to store in EPROM, settable remotely, the unit number from which to  get the temperature for thermostatic control – and the unit which you need to talk to in order to turn the heat on by a relay. I’ve also clarified my worst fear – RF24NETWORK cannot handle sending out one communication after another while expecting to READ communications… so I’ve arranged that data is only initiated by unit 0 – which then always expects a response back. I’ve arranged a timeout for 0.2 seconds for expected data return before sending the next package out – obviously cancelling that delay when something comes in – with that in place and only with that in place I’m getting 99% reliability out of the radio packages. I’ve just added multiple retries (rarely needed) and with that I seem to be getting 100%.

Relay outputs by default are HIGH as most of the relays out there demand a pull DOWN to turn the relay on…!! There is the ability in the software to invert this.

And now a little history – you can stop reading now if you’re not interested but this might help to understand how I got this far… Once upon a time in the 90s, my business partner (of the time) and I developed a home control project called Appcon which consisted of a small board with various sensors and outputs – a triac (solid-state relay) to control, say lighting or heating, a temperature sensor, a general input and a speaker output. Click these pictures to see larger versions.

1994 PC AnswersThis small board ran from 12 volts and connected to other boards via a 4-core telephone wire.  at one end of the wire was a PC firing out commands, reading data and generally controlling the whole thing with a set of rules.

imageIt was developed on the original Windows 3.1 before the time of widely available Internet and LONG before we could buy cheap stuff from China.

This worked well, in my house in Newcastle I had around 40 of these cigarette-pack sized boards controlling just about everything from heating, lighting, burglar alarm, putting out “cricket” sounds in the garden, you name it.

The problem in developing this was the nature of construction – the fact that the boards had to be wired together was a real issue. In some homes it’s just not that easy to put a wire all over the place to connect everything together. It certainly is not in my stone-walled country cottage today.. I’d get shot!

The interface was good but as Windows matured it rapidly started to date and there was no such thing as a mobile phone interface! Today you would not think of using this (well, I wouldn’t!)

That product was GREAT fun and we sold quite a lot of them, but at the end of the day, home control remained a minority sport and we never did get this into the B&Qs of this world. Meanwhile, particularly in the UK, what’s out there remains expensive. Not too many people want to shell out £30-50 just to turn a light on and off!

Home Control over the Internet

Updated June 29, 2013

This article is about a practical home control system for the enthusiast and is being updated as new developments come along. Combining extremely low cost radio and inexpensive Ethernet, the system allows for mobile phone or tablet access to a range of items including lamp control, temperature monitoring (and thermostatic control and general analog inputs)… and this is just the beginning:

tmp901ERecently I’ve turned to working with what are called “Arduino” chips and boards – actually the only “Arduino” bit I use is their boot loader routine – the boards I use are usually ATMEL chips on boards I’ve designed (or kluged)… the chip is a simple microcontroller (not powerful enough to call a “Microprocessor” but a hell of a lot nicer than the PIC chips we used a decade ago.

The Atmel chips are fun devices that are (relatively) easy to use and there are several boards available cheaply, mainly from China or Chinese companies in the UK that make this whole control thing worth looking at again. More’s the point there are lots of libraries – some working, some part working out there to save re-inventing the wheel.

The big issue for me has always been the wires. Much better to control stuff via, say, a mobile phone and have the units work by radio… but it has to be CHEAP to be worthwhile.

That is the basis of the project I’m working on right now. On the left you’ll see the first experimental screen of my Samsung S4 project (though it works seamlessly on any Android or iPhone-type device such as a tablet). For the purpose of experimentation, this screen is showing local 4 on/off type controls, 5 remote station on-off/off controls 4 remote on-off controls, time (from the base unit), temperature monitors from the base unit (internal/external) and temperature and humidity from a small unit at the end of the radio chain… but that’s just the start.

Note (June 23) – I’ve made a decision, rather than continue to struggle with space on the master unit – and in order to use the same final circuit board on the master as will be used on the slaves – to eliminate IO control on the master – hence freeing up plenty of space. The master unit will concern itself with handling keepalive signals from the slaves, talking to the Internet and handling thermostatic calculations. After much thought this is the better way to go rather than having special cases all over the place for “local” controls.

The DISPLAY is made possible by software which costs a pound or so called NETIO and is available for Android and iOS. It is a customisable App that works on the phone complete with web tools to develop such panels without programming as such.

tmp4692Essentially, the panel works by sending simple commands from the phone over WIFI or 3G to a web server running what is referred to as a WEB SOCKET – i.e. the basic underlying mechanism behind a web page but without all the extras – after all we’re talking simple commands here – it just needs to be very reliable.

And what and where is the “socket server”? It is software running on an Atmel chip of course – but the server is relatively simple (it’s actually complicated but we don’t need to know that as we just use a freely available library accepting commands and returning text responses). The rest of the code on the Arduino concerns itself with checking temperatures and firing off remote commands by radio to other boards. The App merely puts the end-user gloss on it. The radio is based on something I’ve written about before – RF24NETWORK which never worked properly in the past due to a minor issue in the underlying radio library and my own incompetence in not putting a smoothing capacitor on the radio board. But that’s all covered elsewhere.

Currently I have a board sitting on my bench talking to the App on my phone, over the Internet. On the top right you’ll see an image – I’m using a standard board for the purpose of experimenting – an Arduino Uno board at the back with an Ethernet “shield” sitting on top of it – together with a couple of test-wired Dallas 3-pin temperature-reading chips that look like simple transistors and actually only need 2 wires to work (but I’m using 3). In the foreground is a twin relay board of the type I’m using to control things such as lights or heating system thermostat replacement. I could just as easily use opto-coupled triacs. Note that the final unit has no wiring for LOCAL controls – these are all handled on the slave radio units.

In addition, there was enough room in the master board software to call an Internet TIME service and that keeps the local clock accurate – and also sends the time in the 4-byte time_t format to the slaves. As these have the real time clock software in them – they can be updated automatically by incoming packages – and hence use the time for whatever purpose…. providing time displays on household gadgets etc… one example – I have a SAD light in the bedroom which runs on a software clock only which in time becomes inaccurate – with the simple addition of the radio network and a little software, this when upgraded will keep perfect time.

Arduino with Ethernet shieldNote the yellow Internet lead and underneath (top picture) , a simple plug-in the wall power supply (5v) feeds the lot.

This is only for the purpose of testing and the hardware above probably comes to £30 – but an Arduino clone can be had for as little as £4 or so, while the  “Ethernet shield” will hopefully be replaced by a cheaper Ethernet board which can cost barely more than £4. Currently the one I’m using is around £12 – but this is only needed on your main board! The slave boards are even more inexpensive as all they need is a radio card – and so you can see it is possible to put together something for very little money.

The radio unit at the end of the chain shown below right costs excluding case and USB power less than a tenner. That board is the one supplying temperature and humidity readings in the top display. The blue item underneath is the sensor – it is pointless putting it inside the case as any residual heat no matter how little in the circuitry will affect readings.

The cheap ENC network cards are not as easy to use for “web sockets” because a lot more is left to the imagesoftware (that’s why they are cheaper) and socket implementations to date have been few and far between but thanks to work by Peter Oakes in Canada who’s been helping me with this, it looks like the ENC board is cracked all but for a question of reliability – we have a working socket implementation using code originally written for the Nanode boards. Right now it falls over occasionally so I’m leaving it to one side while I get the rest perfected.

The second screen on the right above is merely a test page showing 6 local slider controls and analog inputs from 5 boards – the last one being located way down the radio network chain at the other side of the house.

tmp66A1So that gives us one card controlled by the Internet. How does that make a home control? The next stage is to have that card also talk to a wireless controller – to talk to other boards. The cost of wireless is similarly inexpensive but very limited.

The NRF boards will typically talk DIRECTLY to 5 other boards (they CAN talk to more but typical libraries out there limit themselves to 5) over the kind of range you’ll see for cheap home WIFI – i.e. not very far and definitely not through more than a couple of walls – what’s needed is a means to network them – and that’s where the software library RF24NETWORK comes in – a simple means to network dozens of these together. It may be in this case that the network is not needed – the fact that the main unit can talk to 5 other radio boards may well be sufficient and if not then simply relaying software from one to the next takes that up to 25 boards and way beyond.

The NETio designer is in touch and has been helpful. Next step is to fully test it on the really cheap Ethernet boards (done that but not yet happy about potential memory leaks). I’ve already had success with the radio boards and the rest is easy. Here are a couple of early blogs on the subject of the radio network… and with links to other resources.

Current prototypes feature:

2 12-bit analog inputs (had 4 but needed pins 0 and 1 to check serially…)

4 digital outputs

3 analog PWM outputs (ideal for LED LIGHTING by adding a MOSFET and 12v supply for the lighting – again a plug in the wall job)

2 DHT11/22 chip temperature inputs (I have implemented both humidity and temperature – could have used Dallas chips for temperature but this seemed like 2 for 1).

You can add as many radio boards as you need and each one has the same facility.

The THIRD screen (left, above) is the thermostat control page – I’ve added the ability to control a single remote relay as the thermostat chip -  with the kind of control (heat delay etc) you’d expect from a Thermostat.

Alongside this I’ve asked the NETio designers if they will consider implementing MQTT – as this adds security – we already have MQTT working on the ENC boards thanks to a Saturday session with friend Jonathan Farmer and a lot of follow up work. For now my security consists of sending a special character string to allow access to other functions – that times out after 60 seconds unless refreshed by the App. Hey – it works!

Current state of the art

Currently I have enough memory to do the job using the WIZ network card (hence saving RAM)… and ROM I’m up to 27.8k out of 30.7k available.

The way this works (and I have a working setup on my desk)…. the units responds to commands such as x/OUT1=1  or x/OUT1=0  where x is the unit number in octal (base 8 – hence 0 1 2 3 4 5 6 7 10 11 12 etc)

tmp652BThe word OUT1 on it’s own returns the status of that output – and so on. So 2/OUT1=1 would turn on output 2 on the radio slave “2” for example. 0/OUT1 would turn on output 1 on the master board. In the picture above, lets say the radio board that has a blue arrow to it’s right is radio 2. To handle relaying of these short range radios, that radio can talk to radio “22” as well as 4 other siblings. – radio “22” can talk to 5 siblings – “122” through “522” etc., the further down the chain the more you add on the LEFT of the address. So  522/OUT1=1 would turn on the smallest board on the right, picture above, the 5th one.

So – at level 1 the base can talk to 5 units… next level in it can talk to 5+(5*5) = 30 units. At the next level 5+(5*5)+(5*5*5)=155 units – next level out increases dramatically to 780 units – ANY unit can talk to any other but the parents MUST be turned on (hey what do you want for a few pounds)… I think 4 levels (780 units) is likely sufficient (and polling such a large number would likely be impractical but it does give you a good hopping range given the limitations of the cheap radios) and in my prototype setup I can run a slider on the phone and vary the brightness of a LED on the farthest unit virtually in real time with only a tiny, fractional second delay.

Arduino clone plus radioFor an absolute minimal slave board you just need the radio and one of the small surface mount Arduino clones such as the one shown on the left here. We’re looking at no more than £6 plus power (5v – i.e. any old USB plug-in-the-wall supply available for less than £3) – this needs something like a relay to drive a lamp or heating – and that’s it – clearly if you want more – add more. That whole board is the size of my thumb!

And that’s it – the first fruits of this work will be to put temperature monitors and light/heat control in our rental cottage and our place in Spain so I have access to this and other information on the phone. Implementing a movement counter with a simple infra-red is another idea etc.… I need some boards first as right now this is all a bit too spaghetti.

I’m currently making mods to store in EPROM, settable remotely, the unit number from which to  get the temperature for thermostatic control – and the unit which you need to talk to in order to turn the heat on by a relay. I’ve also clarified my worst fear – RF24NETWORK cannot handle sending out one communication after another while expecting to READ communications… so I’ve arranged that data is only initiated by unit 0 – which then always expects a response back. I’ve arranged a timeout for 0.2 seconds for expected data return before sending the next package out – obviously cancelling that delay when something comes in – with that in place and only with that in place I’m getting 99% reliability out of the radio packages. I’ve just added multiple retries (rarely needed) and with that I seem to be getting 100%.

And now a little history – you can stop reading now if you’re not interested but this might help to understand how I got this far… Once upon a time in the 90s, my business partner (of the time) and I developed a home control project called Appcon which consisted of a small board with various sensors and outputs – a triac (solid-state relay) to control, say lighting or heating, a temperature sensor, a general input and a speaker output. Click these pictures to see larger versions.

1994 PC AnswersThis small board ran from 12 volts and connected to other boards via a 4-core telephone wire.  at one end of the wire was a PC firing out commands, reading data and generally controlling the whole thing with a set of rules.

imageIt was developed on the original Windows 3.1 before the time of widely available Internet and LONG before we could buy cheap stuff from China.

This worked well, in my house in Newcastle I had around 40 of these cigarette-pack sized boards controlling just about everything from heating, lighting, burglar alarm, putting out “cricket” sounds in the garden, you name it.

The problem in developing this was the nature of construction – the fact that the boards had to be wired together was a real issue. In some homes it’s just not that easy to put a wire all over the place to connect everything together. It certainly is not in my stone-walled country cottage today.. I’d get shot!

The interface was good but as Windows matured it rapidly started to date and there was no such thing as a mobile phone interface! Today you would not think of using this (well, I wouldn’t!)

That product was GREAT fun and we sold quite a lot of them, but at the end of the day, home control remained a minority sport and we never did get this into the B&Qs of this world. Meanwhile, particularly in the UK, what’s out there remains expensive. Not too many people want to shell out £30-50 just to turn a light on and off!

Home Control over the Internet

Updated June 29, 2013

This article is about a practical home control system for the enthusiast and is being updated as new developments come along. Combining extremely low cost radio and inexpensive Ethernet, the system allows for mobile phone or tablet access to a range of items including lamp control, temperature monitoring (and thermostatic control and general analog inputs)… and this is just the beginning:

tmp901ERecently I’ve turned to working with what are called “Arduino” chips and boards – actually the only “Arduino” bit I use is their boot loader routine – the boards I use are usually ATMEL chips on boards I’ve designed (or kluged)… the chip is a simple microcontroller (not powerful enough to call a “Microprocessor” but a hell of a lot nicer than the PIC chips we used a decade ago.

The Atmel chips are fun devices that are (relatively) easy to use and there are several boards available cheaply, mainly from China or Chinese companies in the UK that make this whole control thing worth looking at again. More’s the point there are lots of libraries – some working, some part working out there to save re-inventing the wheel.

The big issue for me has always been the wires. Much better to control stuff via, say, a mobile phone and have the units work by radio… but it has to be CHEAP to be worthwhile.

That is the basis of the project I’m working on right now. On the left you’ll see the first experimental screen of my Samsung S4 project (though it works seamlessly on any Android or iPhone-type device such as a tablet). For the purpose of experimentation, this screen is showing local 4 on/off type controls, 5 remote station on-off/off controls 4 remote on-off controls, time (from the base unit), temperature monitors from the base unit (internal/external) and temperature and humidity from a small unit at the end of the radio chain… but that’s just the start.

Note (June 23) – I’ve made a decision, rather than continue to struggle with space on the master unit – and in order to use the same final circuit board on the master as will be used on the slaves – to eliminate IO control on the master – hence freeing up plenty of space. The master unit will concern itself with handling keepalive signals from the slaves, talking to the Internet and handling thermostatic calculations. After much thought this is the better way to go rather than having special cases all over the place for “local” controls.

The DISPLAY is made possible by software which costs a pound or so called NETIO and is available for Android and iOS. It is a customisable App that works on the phone complete with web tools to develop such panels without programming as such.

tmp4692Essentially, the panel works by sending simple commands from the phone over WIFI or 3G to a web server running what is referred to as a WEB SOCKET – i.e. the basic underlying mechanism behind a web page but without all the extras – after all we’re talking simple commands here – it just needs to be very reliable.

And what and where is the “socket server”? It is software running on an Atmel chip of course – but the server is relatively simple (it’s actually complicated but we don’t need to know that as we just use a freely available library accepting commands and returning text responses). The rest of the code on the Arduino concerns itself with checking temperatures and firing off remote commands by radio to other boards. The App merely puts the end-user gloss on it. The radio is based on something I’ve written about before – RF24NETWORK which never worked properly in the past due to a minor issue in the underlying radio library and my own incompetence in not putting a smoothing capacitor on the radio board. But that’s all covered elsewhere.

Currently I have a board sitting on my bench talking to the App on my phone, over the Internet. On the top right you’ll see an image – I’m using a standard board for the purpose of experimenting – an Arduino Uno board at the back with an Ethernet “shield” sitting on top of it – together with a couple of test-wired Dallas 3-pin temperature-reading chips that look like simple transistors and actually only need 2 wires to work (but I’m using 3). In the foreground is a twin relay board of the type I’m using to control things such as lights or heating system thermostat replacement. I could just as easily use opto-coupled triacs. Note that the final unit has no wiring for LOCAL controls – these are all handled on the slave radio units.

In addition, there was enough room in the master board software to call an Internet TIME service and that keeps the local clock accurate – and also sends the time in the 4-byte time_t format to the slaves. As these have the real time clock software in them – they can be updated automatically by incoming packages – and hence use the time for whatever purpose…. providing time displays on household gadgets etc… one example – I have a SAD light in the bedroom which runs on a software clock only which in time becomes inaccurate – with the simple addition of the radio network and a little software, this when upgraded will keep perfect time.

Arduino with Ethernet shieldNote the yellow Internet lead and underneath (top picture) , a simple plug-in the wall power supply (5v) feeds the lot.

This is only for the purpose of testing and the hardware above probably comes to £30 – but an Arduino clone can be had for as little as £4 or so, while the  “Ethernet shield” will hopefully be replaced by a cheaper Ethernet board which can cost barely more than £4. Currently the one I’m using is around £12 – but this is only needed on your main board! The slave boards are even more inexpensive as all they need is a radio card – and so you can see it is possible to put together something for very little money.

The radio unit at the end of the chain shown below right costs excluding case and USB power less than a tenner. That board is the one supplying temperature and humidity readings in the top display. The blue item underneath is the sensor – it is pointless putting it inside the case as any residual heat no matter how little in the circuitry will affect readings.

The cheap ENC network cards are not as easy to use for “web sockets” because a lot more is left to the imagesoftware (that’s why they are cheaper) and socket implementations to date have been few and far between but thanks to work by Peter Oakes in Canada who’s been helping me with this, it looks like the ENC board is cracked all but for a question of reliability – we have a working socket implementation using code originally written for the Nanode boards. Right now it falls over occasionally so I’m leaving it to one side while I get the rest perfected.

The second screen on the right above is merely a test page showing 6 local slider controls and analog inputs from 5 boards – the last one being located way down the radio network chain at the other side of the house.

tmp66A1So that gives us one card controlled by the Internet. How does that make a home control? The next stage is to have that card also talk to a wireless controller – to talk to other boards. The cost of wireless is similarly inexpensive but very limited.

The NRF boards will typically talk DIRECTLY to 5 other boards (they CAN talk to more but typical libraries out there limit themselves to 5) over the kind of range you’ll see for cheap home WIFI – i.e. not very far and definitely not through more than a couple of walls – what’s needed is a means to network them – and that’s where the software library RF24NETWORK comes in – a simple means to network dozens of these together. It may be in this case that the network is not needed – the fact that the main unit can talk to 5 other radio boards may well be sufficient and if not then simply relaying software from one to the next takes that up to 25 boards and way beyond.

The NETio designer is in touch and has been helpful. Next step is to fully test it on the really cheap Ethernet boards (done that but not yet happy about potential memory leaks). I’ve already had success with the radio boards and the rest is easy. Here are a couple of early blogs on the subject of the radio network… and with links to other resources.

Current prototypes feature:

2 12-bit analog inputs (had 4 but needed pins 0 and 1 to check serially…)

3 digital outputs

3 analog PWM outputs (ideal for LED LIGHTING by adding a MOSFET and 12v supply for the lighting – again a plug in the wall job)

2 DHT11/22 chip temperature inputs (I have implemented both humidity and temperature – could have used Dallas chips for temperature but this seemed like 2 for 1).

You can add as many radio boards as you need and each one has the same facility.

The THIRD screen (left, above) is the thermostat control page – I’ve added the ability to control a single remote relay as the thermostat chip -  with the kind of control (heat delay etc) you’d expect from a Thermostat.

Alongside this I’ve asked the NETio designers if they will consider implementing MQTT – as this adds security – we already have MQTT working on the ENC boards thanks to a Saturday session with friend Jonathan Farmer and a lot of follow up work. For now my security consists of sending a special character string to allow access to other functions – that times out after 60 seconds unless refreshed by the App. Hey – it works!

Current state of the art

NOTE IN THE CURRENT WORK I’ve SCRAPPED IO shown on the master unit – I was determined to stick with the 328 and I was running out of both RAM and ROM. Currently I have enough memory to do the job using the WIZ network card (hence saving RAM)… and ROM I’m up to 26.6k out of 30k available.

The way this works (and I have a working setup on my desk)…. the units responds to commands such as x/OUT1=1  or x/OUT1=0  where x is the unit number in octal (base 8 – hence 0 1 2 3 4 5 6 7 10 11 12 etc)

tmp652BThe word OUT1 on it’s own returns the status of that output – and so on. So 2/OUT1=1 would turn on output 2 on the radio slave “2” for example. In the picture above, lets say the radio board that has a blue arrow to it’s right is radio 2. To handle relaying of these short range radios, that radio can talk to radio “22” as well as 4 other siblings. – radio “22” can talk to 5 siblings – “122” through “522” etc., the further down the chain the more you add on the LEFT of the address. So  522/OUT1=1 would turn on the smallest board on the right, picture above, the 5th one.

So – at level 1 the base can talk to 5 units… next level in it can talk to 5+(5*5) = 30 units. At the next level 5+(5*5)+(5*5*5)=155 units – next level out increases dramatically to 780 units – ANY unit can talk to any other but the parents MUST be turned on (hey what do you want for a few pounds)… I think 4 levels (780 units) is likely sufficient (and polling such a large number would likely be impractical but it does give you a good hopping range given the limitations of the cheap radios) and in my prototype setup I can run a slider on the phone and vary the brightness of a LED on the farthest unit virtually in real time with only a tiny, fractional second delay.

Arduino clone plus radioFor an absolute minimal slave board you just need the radio and one of the small surface mount Arduino clones such as the one shown on the left here. We’re looking at no more than £6 plus power (5v – i.e. any old USB plug-in-the-wall supply available for less than £3) – this needs something like a relay to drive a lamp or heating – and that’s it – clearly if you want more – add more. That whole board is the size of my thumb!

And that’s it – the first fruits of this work will be to put temperature monitors and light/heat control in our rental cottage and our place in Spain so I have access to this and other information on the phone. Implementing a movement counter with a simple infra-red is another idea etc.… I need some boards first as right now this is all a bit too spaghetti.

I’m currently making mods to store in EPROM, settable remotely, the unit number from which to  get the temperature for thermostatic control – and the unit which you need to talk to in order to turn the heat on by a relay. I’ve also clarified my worst fear – RF24NETWORK cannot handle sending out one communication after another while expecting to READ communications… so I’ve arranged that data is only initiated by unit 0 – which then always expects a response back. I’ve arranged a timeout for 0.2 seconds for expected data return before sending the next package out – obviously cancelling that delay when something comes in – with that in place and only with that in place I’m getting 99% reliability out of the radio packages. I’ve just added multiple retries (rarely needed) and with that I seem to be getting 100%.

And now a little history – you can stop reading now if you’re not interested but this might help to understand how I got this far… Once upon a time in the 90s, my business partner (of the time) and I developed a home control project called Appcon which consisted of a small board with various sensors and outputs – a triac (solid-state relay) to control, say lighting or heating, a temperature sensor, a general input and a speaker output. Click these pictures to see larger versions.

1994 PC AnswersThis small board ran from 12 volts and connected to other boards via a 4-core telephone wire.  at one end of the wire was a PC firing out commands, reading data and generally controlling the whole thing with a set of rules.

imageIt was developed on the original Windows 3.1 before the time of widely available Internet and LONG before we could buy cheap stuff from China.

This worked well, in my house in Newcastle I had around 40 of these cigarette-pack sized boards controlling just about everything from heating, lighting, burglar alarm, putting out “cricket” sounds in the garden, you name it.

The problem in developing this was the nature of construction – the fact that the boards had to be wired together was a real issue. In some homes it’s just not that easy to put a wire all over the place to connect everything together. It certainly is not in my stone-walled country cottage today.. I’d get shot!

The interface was good but as Windows matured it rapidly started to date and there was no such thing as a mobile phone interface! Today you would not think of using this (well, I wouldn’t!)

That product was GREAT fun and we sold quite a lot of them, but at the end of the day, home control remained a minority sport and we never did get this into the B&Qs of this world. Meanwhile, particularly in the UK, what’s out there remains expensive. Not too many people want to shell out £30-50 just to turn a light on and off!

The future of Tablets? The Samsung ATIV-Q

Sure, Android has taken the market by storm, particularly in tablets just as Apple IOS did in it’s day – but just occasionally one gets annoyed that favourite Windows programs are not available.. and so now, the launch of the Samsung Ativ-Q. I was VERY impressed with the Samsung S4 and now they’re at it again. Very thin, VERY powerful and amazingly running Windows 8 – with Android 4.2 running as an APP (clever) at full speed (and with interaction between the two operating systems, the tablet offers a stunning 3,000 pixel across resolution. Could this be the future? Instant flicking between the two!!  No pricing yet but do look at the video!

http://www.engadget.com/2013/06/20/samsung-ativ-q-hands-on-with-the-new-windows-android-slider/

Interesting Video about Social Media and it’s Impact

Social Media Impact

Mobile and Broadband Coverage in Northumberland

A short update on a couple of items of interest to those in Northumberland -  and in the case of THREE mobile users, those with coverage issues everywhere.

http://scargill.wordpress.com/2012/10/15/the-three-home-signal-box/