Category Archives: ESP-01

ESP8266 Arduino NetIO Server Demo


This week as regular readers know I was far away from our little home in Spain – in fact I was at MIT (Massachusetts Institute of Technology)  in Boston, helping promote ESP8266 technology (why? because I could).

IvanAlong the way I met up with Ivan Grokhotkov who as some of you know has done a lot of work to bring the Arduino environment to the ESP8266 community.  As part of what we were doing at MIT, Ivan and I decided to put together a simple demonstration – this would use NETIO (my favourite IOS/Android visual interface) and ESP8266-Arduino to help an ESP board drive a servo via wireless control. 

As it happens any old ESP board would do – an ESP-01 would be just as easy, however both there in America and here in Spain I just happen to have the NodeMCU boards handy and so this short blog will show this in action using pin D5 on the NodeMCU – but there is nothing clever about these boards – if you have something different, use it.

NETIO – so firstly what is NETIO? It is a very inexpensive APP for IOS/Android with a free visual web interface – I’ve discussed it before in here with examples. Essentially you create a free account at and having logged in, you go to the online design editor and put together your dream interface. In this case a simple slider to control a solenoid will do – but more of that in a moment. You then sync your phone to the account and the control page becomes available to your device. The point being it is just SO easy to make pretty interfaces.

ESP8266/Arduino – this is a free add-in for the Arduino environment (which in turn is also free) to allow you to compile C++ code for the ESP8266 chips – just as easily as if you were using an Arduino. Indeed with the nodeMCU board you simply plug it into USB, fire up the environment, fire up an example – and you are up and running. I believe the forthcoming (Kickstarter) node.IT boards will be similarly easy to use.

In my case I firstly downloaded the standard Arduino 1.6.5 environment to my Windows PC – I then followed the instructions on Ivan’s GitHub page – everything you need to know is included in his readme file so I’ll just take you straight there.

The example Ivan put together to control a servo is here.

The only thing you have to do to run this sketch is to put in the name and password of your router. Running the program and using the Arduino serial monitor yielded (in my case) this…


As you can see the little board immediately connected to my router which gave it the address of – this actual address will vary from system to system.

The demo program sets up a trivial web server which will work as follows: In a browser I typed and here’s what appeared in the browser…


Check out the example code and you’ll see where this comes from.  The next step is to add an argument to that URL – so let’s try (remember you likely won’t get that actual address)


You can see that the value has been extracted and passed back to the browser – meanwhile in the serial window…


What you don’t see is that on pin D5 of the ESP8266, a constant pulse is being sent out and this pulse WIDTH has been altered. Values 0-180 work and turn the servo from 0-180 degrees.

It REALLY is that simple to drive a servo – in my case the 3-wire DIRT CHEAP servo is a Turnigy TG9e with 3 wires – ground, power (3v3 in my case) and signal (D5 – but doesn’t have to be that pin).

So by firing the argument (value 0-179) as above to that address I can control the servo – how about doing this remotely via a mobile – for now we’ll assume internal addresses so this would only work within the area covered by the router… but I’m sure you’ve already considered world-wide servo domination.

The image below pretty much covers it – in the web browser I dropped a slider into the NETIO main screen.


The only thing missing here is the code for the slider. Click on the slider and…


(I’m pretty sure I should have made that max value 179 but whatever… it works) So I have set the slider to emit code /servo?value={value} where {value} gets replaced by the actual slider position as you run the app and move the slider around – add that to the address in the main panel and you have exactly the same result as we saw higher up this blog entering the value into a browser manually. Hence… here I am moving the slider around on my Android phone – the result in the debug serial window is:


And as this happens the actual servo is rotating back and forth up to 180 degrees. Whether this powers a piece of paper or a ton of concrete is down to the servo!!!

This is of course trivial – the same APP could control a whole HOST of sliders and the ESP8266 code could perform all sorts of transforms on that data – an ESP8266 spider could be fun (I got that idea from the giant cockroach which appeared on the floor while we were experimenting at MIT)! A nice addition to that code might be a timing arrangement to smooth the value change as moving your finger across a phone tends to produce slightly jerky results.  If you were using this for a window opener or other visual toy…..

