Arduino Mega 2560 Serial Communication to ESP8266(NodemCU)

// varibales initialize to 0
int RedLed =0; // Uses Pin 8
int BlueLed = 0; // Uses Pin 9

void setup() {
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  Serial.begin(9600);
}
void loop() {
  // 
  BlueLed = digitalRead(8);
  RedLed = digitalRead(9);
  if(RedLed == HIGH){
    Serial.println("RED");
  }
  else if(BlueLed ==){
    Serial.println("BLUE");
  }
  else{
    if (RedLed == HIGH && BlueLed == HIGH) {
      Serial.println("BLUE/RED");
    }
  }
  delay(100);
}

Hello everyone, I am a newbie to using Arduino boards. The code that I uploaded is from my Arduino Mega 2560 board. Essentially, I need help with using serial communication to communicate with my ES8266 board each time my Arduino board executes a statement like if the RED led has been turned on or blue, etc... I will be sending that to a UDP server using my ESP8266 board.


#include <string.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>

#ifndef STASSID
#define STAASSID "HOME-WiFi"
#define STAPSK   "SCOOBYDOO"
#endif

unsigned unit localPort = 1234;
WiFiUDP Udp;
//// Some code here for receiving the string message of my arduino board 
/// once string message is received to send a message to a Udp server after ESP8266 has connected to the Wi-Fi.
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status())!= WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.print("Connected IP adress: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP Client Ready", localPort);
  Udp.begin(localPort);
}

void loop() {
  // put your main code here, to run repeatedly:
}
/// code below send a string message where you decided to pass the function and type a string as such e.g sendData("Hello UDP Server")
void sendData(char *msg){
  IPAddress remoteIP(127,0,0,1);
  int remotePort = 2004;
  Udp.beginpacket(remoteIP, remotePort);

  Serial.println("Packet Sent");
  Udp.write("Packet sent");
  Udp.endpacket();
}

Did you wire the boards together and try your code? If yes, please describe how. What was the result?

Be aware that the Mega is a 5V device and the ESP8266 is a 3.3V device; your will need a voltage divider between the Mega's Tx and the ESP8266 Rx.
Also don't forget that there must be a GND connection between the two boards.

Your Mega has 4 serial ports. It's advisable to use e.g. Serial1 for communication with the ESP8266. That will keep Serial available for communication with the PC.

Please edit your posts, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and the forum software will display it correctly.

Your topic has been moved to a more suitable location on the forum as this has nothing to do with Avrdude, stk500 or Bootloader.

it is nonsens to use SoftwareSerial on hardware Serial pins.
the Mega has 4 hardware Serials: Serial, Serial1, Serial2 and Serial3. the pins are labeled on the board

Your c variable is a single char, so it absolutely impossible that it will be equal to string of 8 chars long

Hi, yea still a newbi. But essentially how could I use the hardware serials to send information to my arduino board from my esp8266?

1. Make connection as per Fig-1 between ESP8266 Board and Arduino mEGA Board.
(1) Hardware UART Port of ESP8266 is engaged with SM/IDE for debugging/Uploading of sketches.

(2) The second serial port has been created using SoftwareSerial.h Library and is named as SUART(4, 5).


Figure-1:

2. Sender/ESP8266 Sketch. This sketch will send the message "Forum" to MEGA at 1-sec interval.

Try yourself; if does not work, post it here; we will try to correct it.

3. Receiver Sketch. This sketch will receive the message "Forum" from from ESP8266 and will show on SM1 at 1-sec interval.

Try yourself; if does not work, post it here; we will try to correct it.

4. Upload sketch of Step-2.
5. Upload sketch of Step-3.
6. Press REST buttons of both Arduinos.
7. Check that SM1 of MEGA shows the message "Forum" at 1-sec interval.
8. Add codes with the sender/receiver sketches so that ESP8266 can receive message/data from MEGA and shows on SM2.

Try yourself; if does not work, post it here; we will try to correct it.

I just cross connected Arduino board TX with ESP8266 RX and vice versa & ground with ground. But I did not add a voltage divider. I can do that thanks. Umm but still what would I have to write on each board code that one can send and one can just receive a message and do something with that

I Just looked at the board and it does indeed it has (i=5) TX-i and RX-i). Okay and so I know the arduino board by default uses TX0 and TX0.

simple test sending text messages between the Mega and ESP8266 at 115200baud
connecting Mega hardware Serial1 (pins 18 TX1 and 19 RX1) to ModeMCU ESP-12E using SoftwareSerial pins D5 (RX) and D6 (TX)
Mega code

