In a Far Distance Galaxy

A long, long time ago in a far distant…well, world, when I was pretty much using Arduinos for everything, the ESP-01 came along and my friend and colleague Aidan made a little board to handle a relay, an LCD display and.. an ESP-01. At the time we had no use for it, just seemed like a good idea.

Arduino board with ESP-01Today thanks to the likes of Tuan for his work on MQTT and the guys responsible for getting the ESP environment working on Windows via Eclipse (not to mention Espressif themselves) it is very tempting to think that we no longer need Arduinos.

When I say Arduino I mean of course any board based on the Atmel chips. In this case we used a 1284 chip which is an Arduino on steroids but completely compatible. Way more pins, 16K RAM, 128K FLASH – lovely. But as with most Arduinos, getting them to talk to the Internet is disproportionately expensive in terms of available FLASH and real cost.

Well, it was. Thanks to Tuan once again, we now have a way to merge the two. When I was talking to him a LOT about the MQTT software (as I kept finding bugs) he mentioned briefly that he was working on a serial protocol called SLIP so that the ESP boards could communicate efficiently and reliably with the likes of Arduinos.

Well, this evening I decided to revisit this and went off to his Github site here. You may be familiar with his esp_mqtt software which is superb and which I use as the base of my various ESP-12 boards. Well, in that link you’ll find another project of his called espduino. I suggest you take a look.