When I get a minute I’ll do a video of this for YouTube – in terms of development, if you have everything to hand you could have this all running from scratch within 10 minutes, it is that easy.

My thanks to Ivan for bringing the ESP8266-Arduino environment to my attention – I talked about this ages ago when it was new but at the time was not that impressed – today – hell yes this is a very easy way to get quick results with ESP8266. If you have any code issues, Ivan is really approachable.



That’s it for now. Just back from the states to our little village and I’m thinking a pint is in order..



This coming Monday/Tuesday/Wednesday I’ll be working with Espressif CEO Swee-Ann TeoJeroen Domburg, Ivan Grokkhotkov and others at FAB11 in Boston – demonstrating how to use the ESP8266 chips in home control and other applications.Heading off from Alicante first thing in the morning tomorrow (Sunday) and arriving late afternoon in Boston. Should be quite an adventure.

As well as talking about the technology and helping others I’m hoping to learn a lot more myself about these chips and of course I’ll be spending time armed with camera, checking out the many seminars and exhibitions at FAB11.  For pics and info…. I’ll be updating this blog occasionally – tweeting at @scargill (#fab11) and on the Facebook page esp8266wifi



Just a quick note – as a few of you have said you’ve had issues flashing ESP boards and some have worked then not worked etc… ensure you use the relevant pull ups and pull downs and the right voltages.  As you know I do a LOT of work with ESP-01, ESP-12, ESP-12E and I can say I have NEVER broken one!  So look for other problems than broken FLASH I suggest??


ESP8266 Reliability and ID

Questions for you C programming ESP8266 guys re: the ESP SDK (1.2.0) and some possibly important info on WIFI and MQTT reliability.

In the SDK:



HOST ID….  If you do an IP scan on a windows network – ESP units will coming up as IP addresses – and Manufacturer ESPRESSIF…   there is a WIFI HOSTNAME GET function in the SDK –  but not only can I NOT get it to do anything, the corresponding GET function returns something like ESP_945645  – and not what I put in and NEITHER are showing up in the Windows IP scan – so the question is… where are you supposed to use the SET function in the SDK to replace the info that comes out of the GET function – and why isn’t any of this showing up on a Windows scan.


I discovered that if our software was running happily on the ESP12, happily talking to MQTT, if I then disconnected the WIFI and router for a few secs and reconnected…… DEAD. The board would respond to serial but it had LOST the WIFI number.

No amount of waiting would fix this – I wonder how many people have NOT twigged to this – well, if not – this could be your lucky day. The reconnect policy set to 1 will ensure that within a few minutes, the IP will reconnect.. and of course you can CHECK this!!!

So the next step was to see if the IP had been reconnected and call the MQTT reconnect routine – after all, you’ve lost all your MQTT subscribes at that point.

Then it occurred to me – there might be other circumstances – the NODE-RED might have gone off for a while – could I be sure I’d still have a connection – so finally we set Node-Red off sending a “heartbeat” message to all of the units (by missing off the unit name and just having a topic of “toesp” on our boards, you send to all) every minute – and that resets a timer – so then in the above routines – if the WIFI is active and the timer has timed out – once again reconnect to MQTT.  I checked the stack and this does not suffer – so fingers crossed that will be just fine for the odd occasion that this is needed.

Hence reconnect without rebooting and all the horrible relay flicking results of that!!!  I’m quite happy – a beer, I think. The repository is updated and I have boards testing now.

If you are using MQTT and C on your ESP8266 boards, you might just want to ponder the above and if you have an answer for my query at the top, please do let me know.


Hackitt and Bodgitt discover Healthy Lighting

Hackitt and BodgittThere has been a lot of talk in the press recently about the colour of lighting and it’s relevance to health.

As far back as 2012, the BBC reported that NASA were to test “space-sleep colour-changing lights” on the international space station and since then there have been a number of articles in the press suggesting that the colour of light at certain times of the day can affect everything from sleep to prostate cancer.

Of course newspapers have also claimed at one time or another that aliens landed on the moon but there is a certain amount of obvious logic in selecting the right colour for the right time of day. Everyone knows that due to atmospheric filtering, the sun appears shifted toward red at night – and it is quite feasible that our bodies respond to those colour changes. No, I don’t follow my star signs!!

KelvinRecently the papers were full of the news (as it there’s nothing more important going on in the world) that our lighting and our various devices such as tablets can affect our sleep and that you should avoid blue light in the evening and that light shifted toward blue in the morning can aid waking up etc. I subsequently found, thanks to readers, a boatload of APPs that do this colour shifting on phones – not a lot of use when you’re trying to take a photo and you’re mobile has gone all red, but probably a good idea when you’re trying to read yourself to sleep.

And with that in mind I set off trawling the web for more information and finally ended up with a set of RGB values which match “colour temperature” in degrees Kelvin. Now I would not be one to claim 100% accuracy here because for that to work with modern LEDs you’d have to be assured that the RGB values you poked into the LEDs would render accurate colours and I doubt very much that this is the case.  However, I’m going to suggest that what I’ve done here is “near enough”.

In our “Hackitt and Bodgitt” board described in the blog article “Home Control 2015” I’ve added a new command to provide white light from RGB LEDs which can be “shifted” by providing a value in degrees Kelvin from 1,000K (very orange-ish) to 40,000K (very blue-ish).  At 6400K you have a perfect representation of the more garish compact Fluorescent lighting and at 1,100K you have a pretty good match for dim candle-light – with all the subtle variations in between.

Hence with a simple command to alter the hue of white lighting, in our case initiated no doubt in a node-red command, we gain the ability to alter the lighting to the time of day.  Now, WHY would you use more expensive RGB lighting as against simple WHITE LED strip in your home of the future? I have a theory about white LEDs and it’s a theory increasingly backed up by anecdotal evidence that white LEDs don’t last as long as some manufacturers might have you believe. RGB LEDs on the other hand don’t seem to have that problem and have the benefit (we’re talking about modern integrated LED, not separate red, green and blue LEDs) of allowing just about any shade of any colour and in this case, any shade of “white”.

I doubt very much that due to this new lighting interest you’ll read “Peter Scargill becomes the first person to live to 150” but you never know, it might just be an aid to better sleeping and that has to be a good thing.


Accessing ESP8266 Arrays in FLASH

A new challenge – the answer to which I will post in here.

Accessing an ESP8266 C array in FLASH – i.e. never touching RAM until you actually access something.  One suggestion was to ensure you use 4-byte variables.. so I tried this – it compiled – the array is in FLASH – but the access IMMEDIATELY reboots the ESP8266. iprintf is just a macro for printf – no issues there..

static const uint32_t petes[] ICACHE_RODATA_ATTR = {

iprintf is just a macro for a normal printf function…

iprintf(“Data= %ld”,petes[37]);

Result: Immediate reboot….

Thoughts anyone? Why do we need this? Because FLASH is in plentiful supply on these boards, RAM is not.  Ok, you could use tricks from the web page lookup and make functions – but that involves knowing exact addresses etc.. If possible I’d rather let the compiler worry about that.

I’ve posted this on the Espressif forum – I’ll post a working solution in here – but I’m out of ideas….


Lua Revisited

Back in the dark ages when the ESP8266 was something new, I had a go at the nodeMCU software, essentially implementing the Lua language (or a subset of it) on the ESP8266.  At the time I was not familiar with Lua but thanks to some of the excellent resources out there on the web, I quickly caught up.

Sadly, my expectations exceeded the ability of the implementation to match and every time I went past flashing a few lights – CRASH. I gave up in disgust.  I’m not complaining as that gave me the impetus to learn the hard way how best to program these chips – which I maintain is using the C language – and in my case via the unofficial development kit which is just an excellent piece of work by Mikhail Grivorev, leading to a complete Windows environment with a ton of examples. Between that and TuanPMs excellent MQTT code as a starting point  I’ve been happily programming away and even built up a working relationship with Espressif in the process. My home control project is well under way and now just waiting for some new boards – and a function that Espressif have promised to shut down PWM.

LuaMeanwhile this morning, I can’t remember what I was reading that triggered me off, I decided to have a quick look at the node-MCU code again.  I had it sitting in the development environment but figured it would be out of date and so went off to the repository to study any changes – which I duly put into the code. The GIT Makefile is useless for Windows but fortunately there was one in the environment – and as it happens, a good one. I wasn’t about to go backward so I made sure I was linking to the very latest SDK which at this time is 1.1.2 with at least one patch – all available from the Espressif forum.

I checked the settings in the Makefile, adjusted the SPI speed up to 80Mhz, set the FLASH size to 4Mb to suit the sparkly new ESP-210 that is sitting here looking for a job – and started the compile process. Within a minute and without any issues, the code was flashed. I have to say I was expecting at least one or two issues – but no.

I set up a 9600 baud terminal, in the process discovering for the first time that you can have multiple terminals at once in the Eclipse environment – handy (it would be more handy if it always reported the right location of bugs but you can’t have everything). Sure enough – a working Lua board.

I told it about my WIFI setup and sure enough it grabbed that too.  The project has come a long way since the early days and now (apparently) supports json, a file system, timers, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpiio, wifi, adc, uart and more. I’ll believe all of that when I see it.

I was delighted to see that both the integer and float versions are available – that’s a plus! I also noted support for the WS2812b chips – I noted that as I had to make a change to the source code and I’m delighted to note that they’ve made use of a local compiler directive that I was not even aware of to ensure accurate timing. Wish I’d seen that a month ago when I started work on my own WS2812b support. 

WS2812B LEDs on nodeMCU LuaI plugged a (short) length of WS2812b strip (a custom board we had made) into the ESP-210, fired the instruction and… worked first time out of the box. Impressive.

I’m pleased to see the nodeMCU docs have come on a long way – sadly the website was running like a DOG at the time but here’s a few stats I managed to get off the board.

print(node.heap()) returned 20kbytes which is more than when I tried it – but then it used to die when the heap got down to 5K so we’ll have to wait and see how reliable that is.

3 Channels of PWM are available – at 10 bit resolution. Clearly some work to be done here as Espressif have just released a library handling more outputs than that at nearer to 14 bit resolution – and it works as I’ve been using it in my own research and projects. However – handy enough.

There are libraries for sending to displays complete with fonts – now that I’m interested in – if this will do reliably MQTT and report results to a display I have a use for this already – wall thermostats.

So, overall, a very brief test – it is possible with the right environment to get interactive and easy to use nodeMCU up and running in minutes – you don’t need special boards and the software is free. Can’t be bad – how complex a program you can make withou running out of RAM remains to be seen. Here is some more info on nodeMCU.


A new Day – A new Board

New dayWhile waiting we’ve done ourselves a new board – it’s not too obvious but under that power supply / dc/dc convertor area – are three MOSFETS for PWM use and we’ve added a connector so you could stick some RGB serial LED strip straight onto a connector.

GPIO16 is on there and on this particular board we have NOT shorted GPIO16 to reset so it can be used for purposes other than for SLEEP mode! The board is designed to take ESP-12s (or ESP12-E ignoring the new leads) but at a pinch could use an ESP-01 if all you wanted was a RELAY output and a sensor.  Before anyone asks – with the DC/DC convertor, the board with no lights attached uses around 100ma with 12v in.  The Tiny85 is there for interest only so some of those parts on the left would normally not be used. In a couple of of weeks we should have boards and get one built up – but 95% of the layout is already tested. More details on the main Home Control 2015 page.


New Board


More ESP8266 Woes

Updated Sunday 22/06/2015 : The thing about the ESP8266 is there is always something new to discover problems with!!

So this week –

The new PWM module appears to have issues and as we don’t have the source, we can’t fix them.  Right now it will only do up to 90-95% duty cycle– and once it starts up there is no way to stop it – that is, attempts to use tight code for, say, serial LED control fail because of the interrupts going on in the background – I’ve asked for fixes and a stop routine. Started a new version of our board with 3 MOSFETS on it in anticipation of getting this working.

SNTP – the sntp routines work – you can get the time from a server – but if you add in from the SDK documentation, the bit to set the time zone first – it will then NOT get the time.  A better example is needed. The default time routine gives the time not at GMT – but GMT-5 ?!!?!?!   So you need to add the number of hours for where you are (*60*60). That leaves just the summer/winter correction which is a whole other ball game.


ESP8266 Debug

Light bulb momentsSo – I’ve learned quiet a lot in the past few days..

Watchdogs on the ESP8266

Since Espressif SDK 1.01 or thereabouts, watchdog timer operation seems to have changed – delays of any length even in init()  are out – or so it would seem? So I got onto Stanza Wang, the business Development Director of Espressif Systems who as always was most helpful and got a quick reply from one of the development engineers on this subject.

It turns out they’d prefer the use of these function:

void pp_soft_wdt_stop();    // close software watchdog
void pp_soft_wdt_restart();    // reset software watchdog

So for example use void pp_soft_wdt_stop();    before your delay and then use void pp_soft_wdt_restart(); 

Delays are not a good idea full stop if you’re using background processes such as WIFI – but you might not be!

Maximum use of FLASH

Thanks to Richard Burton, it is now possible to start programming right down at the bottom of memory – leaving LOADS of FLASH free (though iRAM continues to be a precious commodity – someone needs to do a “how to” on saving RAM. I’ve blogged on this elsewhere with a link.

Debugging and those Pesky Messages

ESP12-EAnd now…. debugging.  I have had it up to here with odd strange output from the Espressif SDK. TuanPM implemented a simple macro called INFO for outputting to the serial port for debugging and general information – but there was no way to control what came out – worse, the SDK kept throwing status information out which wasn’t wanted – so we’ve written our own.

If you’re outputting info to the serial line the best way as we’ve found is to disable Espressif’s messages using


in your user_init function. This turns off all output to the serial – unfortunately that means that os_printf() no longer works – so you’re dead in the water. We wrote our own printf equivalent for the serial port and we call it iprintf().

You don’t HAVE to make it this complicated… if you want you can simply, having disabled os_printf() use ets_printf() where you want text to the serial port – but we wanted more control.

Here are a couple of definitions – bit masking – you might choose to make your own.

This is in our DEBUG.H file

extern int enable_debug_messages; // Needed by debug.h

#ifndef USER_DEBUG_H_
    #define USER_DEBUG_H_

    #define DEBUG 1
    #define INFO 2
    #define RESPONSE 4

#endif /* USER_DEBUG_H_ */

So somewhere in the init, set a variable (see above) to the level of debugging you want….

//int enable_debug_messages = INFO | DEBUG | RESPONSE;
int enable_debug_messages = INFO | RESPONSE;


And here is the function to use that…

void ICACHE_FLASH_ATTR iprintf(uint16_t debug_type, char *fmt, … ){
  char buf[128]; // resulting string limited to 127 chars inc arguments – change if you like
  va_list args;
  va_start (args, fmt);
  ets_vsnprintf(buf, sizeof(buf), fmt, args);
  va_end (args);
  if (debug_type & enable_debug_messages) uart0_tx_buffer(buf,os_strlen(buf));

So basically you have a function you can call with a debug level..

iprintf(INFO,”Starting up the program”);

You can pass parameters to it just as you would with printf….

iprintf(RESPONSE,”The answer is %d”,answer);


Depending on the level of debugging you want you could expand on this greatly.   Someday we’ll convince Espressif to optionally turn off that start-up 78k rubbish and we’ll have totally clean output – for now this is a great start.

A shame you often have to hunt around for this stuff!



As often as not, when I reset an ESP8266 by using the system reset call in the SDK, after flashing – for example.

if (strcmp(token, “id”) == 0) {
                    if (isQuery) RESPONSE(“ID=%s\r\n”, sysCfg.base);
                    else if (strlen(strValue)>=2){
                        strcpy(sysCfg.base, strValue);

The unit does not come out of reset.. the internal startup code will run, maybe there will be a reference to the watchdog – and that’s it. This usually happens after FLASHING the chip.. ie you flash it, you run the code to reset – it won’t.

But otherwise the system_restart works fine.

Anyone have any idea why this should be and how to get around it. I’m not the only one to mention this?