Home Creators Posts Import Register Favorites Logout

Content

Thanks to the ridiculously cheap cost of purchasing direct from Chinese suppliers on AliExpress, BangGood etc., I have acquired a very large number of "old" Arduino Nano and Uno boards. I say "old" because the Uno and Nano are based on designs that are, perhaps, 8 years old, and the microprocessing world is moving so fast that some of their features now seem positively antiquated. More modern microprocessors, such as the ESP32, come with built-in Wi-Fi, capacitive touch sensors, OLED screens etc. 

However, there's still substantial benefits of using those older Arduino boards: 

There is a vast range of libraries available for them - which are often more documented, tested, and supported than those for newer boards. 

Some hardware sensors are easier to integrate with the 5V logic level of the Uno, Nano, and Mega, compared to the 3.3V logic level more commonly-seen in newer boards. 

They're often a little bit more robust than newer boards - with built-in protection in case you accidentally apply voltage the wrong way around etc. 

The UNO has 6 analog inputs, and the Nano has 8 - an ESP8266 only has one analog pin, by comparison.

They only cost a few $ each!

So, I'm not ready to throw away my Arduino boards just yet, but I'd like to bring them up-to-date a little bit more, and the first thing is to make them all Wi-Fi enabled - to be able to monitor and issue commands remotely from any other device on the network. The cheapest way I've found to do that is by adding an ESP-01 module.

Now, there are several tutorials I found on the web about the ESP-01, but they all seemed very complex - involving flashing firmware onto the microprocessor, or sending arcane AT+ commands. I just wanted to use the Wi-Fi functionality of the chip as a cheap "Wi-Fi shield" for my Arduino. So here's how I did it

Hardware

----

Arduino UNO/Nano/Mega etc.

ESP-01 Module - around £3 from BangGood. It measures only about 1cm wide by 2cm long, and looks like this:

ESP-01 Adapter - now, this is not strictly necessary, but it does make wiring the ESP-01 to your Arduino a lot easier, as I'll explain below. 

The ESP-01 operates at 3.3V. The Arduino does have a 3.3V output pin, but it can only supply up to 200mA of current, and the ESP-01 can exceed that during Wi-Fi operation, so it shouldn't be powered by the Arduino's 3.3V output. Without the adaptor, you'll either need a separate 3.3V power supply, or you could use some method to stepdown the 5V output from the Arduino (which is capable of supplying up to 500mA). The ESP-01 also operates at 3.3V logic level when communicating, so directly writing a HIGH signal from one of the GPIO pins on the Arduino (which use 5V logicl level) pins could fry it. So you also need to create a voltage divider, or use a logic level converter on the RX/TX connection.

The adapter solves both of these problems - it is powered by the 5V supply of the Arduino (which can supply up to 500mA), and regulates it down to 3.3V via a few smoothing capacitors. It also uses a voltage divider to convert the logic signals.  So, for the cost of a few dollars, I'd encourage you to buy one to save yourself some time. You can get them from the same suppliers as the ESP-01 itself, e.g. here.

See the attached image for details of how to wire the ESP-01 to the Arduino (with or without the adaptor).

Configuring the ESP-01

---

To make use of the Wi-Fi on the ESP-01 chip, I'm using the WiFiESP library, which mimics the functionality of the familiar Arduino WiFi library. You can download it from https://github.com/bportaluri/WiFiEsp

To test the hardware, I started off by running the test>EspDebug example sketch supplied with the WiFiESP library, and loaded up the serial monitor. This, in theory, allows you to send commands to the ESP-01 board by typing in the bar at the top of the Serial Monitor pane and clicking send, then reading the response back. According to this EspressIf reference page, the command AT+GMR should print the version info of the firmware, but I couldn't get it to respond with anything at all.....

After some research (and as noted in the comments in the code), it seems that different ESP-01 boards are pre-configured to run at different baudrates. In my case, (through experimentation), I discovered my ESP-01 was configure to communicate at 115200 baud speed. So I changed the line of code in the EspDebug sketch from:

  Serial1.begin(9600);  // your ESP's baud rate might be different

to

  Serial1.begin(115200);  // your ESP's baud rate might be different

(Note that I'm changing the speed of Serial1 here, which is the speed of the (software) serial connection to the ESP01. Don't confuse it with Serial, which is the speed of the (hardware) serial connection via USB to your PC)

Now, with the serial monitor loaded up, I was able to type AT+GMR and got the following response:

AR)⸮MI

