Serial Communication Arduino to NodeMcu

Viewed 2125

I'm looking to take inputs via keyboard by Arduino mega through usb cable, and send them to nodemcu through serial communication (TX - RX ) so I can send it to a server (from wifi module in nodemcu).

I have tried to write the codes for both Arduino and NodeMCu can someone help me to correct them


Arduino mega Code

#include <SoftwareSerial.h>
SoftwareSerial trans(19,18);

String scr ="";
String User = "";
String Request = "";
String Roomid = "";
String Email = "";

void setup() {
  Serial.begin(115200);
  trans.begin(115200);
}

void loop() {
  Serial.println("Enter your User ID.");
  while (Serial.available() == 0) {}
  User = Serial.readString(); //Reading the Input string from Serial port.
  Serial.println("Enter your Request.");

  while (Serial.available() == 0) {}
  Request = Serial.readString();
  Serial.println("Enter your Roomid.");

  while (Serial.available() == 0) {}
  Roomid = Serial.readString();
  Serial.println("Enter your Email.");

  while (Serial.available() == 0) {}
  Email = Serial.readString();
  Serial.println("-------------------------"); //Showing the details
  Serial.println("YOUR User ID:" + User);
  Serial.println("YOUR Request ID:" + Request);
  Serial.println("YOUR Room id:" + Roomid);
  Serial.println("YOUR EMAIL:" + Email);
  Serial.println("Thanks You...");
  Serial.println("");
  scr = User+Request+Roomid+Email;
  
  trans.write(scr);
  while (Serial.available() == 0) {}
}

NodeMcu Code

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include<SoftwareSerial.h> //Included SoftwareSerial Library

const char* ssid="SLT-4566";
const char* password="1234";
int data;
const char *host = "0.0.0.0"; //Your pc or server (database) IP, example : 192.168.0.0 , if you are a windows os user, open cmd, then type ipconfig then look at IPv4 Address.

void setup() {
  // put your setup code here, to run once:
  pinMode(2,OUTPUT);

  delay(1000);
  Serial.begin(115200);
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  delay(1000);
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
  
  WiFi.begin(ssid, password); //Connect to your WiFi router
  Serial.println("");

  Serial.print("Connecting");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
    delay(250);
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.println("Connected to Network/ssid");
  Serial.print(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}

void loop() {
  // put your main code here, to run repeatedly:
  data = Serial.read();
  Serial.println(data);
}
1 Answers

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69), so this is the firs problem.

serial.read() command read just one character, test serial.readString() here also.

But, I don't know, why it is needed that mega communicates with NodeMCU Serial.

I hope I could help! Good luck!

Related