ESP8266 Now Working/Updated

tmp4A50Getting this pesky ESP8266 working has been one of the most frustrating things I’ve done in recent months. Thanks to a combination of newness, just plain WRONG information out there and documentation slowly and sometimes inaccurately being translated from Chinese – not to mention software bugs, I’ve spent several evenings getting myself annoyed at this new and interesting WIFI product from China, until last night when it finally all worked.

For anyone looking in here who is wondering what this is about – I’ll explain – for those looking to fix something – skip to Cheap WIFI. For instant results (like maybe you don’t think the board works as I didn’t – check out my short video but do come back here.. http://youtu.be/08RlcuyeKSI )

The ESP8266 is a VERY low-cost tiny WIFI module for embedded systems. Anyone familiar with, for example Arduino will know that just to open a remote web page via the Internet will cost you most of your code space and almost all of your RAM memory.

I will refer to Arduino here as that’s one of the cheapest and most popular ways to do embedded processing – in fact I use a chip called the ATMEGA1284 and build my own supporting hardware as this chip has more memory and more IO than the standard ATMEGA328 – but I’ll argue here from the perspective of the standard product.

Ethernet Card: A typical Arduino installation of Arduino processor and standard Ethernet card will see at least half of your available FLASH memory used up to simply open a web page. The Ethernet card will cost you between £10 and £30 depending where you buy it from.

EN28J60 Board: These are the cheap end of the Arduino Ethernet spectrum coming in as cheap as £4 but with another price – the chip is fairly primitive meaning you need a lot more software in the Arduino to make it all work. There are a couple of libraries, i.e. Ethercard which is frankly awful and Arduino-UIP which emulates the standard Ethernet library but who’s client software is unreliable and either way you lose most of your RAM memory and again end up with less than half of your program space left. This board needs 3v3 to operate.

WIFI Solutions: The above boards assume you can hook up a wired Ethernet connection. There are also WIFI solutions varying from solutions as above with WIFI added to the rather novel idea of having an intelligent board you talk to serially using AT modem commands. The latter have the advantage of dramatically reducing the amount of FLASH and RAM memory needed – but the downside has always been the cost. By the time you’ve finished you end up with a product that is just too damned expensive and you may as well have cannibalised an Android tablet.

Cheap WIFI: Along comes the ESP8266 – a board from China, using a 32-bit processor with on-board aerial, requiring a few simple commands to get a WIFI connection and a cost as low as £3 – this is potentially THE BUSINESS.  Out there very many people have ordered, very few have results – and is it surprising? Most of the examples of code are based on one or two originals and are just plain wrong.

Typically you will see code that shows a connection to your router – and time and time again when the response “OK” comes back  the articles will suggest you have a connection.. NO, NO, NO. Not in the early versions of the board’s software.  The board features the ability to look at the IP address it has been assigned – and if that does not respond with a valid address, you are not connected.  When actually setting up the AP and password the board will come back and say OK to any old rubbish provided it is formatted properly. You NEED to ensure you have a valid IP address. Up to now I can’t see how to set this manually – that in itself is an issue but one thing at a time.

It LOOKS like (still testing) the latest version of the software (which will likely NOT be included in the ones you get from the factory) now comes back with a useful indication of success or otherwise when you actually try to connect. By latest version I mean..

AT+GMR  returns this….    0018000902

See lower down as there is more than one set of software bearing this version number.

I’ve spent nearly 2 weeks using a version that simply doesn’t work properly – and only now have I found an article that actually tells you firstly how to get the software up to date but also potentially how to keep it up to date (that bit doesn’t work for me yet ) and even then, some of the responses from the unit are distinctly poor English… like this one.. when sending data to a website for which you don’t yet have a connection, the board replies “link is not”.  I think we’re a long way before this can be described as “the finished product”.

Some time ago I made myself a little PHP page to return the time along with lighting up times for my area – this is for a home control project.  I  did this because NTP time servers are notoriously busy and just not something you can count on and then if you want lighting up time – you have to write some code to work this out.. Would it not be great, I reasoned if I could offload this work to my own website and those of a few others (preferably using other providers) for backup.

The web page returns the usual header stuff and this simple statement…

{time=1412468381;timestr=01:19:41 05-10-2014;dawn=06:42;dusk=19:09}

In there you see the time in a format suitable for Arduino real time clock software, you see readable time and you see dawn and dusk. By adding parameters to the page call I can make it work for any area in the world. LOVELY but for many projects, like my wife’s SAD TIMER, adding an Ethernet card and an Ethernet cable to the project is a no-go.. hence my excitement at seeing the EAP8266 board. for a nominal sum I could add time facility and do away with all that display and keyboard nonsense necessary just to set the time!! I could even develop a nice web interface to the page for setting different days etc. and return that in the string above…. the sky’s the limit.

But no, try as I might, I spent over a week trying to get this little board to talk to this simple web page.

Last night (or technically this morning) that all changed and it works. I’m documenting this to save others a LOT of wasted time.

So, you have your little WIFI board and I’m hoping you have an FTDI board, if not, get one – in other words a USB serial convertor used for programming some types of Arduino and talking to devices over serial from your PC. They cost a few pounds only.

Read THIS article and it will show you how to talk to your new ESP8266 board AND how to update it from a file (link is in the article) and potentially from that point how to update it automatically via the web. THIS article was the turning point as it correctly describes (for my board and note not all boards are identical) the use of the control wires often ignored elsewhere.

http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now/

This article http://hackaday.io/project/3072/instructions shows a diagram of the board itself and was particularly helpful – I do indeed use the CH_PD wire, the example suggests a 10K resistor holding it to 3v3 but I just shorted it to 3v3.  For boards like mine – NOTHING works unless you do this. You can’t send or receive AT type commands to the board if you do not connect up this wire.

So we’re talking 5 wires for normal use… 3v3 and ground, signal in and out – and the CH_PD wire held to 3v3. IN ADDITION if you want to program the board itself, using the software in the first link, you’ll need to hold GPIO0 to ground and release when done.

With those wires and nothing else connected, I’ve managed to upgrade the board almost to the version mentioned in the first link – but more importantly, it now works. Once you have selected your access point (AP) and settings and successfully achieved an IP address, you can turn the board OFF – back on and it needs NO code to get hooked up and get that IP address again as the details are stored in EEPROM.  Sadly you cannot easily test all of this with just a serial connection to the PC unless you have a fairly advanced piece of modem software (unlike the Arduino IDE serial monitor) because by the time you’ve connected to a page and then started typing commands to it, the thing has timed out (see later-  you can now control the timeout).

So a combination of setup and testing with your PC and then some simple Arduino code and you should be up and running in no time.

Oh, wiring before I forget – I’ll assume you have a 5v Arduino and this thing needs 3v3. Note that you really do want a decent amount of power at 3v3, maybe 300 ma or more – check elsewhere as I’m not sure the actual requirement but I suggest for experimenting you ensure plenty of power is available. In my case my own 1284-based boards have a 1amp 3v3 regulator on-board so power is not an issue.

Things to remember… this little board can talk to web pages – and you may find yourself taking in more than 64 characters at once – why is that important? Well most of the writers out there seem to have missed this – the Arduino can only take in 64 characters into it’s internal buffer before throwing the excess away!! The existing Serial.wait() function call that some writers out there advise using – falls fowl of this limit – so I wrote my own. The limit incidentally is set in software in the Arduino and you can go poking a file and fix it – I’ve written about that elsewhere – but of course in the standard Arduino you only have 2K of RAM so you don’t want to get too ambitious – but my preference is for a 256 byte buffer.

Ideally you want to wait X amount of time for incoming characters until you see a string – like “OK” – and THEN wait another Y amount of time in the loop just to get rid of any rubbish that might come in after that, returning a result and a nice empty buffer. That’s a fairly easy thing to write and you can test it with the PC serial port and a standard Arduino. So for modem use you might say  mySerialWait(“OK”,5000,200);  or wait for up to 5 seconds for incoming data – if you get nothing, return after that time with a FALSE response, if you see “OK” in there, scrap the original delay and  hang around for another 200ms and come back with a TRUE response.. Well, that works for me. I’ve since added parameters to optionally store useful info in an array and have a working “light on, light off” demo as per the video referenced elsewhere. That will soon turn into a fully blown SAD light controller.

I am experimenting with code that returns the information from my web page and as yet does not do anything with it – the TOTAL code (no libraries) is just over 3K – compare that to ANY other Arduino Ethernet solution.  Something in the back of my mind says this serial approach is going to have some severe limits – but already it is the solution to my wife’s SAD light for very little code!  I can make a really sophisticated web page, store all the settings from, say a mobile phone into a web page (SQL database) and every now and then the little SAD light can poll the page to get all the updated info, ready processed for use. Lovely. Well, that’s the plan.

Today I discovered THIS link http://www.electrodragon.com/w/Wi07c

This has commands not featured in other lists – including CIPSERVER. I need this to serve up a TCP socket!!

So how do you do this? I set up a little socket communicator on my phone as a socket talker – with 2 buttons.

On the WIFI board I set CIPMUX=1 – you have to do this on power up unlike the rest of the stuff – if I was to set up a client to a web page I’d set that to 0 but this CIPSERVER command will fail if you do that.

Bear with me. I set up AT+CIPMUX=1 then AT+CIPSERVER=1,4000   that means start a socket listen – on port 4000. It says ok and sits there.

I press a button on my NETIO setup – which is pointing to the WIFI address of the board – and set up to send to port 4000.  Magically – the board responds…

+IPD,1,4:YES

and the button on NETIO remains sunk it. That first figure is 1 is the ID of the command….

I send AT+CIPSEND=1,4   – which say I’m going to send 4 characters back – to ID 1 on this socket.   I get a little > in the response window..  I type nothing more than OK in the terminal window and hit ENTER

> OK

SEND OK

As if by magic, the little button on my NETIO Phone app lifts up – all is well.

But after a while I get “unlink” – that’s my phone timing out – bring the phone back to life and I can talk to the WIFI unit again with no further action needed on the WIFI link… AT+CIPSTO? says 180 seconds timeout but it’s kept on working WAY beyond that…

Not sure yet what I need to look out for in terms of the thing timing out… but right now – I’m THAT far off having a working controller APP on WIFI!!

I do with they’d fix the error message “no this fun”

Update: the software version you should be looking for is this…

0018000902

HOWEVER if you got that from the CLOUD update – it is NOT the same as the later 922 version which still sadly calls itself 902 and reports the same but in fact NOW defaults to 9600 baud… and will let you use stupid high baud rates as well as new commands. See these docs – the first is how to update – and the second at the bottom has the actual latest update, the third has the pins!

http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now/

http://www.electrodragon.com/w/Wi07c

http://hackaday.io/project/3072-ESP8266RetroBrowser

If you want to read all about what started this off – ie my home control stuff – check out www.scargill.net

Update: Two more things – I now have the board running as a socket listener – which is ideal for, say pressing a few buttons on your mobile phone to turn things on and off.  I’ve discovered how that works – and how it doesn’t.

I’ve been doing some further work with the ESP8266 – as I want it to act as a Socket listener.  Check my posts to ensure you have the latest firmware because without that you won’t be able to use the command AT_CIPSERVER.
Armed with that update, in order to use this mode, you must first set CIPMUX=1
i.e. AT+CIPMUX=1
Then to activate the listening mode for, say port 4000, you should use this.. AT+CIPSERVER=1,4000
The “1” is for start.. 0 for stop is supposed to work but I found that needs a reset…  once in that mode the ESP8266 will reliably STAY in that mode..  I wrote a little code to work with NETIO software on my mobile phone so I could press buttons and have the Arduino talk to me – when reading this code bear in mind I have 2 serial ports on the 1284 chip…

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

void loop() 

{ 

Serial.println(F("Changing mode")); 

Serial1.println(F("AT+CIPMUX=1")); 

SerialFinder(Serial1,"OK",4000,200); 

char PROGMEM cmd[]="AT+CIPSERVER=1,4000"; 

Serial.print(F("Sending ")); Serial.println(cmd); 

Serial1.println(cmd); 

SerialFinder(Serial1,"OK",3000,200); 

while (1) 

{ 

   if (SerialFinder(Serial1,"+IPD,0",1000,20)) 

        { 

         char PROGMEM cmd2[] = "OK\r\n"; 

         Serial.print(F("Sending AT+CIPSEND=0,")); Serial.println(strlen(cmd2)); 

         Serial1.print(F("AT+CIPSEND=0,")); Serial1.println(strlen(cmd2)); 

         Serial1.print(cmd2); 

        }  

} 

}

SerialFinder (I wrote myself, trivial, waits for the string and returns true or false after a further delay – that simple).
So – it all works a TREAT… it’s waiting for “+IPD,0” which just so happens to be what comes back in this case, might be different in yours, followed by some info on whatever key you press on the phone – I’m not ready to start parsing info yet, just testing reliability, so no matter what it sends, I send back “OK”.
Now all of this works REALLY reliably. However, if I press buttons on the phone too quickly – the board at some point returns “busy” or otherwise locks up. It’s pretty hard to replicate and happens only occasionally but trust me it DOES happen and resetting the processor does you no good most of the time.
You need to reset the ESP8266.. and there is a reset line for that. It will re-establish the connection to your router etc and will simply need you to re-send that CIPMUX command and put it back into server mode.
So now our simple serial chip is not quite so simple – needing RESET and CH_PD pins (CH_PD is on my setup permanently hooked to 3v3) and GPIO_0 if you want to go flashing the board – which is easy – see my blog).  So if you want to reset the board from your 5V Arduino you will need a resistor divider pair just as you did for CH_PD. Actually as reset is hardly a high speed effort – you could use a 4148 diode pointing to ground… I’ve tested that and at least on a rig it works well.
As far as I can tell, no matter what goes wrong, the ESP8266 recovers if you use that reset pin. More as I learn more. It would appear that NURDSPACE site now has now been updated to include the CIPSERVER command but still claims the unit runs at 57k baud. Last but one version actually ran at 115k baud and latest defaults to 9600 baud and you can then set whatever speed you like, right up to 512k baud.

Advertisements