interfacing arduino uno with nodemcu

NodeMCU is great for connecting cloud and arduino is great at talking with different sensors. nodemcu has only one analog pin. In this blog we will see how to connect arduino to nodemcu and post data to a mqtt broker. Arduino will take temperature readings and send the readings to nodemcu over serial connection. Nodemcu will send a mqtt message for every reading it receives. If you have just one sensor to monitor you can directly use the analog input available on nodemcu, see this blog on how to use the analog pin of nodemcu.

On Arduino side, we will take sample periodically and send a JSON message over softserial to nodemcu. Please note that Arduino uno works on 5v and nodemcu works 3v3 level. So you should use a level shifter to connect arduino soft serial pins to the nodemcu uart port (It also works without level converter , but it is not recomended).

Connecting nodemcu with arduino uno
Connecting nodemcu with arduino uno

On nodemcu side we need to receive the messages sent by arduino over serial. Arduino will send the messages in JSON format. We will not be doing any processing on nodemcu (It is possible to process the messages before sending to cloud, for example you can compare the present reading with the previous published messages and send the message only if the reading is different) . You need to have working MQTT broker account. You can follow this blog to create and test mqtt account.

Arduino Code :

Nothing fancy on arduino side, you take sample form ADC and send it over softserial . load the following code to arduino.

int id=99;
#include <SoftwareSerial.h>

SoftwareSerial sw(2, 3); // RX, TX

void setup() {
 Serial.begin(115200);
 Serial.println("Interfacfing arduino with nodemcu");
 sw.begin(115200);
}

void loop() {
 Serial.println("Sending data to nodemcu");
 int adc=analogRead(A0);
 Serial.print("{\"sensorid\":");
 Serial.print(id);//sensor id
 Serial.print(",");
 Serial.print("\"adcValue\":");
 Serial.print(adc);//offset
 Serial.print("}");
 Serial.println();
 
 sw.print("{\"sensorid\":");
 sw.print(id);//sensor id
 sw.print(",");
 sw.print("\"adcValue\":");
 sw.print(adc);//offset
 sw.print("}");
 sw.println();
 delay(5000);
}
Arduino code
Arduino code
NodeMCU code:

On nodemcu you need to load 4 files, all are available on github.

init.lua

config.lua (edit your wifi,MQTT broker details here)

setup.lua

app.lua

Load all these three files to nodemcu. To test the setup, send a message from console , it should appear in your MQTT channel.

Testing MQTT Connection
Testing MQTT Connection
MQTT messages
MQTT messages
Connecting nodemcu with arduino uno
Connecting nodemcu with arduino uno

Once you verified that the nodemcu is able to connect to broker and send messages, you can connect Arduino to nodemcu. First you should close the serial port connection to nodemcu , then Connect Arduino SoftSerial Tx to NodeMCU Rx (through level shifter).

interfacing-arduino-with-nodemcu
interfacing-arduino-with-nodemcu

Note : This post uses Lua for programming NodeMCU, if you want to use Arduino IDE, checkout this post

53 Comments

Add a Comment

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