In essence there are a couple of binary files you blow into an ESP01.  The ESP-01 talks to the Arduino via serial and in the case of lesser Arduinos, that means talking via SERIAL (the only serial port) while doing debugging via a software serial port. Well, the `280 and 2560 based boards have more than one serial port, so I’ve taken his Arduino code and swapped things around… Serial1 for the connection between Arduino and ESP-01 – and Serial for debugging.

So basically once you’ve dumped the binaries into the ESP-01, you connect serial1 from the Arduino (in his example Serial) to the ESP8266 serial – taking into account any level shifting – best to run everything off 3v3 then you don’t have to worry about it.

He shows a wire from the ESP (CH_PD) to D4 on the Arduino but then doesn’t use it so tie CH_PD high as usual.

Run Tuan’s library examples on the Arduino and I can tell you that they just run – they work without issue – I’ve had the board send thousands of messages off to MQTT-Spy. The minimum setup takes in total around 9k, that’s including MQTT -  this is WAY better than anything you could achieve with Arduino Ethernet boards which, as far as I’m concerned are now dead in the water.

In my case we had this little board with the option to add an ESP-01 – took me minutes to get it working – I’ve now imported my command parser from the ESP-12 projects (they’re not that dissimilar if you are using C)  and now I have a board that asks my PI for the time (it’s in the blog somewhere – MQTT request – all my boards know and maintain the time, updated from a Pi on power up and every day otherwise) and can turn a relay off using the same protocols I discussed here.  All I need now is a boatload of libraries and if-then-else blocks to pad the board out into something really useful.

So why add this extra level of complexity?  Well, I use serial LEDs and PWM based lighting and the ESP-12 does NOT do PWM well AT ALL. If you control serial LEDs and say they are slowly fading out – any PWM in use fluctuates slightly. In addition I have serial LEDs working almost perfectly on the ESP8266 but every now and then there’s a timing-related flicker – almost never but you know it is going to do it just as the lights are fading out as you are going to sleep.

The Arduino on the other hand does multi-output PWM flawlessly and there is the great FastLed library for the serial LEDs – not to mention the ability to store data in FLASH easily which means that LCD fonts are easy to implement… and there are many LCD display libraries out there. The combination then is a matter of sense – and let’s face it you can get a bog-standard Nano Arduino from China for under £2 and the ESP-01 for WELL under £2 so using both together is hardly going to break the bank.

So there it is… that’s my Sunday offering – already the board is getting the time and controlling a relay… it won’t be long before I turn it into a kitchen sink.


17 thoughts on “In a Far Distance Galaxy

  1. And along the same lines, another ESP8266 board, this variant with (promised) FCC approvals (but only a chip antenna, by the looks of things) and a nice carrier board.  The neat part appears to be the software/firmware side, though.  Over-the-air updates.  A browser-based editor for programming.  Arduino IDE is available too (for those one or two people out there who are familiar with it ).  REST API.  Fully Open Source.

    Not the cheapest alternative out there, but if all the promises are fulfilled, it should be one of the best integrated.

  2. Hello Pete,
    Very nice, I use a similar approach for the same reasons, ie: the lousy PWM
    on the ESP. In my case I use the Arduino as an i2c slave driving the Atmega328p
    PWM and other I/O via i2c commands from the ESP. This has the great advantages to me
    that I can use serial on both chips to debug, and still run the system.
    Both the nodemcu and Arduino i2c libraries are pretty easy to use, so this works well
    for me.

  3. Do you know where is MQTT processed? On Arduino or ESP8266? The reason I ask is because what I want is to offload Wifi/MQTT processing/retries on ESP8266 and just let Arduino send the commands and get the status updates. This way make efficient use of both processing powers.

  4. Hi Niraj - the MQTT is handled by the Arduino - right now TUAN is not doing a lot with this software - but I have to say it works reliably. The overhead of the WIFI commands and the MQTT is much less than say using an ETHERNET card so it's not that big a deal. The alternative is to write your own SLIP setup... On our board we're also allowing the Arduino to control the reset line on the ESP so if all else fails you can reset it.

    Up to now I have the MQTT, an LCD colour display driver with fonts, Serial LED control, a large input parser which is FAR from efficient but works - and I've used 38K of code. With the Atmel 1284 chip in this case, I have 130k of code so I've not really made a dent in it. The MQTT and WIFI on it's own tool under 10K.

    Sure, if I had a list and Tuan had the time I'd suggest putting MQTT over there along with remembering everything reliably, I'd put the real time clock in there (it is in his plans), web page access for setup... and so on and so on...

    1. That will be easy, who want easy 🙂
      At the moment I do have ESP with MQTT and Arduino running custom code. I have offloaded most of the logic on node-red including when I want to read the sensors but all the codes are not so clean. What I want is Firmata Library kind of thing on Arduino (, MQTT on ESP, Communication between Arduino and ESP is SLIP. Arduino monitoring ESP (reset when needed), ESP monitoring Arduino (reset when needed) and node red makes sure it is receiving heartbeat on a regular bases. This allows me to make my devices dumb terminals and may not require RTC. Code on all of them can be generalized. Node red decides and lets device know what and when to send the data. Downside is they are not standalone but as there is no memory card, they are useless without network connection anyway.

      Do you think it is too much to ask?

  5. Up to now Niraj I've not looked at the source for TuanPMs slip code but assuming it is not too convoluted, checking the Arduino for a pulse should not be that difficult.... and as GPIO0 and GPIO2 are unused in this application, they could be pushed into service. Mind you - I think that's overkill because I have NEVER known an Arduino to fail beyond the point that the watchdog can recover it. So assuming you have a watchdog on the Arduino and the ability to reset the ESP I would have thought that would be solid enough. As for Firmata type controls of the Arduino over Ethernet.. I don't think that's really an issue either. Personally I'd keep the data in JSON format.

  6. Hi Pete,
    Starting to test out espduino library. Is there a way to know the chipid of ESP from this? Wanted to automatically assign station id based on the ESP8266 chip id as I think they are unique and has worked for me in my other setup.

    1. Yep, that would be very cool. I also need something similar in order to make some kind of network discovery.
      If I could retrieve IP and MAC address and having opportunity to broadcast these information using UDP broadcast ... that will be great starting point for making network discovery protocol.

  7. I ran into the ESP PWM and ADC problems last week. My biggest problem was that I needed to read 2-3 analogue values from sensors. The ADC on the ESP is really limited as it only responds to a maximum of 1V. Since some of these sensors only ran on 5V I needed to convert 5V to 3.3V to 1V in some places.

    Essentially my design was getting too messy. This massively simplifies it as I am now use the 328p chip(only one in stock in Sydney sigh!) for reading the sensors and I am planning to use the BSS138LT3G for logic level conversion. which is 22c. If I can only get Gas sensors running at 3V... those damned heating elements.

    I love the ESP chip but it needs some work on the ADC and PWM pins before I can throw the Amtels. The best part of this for me is that I now don't need Arduinos anymore. During this project I went from purchasing expensive pre-made shield and Arduinos to building my own boards and using raw AVR.

    Last night I went over an old project of mine and I realised how limited I was by the Arduinos themselves. Dont get me wrong I still think they are awesome little devices to learn and play with but the ease of it just working hides a lot of details which will make you a better engineer.

    I think I am now a level 1 engineer at least or something like that.

    Once again Peter your thoughts on these matters has been really useful to me. Hope to read some more from you in future.

  8. Hi - well on our boards we put a divider on the ADC so the max output is around 20v - good for self-checking 12v supplies etc. I keep at Espressif to put a proper "stop" command on the PWM - we'll get there eventually - yes, I hardly use Arduinos any more - as my knowledge of the ESPs grows.. However I do still have some lingering issues about utter reliability. I've had Arduino-based thermostats running 24-7 for a couple of years now and although my test boards now manage very high reliability - there is still the odd time they just don't power up or don't reset properly - very rarely but when a controller is in the UK and you're in Spain - may as well be Earth and Mars. Still working on this on a daily basis.


  9. Hi Peter

    I am watching the development of the MS Visual Studio Arduino/IOT edition with dread to be honest. Drag and drop Arduino programming is nice as a time saver it should not be used as teaching tool. There is way too many blogs of people using a full Arduino to make a light dim or to move a motor.People don't realize the power of these things for example the Apollo space mission had 4KB of memory for their navigation system and they landed on the moon. The Arduino is much more powerful than those chips in orders of magnitudes. Arduino is a easy entry into this world but it should not stop there.

    Maybe I am a bit old fashioned but if you never dropped into pure assembly to do things maybe programming is not for you. When you are passionate about something you want to know exactly how it works. Following build instructions blindly and just dragging and dropping control onto a form is fancy and shiny(bit like diamonds which are just shiny rocks) and then strutting around like a hero slightly irritates me. I guess I am concerned that at some point that the whole scene is flooded with so much junk that finding information becomes harder.

    The ESP was a game changer for me personally I went from buying an Arduino to make something to just using a AMTEL MCU on a breadboard and then designing a PCB, etching it, getting visited by the blue smoke monster and repeat till I got it right. Arduino has a special place in my heart as it reignited my love for electronics that the conventional schooling system in the apartheid era South Africa managed to kill. I hope it remains a guide for people to learn and become better engineers, programmers, hackers or like myself something to make some really cool toys for my kids and spark their curiosity.

    Ok well I am off to have some cheese with my whine now.


Comments are closed.