// Arduino Mega serial1 test

// mega pin 18 is Tx
//      pin 19 is Rx
// for loopback test connect pin 18 to pin 19

// for RS232 shield connect pin 18 to Tx and pin 19 to Rx
// for loopback test connect 9 pin D connector pins 2 and 3

unsigned long time;

void setup() {
  Serial.begin(115200);   // initialise serial monitor port
  Serial1.begin(115200);  // initialise Serial1
  Serial.write("Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19\n");
}

void loop() {
  if (Serial1.available())        // read from Serial1 output to Serial
    Serial.write(Serial1.read());
  if (Serial.available()) {       // read from Serial outut to Serial1
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

ESP8266 code

// ESP8266 SoftwareSerial
// https://circuits4you.com/2016/12/14/software-serial-esp8266/

#include <SoftwareSerial.h>

// pins Rx GPIO14 (D5) and Tx GPIO 12 (D6)
SoftwareSerial swSer(14, 12);  

void setup() {
  Serial.begin(115200);   //Initialize hardware serial with baudrate of 115200
  swSer.begin(115200);    //Initialize software serial with baudrate of 115200
  Serial.println("\nESP8266 Software serial test started");
}

void loop() {
  while (swSer.available() > 0) {  //wait for data at software serial
    Serial.write(swSer.read()); //Send data recived from software serial to hardware serial    
  }
  while (Serial.available() > 0) { //wait for data at hardware serial
    swSer.write(Serial.read());     //send data recived from hardware serial to software serial
  }
}

mega Serial monitor output as text is entered in ESP8266 serial monitor

06:05:01.387 -> Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19
06:05:49.661 -> esp8266 test 1 hello
06:06:07.058 -> esp8266 test 2  12345678901234567890
06:06:24.805 -> esp8266 test 3  abcdefghijklmnop

ESP8266 Serial monitor output as text is entered in Mega serial monitor

06:04:56.924 -> ESP8266 Software serial test started
06:05:14.329 -> mega test1 hello
06:05:23.875 -> mega test 2   1234567890
06:05:36.124 -> mega test 3 abcdefghij

remember to have a voltage divider on the Mega TX1 pin18 (5V logic) to ESP8266 Rx pin D5 (3.3V logic), e.g.

Mmm so are you saying you would be transmitting what you are typing in the serial monitor?

Is there a way that whatever my arduino boards sends be put into a variable? Essentially, my ESp8266 would connect to wi-fi and pass that message onto my server. Like if i press a push button to turn on an Red LED and my arduino board mega 2560 reads the pin as being HIGH. To send a string message ("RED LED ON") to my ESP8266 wifi board so that the reading can be sent to my udp server.

as the text is received copy it into a char array, e.g. using Serial.readBytesUntil()
then parse the array looking for text strings (e.g. "RED LED ON"), numeric data (34,-98,3.1415926) etc

have a look at serial-input-basics

yes, post #11 is just a test to check the hardware, wiring and basic software works
once you know the communication works you then modify code to transmit and receive information as required by the project

Oh okay, that makes sense. Once I have ensured that communication in that way works, what modifications do I need? So sorry again I am such a newbie, but this stuff interests me a lot. Anyways, thanks so much for responding and trying to help me.

This is essentially my code for ESP8266

#include <string.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SoftwareSerial.h>

#ifndef STASSID
#define STAASSID "HOME-WiFi"
#define STAPSK   "SCOOBYDOO"
#endif

unsigned unit localPort = 1234;
WiFiUDP Udp;
//// Some code here for receiving the string message of my arduino board 
/// once string message is received to send a message to a Udp server after ESP8266 has connected to the Wi-Fi.
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  while (WiFi.status())!= WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.print("Connected IP adress: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP Client Ready", localPort);
  Udp.begin(localPort);
}

void loop() {
  // put your main code here, to run repeatedly:
}
/// code below send a string message where you decided to pass the function and type a string as such e.g sendData("Hello UDP Server")
void sendData(char *msg){
  IPAddress remoteIP(127,0,0,1);
  int remotePort = 2004;
  Udp.beginpacket(remoteIP, remotePort);

  Serial.println("Packet Sent");
  Udp.write("Packet sent");
  Udp.endpacket();
}`Preformatted text`

I am sorry, I am unsure how I would copy it into an array like that. Would it work if there were incoming messages all of the time?

Don't run before you can walk.

First verify that you can communicate and let us know. After that we can talk about adding things like arrays.

God I love advice like this lol but you are right let me make sure I know how to walk first lol