Sankar Cheppali | December 10, 2017 | Arduino, ESP32, IoT | 17 Comments
We have already seen how to connect arduino uno to NodeMCU (ESP8266) here. In this blog we connect Arduino UNO with ESP32. When connecting UNO with nodemcu we used Lua to program the ESp8266 device, here we will use arduino IDE to program both UNO and ESP32.
Just like in the previous post, UNO has some JSON data that its want to publish to a MQTT broker (actually it doesn’t matter what data UNO is sending,ESP will just consume data until it sees newline char,once newline char is received it will send the received data to the MQTT broker). Since UNO can’t do that alone, it will send that on a SoftSerial line to ESP32. We will program ESP32 to send every new line appeared on serial line to MQTT broker on specified channel.
Environment requirements:
- you need to have UNO and ESP32
- you would also need a 5v to 3v3 level converter to convert the UART signal levels
- setup your Arduino IDE to program ESP32
- 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 useeclipse Paho broker for experimentation (host: mqtt.eclipse.org , port : 1883, it is a free and open broker)
Solution Approach:
We will push the data received on serial line on “/ic/esp32/serialdata/uno/” channel/topic. You can view the data sent by the UNO on any MQTT client by subscribing to the topic “/ic/esp32/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 periodically then we will move to ESP32 code
Arduino UNO code:
Nothing fancy on arduino side, you take sample form ADC and send it over softserial . load the following code to arduino uno board (don’t forget to change the board and COM port).
ESP32 Code:
On ESP32, we need to do the following
- connect to WiFi (Access point)
- connect to MQTT broker
- send every new line received on serial line to MQTT broker
Change the access point details and MQTT broker (if you have a private instance otherwise use the defaults) Load the following code to ESP32 (don’t forget to change the Board and COM port settings in the IDE)
How To Test:
Connect UNO to system and open serial console. You should see something like the following
Connect ESP32 and change the COM port appropriately. Open serial console, it should print IP after connecting to Access point
If both boards are working as expected, we can connect them together now. Connect Arduino UNO pin 3 to the Rx pin of ESP32 through Level Shifter.
Click on Connect button below and power up both boards,you should see the messages below or in your MQTT Clinet.
——————————————————————–
——————————————————————–
Tags:Arduino, esp32, esp32 arduino, MQTT
Related Posts
- Atomic Pi eats other Pis for lunch
No Comments | Jul 10, 2019
- Creating a Azure IoT hub
No Comments | Jun 26, 2016
- ESP-IDF: ESP32 Setting Hostname
2 Comments | Oct 8, 2019
- Android MQTT – FATAL EXCEPTION: MQTT Con
No Comments | Apr 24, 2017