1

For the last days, I am trying to establish serial communication between nodemcu and Arduino Uno but I failed to do this. Currently, I am working on a Home Automation project. I receive data from nodemcu and send it to Arduino Uno, but some dummy data also receive with original data. Please help.

Nodemcu code:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define Automated   D0
#define Led         D1
#define Fan         D2

#define WLAN_SSID       "JioFi2_1EAFF8"
#define WLAN_PASS       "Ode9c@,1S%31ja$1xANkeL"

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "_inishu"
#define AIO_KEY        "aio_HZUG00TupkcsC2dvraQM7HLHrCd4"

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

Adafruit_MQTT_Subscribe speed = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/Fan"); 
Adafruit_MQTT_Subscribe inten = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Led");
Adafruit_MQTT_Subscribe autom = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Automate");

void MQTT_connect();
void datatransfer(int);


int on_off;
int sliderval1;
int sliderval2;
void setup(){
    Serial.begin(9600);
    pinMode(Automated, OUTPUT);
    pinMode(Led, OUTPUT);
    pinMode(Fan, OUTPUT);

    WiFi.begin(WLAN_SSID, WLAN_PASS);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
    }

    mqtt.subscribe(&autom);
    mqtt.subscribe(&speed);
    mqtt.subscribe(&inten);
}


void loop (){
    MQTT_connect();

    Adafruit_MQTT_Subscribe *subscription;
    while ((subscription = mqtt.readSubscription(5000))){
        if (subscription == &autom){
            on_off = atoi((char *)autom.lastread);
            if (on_off == 1)   digitalWrite(Automated,HIGH);
            if (on_off == 0)   digitalWrite(Automated,LOW);
        }
        
        if (subscription == &speed){
            sliderval1 = atoi((char *)speed.lastread);
            analogWrite(Fan, sliderval1);
        }

        if (subscription == &inten){
            sliderval2 = atoi((char *)inten.lastread);
            analogWrite(Led, sliderval2);
        }
    }
    datatransfer(on_off);
    datatransfer(sliderval1);
    datatransfer(sliderval2);
}


void MQTT_connect() {
    if (mqtt.connected()) {
         return;
    }
  
    while (mqtt.connect() != 0) {
        mqtt.disconnect();
        delay(2000);  
    }
}

void datatransfer(int value){
    Serial.println(value);
    delay(500);
} 

Arduino Uno code:

int x = A0, y = A1, z = 1;
int fan = 11, led = 10;
float temp, lux;
int autom = 1;
int inten = 0;
int speed = 0;
int fanspeed, light;

int datareceive(){
    int receiver = 0;
    while(Serial.available()==0 );
    String s = Serial.readStringUntil('\r');
    receiver = s.toInt();
    Serial.println(receiver);
    delay(1000);
    return receiver;
}
 
void setup(){
    Serial.begin(9600);
    pinMode(fan,OUTPUT);
    pinMode(led,OUTPUT);
    pinMode(x,INPUT);
    pinMode(y,INPUT);
    pinMode(z,INPUT);
}
 
void loop(){
    autom = datareceive();
    speed = datareceive();
    inten = datareceive();
    if(autom == 1){
        fanspeed = 255 - speed;
        light = 255 - inten;
        analogWrite(fan,fanspeed);
        analogWrite(led,light);
    }  
    else {
        temp = analogRead(x);
        if (temp >= 52 && temp <= 104){
            int fanspeed = -4.90384615*temp + 510;
            analogWrite(fan, fanspeed);
        }
        else if(temp > 104)  digitalWrite(fan,LOW);
        else digitalWrite(fan,HIGH);
        lux = analogRead(y);
        if (lux >= 430 && lux <= 594){
            int light = 1.342657*lux-577.342657;
            analogWrite(led,light);
        }
        else if (lux < 430) digitalWrite(led, LOW);
        else digitalWrite(led,HIGH);
    }
}
5
  • 1
    You didn't describe, what actually happens, just that it doesn't work. Please add this description to your question. At the first glance I see, that you send up to every halve second, while you receive only once a second, since you delay for 1s. Why are you even delaying there? That doesn't fit any purpose
    – chrisl
    Nov 27, 2020 at 7:55
  • I agree with @chrisl, don't use delay, bad practice, search for "Arduino millis delay" that's a better way. And why do you transfer a value without a tag, when I have done similar tasks I have used a transfer format like "<type>, <value>", example "speed, 200". Nov 27, 2020 at 9:28
  • And to trouble shoot, create a simple KISS program so you learn how to do transfer over serial first before you do a more complex program. Nov 27, 2020 at 9:31
  • Delete you AIO details from sketch.
    – Tomas
    Aug 21, 2022 at 12:06
  • give us example of data and what is extra. I for one have noticed that you use println, so after each message you are getting a newline symbol. (extra data at the end). Although NODEMCU sends some data to serial every time it starts. Also dont forget the difference between number and symbols of numbers. print sends numbers as their symbols, while "write" sends numbers as numbers
    – Tomas
    Aug 21, 2022 at 12:17

1 Answer 1

1

Arduino UNO is 5v logic and esp8266 is 3.3v logic. You need a level shifter for them to be able to have bidirectional serial link. Alternatively you can just use a voltage divider between Arduino Tx and ESP Rx pin to protect the rx pin of the esp.

1
  • It would be a preferencial but arduino is more then capable of handling the ESP8266 input and while not ideal, esp is designed to STRESS handle up-to 5.1 volt. Full shifter is an overkill. So if you extend your message to show a simple ressistive divider to protect nodemcu, dropping message voltage from arduino tx towards nodemcu RX, your note (rather then answer) might become useful
    – Tomas
    Aug 21, 2022 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.