1

hey guys I am sending my data from Arduino Mega to NodeMCU. I want to do a serial communication. My Arduino code is about operating multiple buttons while the nodemcu code is relatively simple. The code on arduino is working perfectly and the code on NodeMCU gets uploaded without a problem but it is not showing anything on the Node serial monitor. I am attaching both my codes below can anyone please tll me what i need to do to make it work properly. First, my arduino code:

#include <ezButton.h>
#include <SoftwareSerial.h>


ezButton button1 (22);
ezButton button2 (24);
ezButton button3 (26);
ezButton button4 (28);
ezButton button5 (30);


SoftwareSerial espSerial(18, 19);
String str;
int Myval = 255;
int buttonDown = 0; 

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
espSerial.begin(9600);
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);


}

void loop() {
// put your main code here, to run repeatedly:
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();


int btn1S = button1.getState();
int btn2S = button2.getState();
int btn3S = button3.getState();
int btn4S = button4.getState();
int btn5S = button5.getState();

Serial.print("button 1 state: ");
Serial.println(btn1S);
Serial.print("button 2 state: ");
Serial.println(btn2S);
Serial.print("button 3 state: ");
Serial.println(btn3S);
Serial.print("button 4 state: ");
Serial.println(btn4S);
Serial.print("button 5 state: ");
Serial.println(btn5S);

if(button1.isPressed()&& buttonDown == 0){
Serial.println("Fucntion for 22 is now on");
buttonDown = 1;
}

if(button1.isReleased()&& buttonDown == 1){
Serial.println("Function for 22 is now off");
buttonDown = 0;
}

if(button2.isPressed()&& buttonDown == 0){
Serial.println("Fucntion for 24 is now on");
buttonDown = 1;
}

if(button2.isReleased()&& buttonDown == 1){
Serial.println("Function for 24 is now off");
buttonDown = 0;
}

if(button3.isPressed()&& buttonDown == 0){
Serial.println("Fucntion for 26 is now on");
buttonDown = 1;
}

if(button3.isReleased()&& buttonDown == 1){
Serial.println("Function for 26 is now off");
buttonDown = 0;
}

if(button4.isPressed()&& buttonDown == 0){
Serial.println("Fucntion for 28 is now on");
buttonDown = 1;
}

if(button4.isReleased()&& buttonDown == 1){
Serial.println("Function for 28 is now off");
buttonDown = 0;
}

if(button5.isPressed()&& buttonDown == 0){
Serial.println("Fucntion for 30 is now on");
buttonDown = 1;
}

if(button5.isReleased()&& buttonDown == 1){
Serial.println("Function for 30 is now off");
buttonDown = 0;
}

str = String("Coming from arduino: ")+String("Button 1 is now: ")+String(btn1S)+String("Button 2 is 
now: ")+String(btn2S)+String("Button 3 is now: ")+String(btn3S)+String("Button 4 is now: 
")+String(btn4S)+String("Button 5 is now: ")+String(btn5S);  
espSerial.println(str);


delay(1000);
}

My NodeMCU code:

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
while (!Serial) {
; 
}
}


void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
Serial.write(Serial.read()); 
}

}

Please help. I am in quite a predicament here.

0 Answers0