Arduino : Connecting Arduino UNO with ESP8266

We have already seen how to connect Arduino UNO with NodeMCU/ESP8266 over UART and send data to MQTT broker here.  That project uses lua to program the NodeMCU devices, to make it simple to use I will use Arduino IDE to program both the devices in this post.

What we are trying to achieve is simple, Arduino UNO generates some data (may be sensor readings) and we want to send that to cloud (in this case MQTT broker). Since Arduino UNO doesn’t have any network capabilities we need to device that can do networking for us, whats better than ESP8266. To send data from Arduino UNO to ESP8266 we will use UART.

Environment requirements:

  • you need to have UNO and ESP8266/NodeMCU
  • you would also need a 5v to 3v3 level converter to convert the UART signal levels. (Note: most of the nodemcu devices work fine without a level converter. I am not showing the level converter in the circuit diagrams, if you want to use refer this post for connection diagram)
  • setup your Arduino IDE to program NodeMCU
  • Arduino libraries : PubSubClient (if you haven’t already installed it , you can install it from Sketch->Include library->Manage Libraries)
  • mqtt broker , if you don’t have one you can use eclipse Paho broker for experimentation (host: iot.eclipse.org , port : 1883, it is a free and open broker)

Solution Approach:

Connecting Arduino UNO with NodeMCU
Connecting Arduino UNO with NodeMCU

We will push the data received on serial line on “/ic/nm/serialdata/uno/” channel/topic. You can view the data sent by the UNO on any MQTT client by subscribing to the topic “/ic/nm/serialdata/uno/“, I also embedded MQTT client at the end of this blog, scroll down to see it. Put your Broker details (if you don’t have a broker you can use the defaults and click on connect, when ever UNO sends a new message it should appear here)

First we will write program Arduino UNO to generate data periodically and send it on serial line to NodeMCU.

Arduino UNO code:

We setup a SoftSerial on pins 2,3 (Rx,Tx) and read a analog pin every five seconds. Put this data into JSON format and send it SoftSerial. Pretty much this is what the following code does. You can send any data over the Serial line not limited to JSON format. Only requirement is that each message should be terminated by newline character. If your data has newline characters, you can choose your own and change the NodeMCU code accordingly. We are using 115200 baud rate on softserial as well, at times you may see garbage on MQTT channel depending how the connections are made, you can reduce the baud rate on both Arduino UNO and NodeMCU and try again (Both Arduino UNO baud rate and NodeMCU baud rate should be same )

NodeMCU Code:

NodeMCU code does the following

  • setup UART
  • connect to configured access point
  • connect to MQTT broker
  • read data from UART channel until a newline character appears, then send the message to MQTT broker

 

How To Test:

Load the code shown above to UNO and NodeMCU.

Connect UNO to system and open serial console. You should see something like the following

arduino_uno_a0_read_periodic
arduino_uno_a0_read_periodic

Connect NodeMCU and change the COM port appropriately. Open serial console, it should print IP after connecting to Access point

nodemcu_uno_interfacing
nodemcu_uno_interfacing

If both boards are working as expected, we can connect them together now. Connect Arduino UNO pin 3 to the Rx pin of NodeMCU.

Connecting Arduino UNO with NodeMCU/ESP8266
Connecting Arduino UNO with NodeMCU/ESP8266

Click on Connect button below and power up both boards,you should see the messages below or in your MQTT Clinet.

 

——————————————————————–

——————————————————————–

18 Comments

Add a Comment

Your email address will not be published. Required fields are marked *