AT version:1.3.0.0(Jul 14 2016 18:54;01)

SDK vession:2.0.07aTg..1:

Definitely an improvement, but it had some odd garbage characters in it. I wondered if I had a loose connection, so checked all the wiring and then tried sending the AT+GMR command again. This time, I got slightly different garbage characters:

AR)⸮MI

AT versioo:1.3.0.0(Jtl 14 2016 18:54:01)

SDK version:2.0.05ilL.1:

It turns out that, although I was now communicating at 115200, which was the correct speed to match the ESP-01, the Arduino was unable to read data that fast using a software serial connection. So, rather than increasing the baud rate specified in the Arduino code to match the speed of the ESP-01, what I needed to do was to decrease the speed of the ESP-01 to match that originally specified in the Arduino code. 

To do that, while still running the test>EspDebug sketch, type the following into the input field at the top of the Arduino serial monitor window and hit Send (you might also need to change the line ending to Both NL and CR)

AT+UART_DEF=9600,8,1,0,0

You should get the answer "OK". This means that the ESP-01 has been set to run at 9600 baud, and will continue to do so until issued a new command to change (and will retain this value even during power off).

Now we have to change our code to match the new baud rate, so go back to the EspDebug sketch again and change the Serial1 baud rate back to 9600. And NOW, when we run AT+GMR get sensible output in the serial monitor:

Success!

Using the ESP-01 to remotely control/monitor your Arduino through a web browser

---

So now we have the ESP-01 hardware working and are able to communicate with it, how do we go about using it to remotely monitor or control the code running on the Arduino?

In previous projects, I've used MQTT to send commands to and from different networked devices - using the popular PubSubClient library for Arduino. One thing I found when using the approach described here was that MQTT didn't prove to be 100% reliable - the serial interface to the ESP-01 uses software emulation, which can't poll the MQTT server with sufficient frequency. Reliability improved significantly when I started using the AltSoftSerial library rather than the SoftwareSerial library, but occasionally the client would still not receive messages published to topics to which it was subscribed, so a different approach was required.

I did briefly consider using CoAP, but I found it hard to find any PC clients that supported the CoAP protocol, so instead I decided to run a simple webserver on the Arduino itself, which meant that it could be monitored and controlled from a HTTP GET request from any web browser on another device on the network.

You can download the Arduino code I used from https://drive.google.com/open?id=1F2zRSvmRoVASxzjBaS8ALg9sRC1lNjZU . I've tried to comment it fairly thoroughly, but please ask in the comments if you have any questions about how it works.

And now, visiting the page http://192.168.1.18/?led=on from my laptop or my mobile phonebrowser turns on the LED on my Nano, while loading  http://192.168.1.18/?led=off turns it off again. And I can monitor how long the Arduino has been running for by looking at the reported millis() value on the webpage. Neat, huh?

Files

Comments

Russell Hardman

This looks great, I’ve ordered my parts and can’t wait to start building. Thanks Alastair.

Joe Gould

Nice, I just order two of each from DX.com for just under $10 USD.

Russell Hardman

Anyone got this working? I’m not getting anything back from the ESP. I’m using the adaptor. I’ve tried all the baud rates up to 115200.

Russell Hardman

Hi Alistair, yes I was doing the above. Thanks for the quick reply. After 3 hours of scratching my head this morning and a guess I got the communication of EspDebug working. SoftwareSerial Serial1(6,7); // RX, TX isn’t correct it should be TX,RX. I check my wires and they were correct. On to the 2nd part now :-)

Russell Hardman

Wow, 2nd part worked first time. Turned on the LED from my phone. Very interesting, my mind will be spinning with ideas from this. You are a Genius!

Thomas Terkildsen

Hi Alastair! I am interested in using the ESP8266 with MQTT. Is there any chance you can show an example of your code using AltSoftSerial. I know it might not be completely reliable but I am having trouble getting mine to compile!

Sara Fernández

Hi Alastair! First of all, I'm very grateful with all your posts, they are very very interesting, you're a genious. I am interested too in using ESP-01 and Arduino Nano with MQTT. I've already get it and it works, but I don't know why the ESP-01 suddenly stops working, disconnects from the mqtt server, disconnects from Wi-Fi, and it doesn't work again until I reset the arduino. I have also detected that not all messages reach the arduino, nor all messages are received by the MQTT server. Is there no possibility to use MQTT with Arduino + ESP01? I am very desperate, I would appreciate your opinion. Thank you.