-->
Page 1 of 1

Problems of ESP8266 with Arduino Mega 2560

PostPosted: Sat Sep 10, 2016 10:54 am
by samchan1123
Hi everyone. I am now using Arduino Mega 2560 and ESP8266, but i have just met some difficulties.

At the begining, i tried send AT command through Serial Monitor and obtained the feed back.
HERE IS MY CODE
CODE start-------------------------------------------------------------------------------------------------------
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
Serial.println("SoftSerial to ESP8266 AT commands test ...");
delay(100);
}

void loop()
{

if (Serial1.available()) // if the channel for ESP8266 recieve the signal
{
Serial.write(Serial1.read());
}

if (Serial.available())
{
char command=Serial.read(); //Set a variable to temporarily store the command, work as buffer
Serial1.write(command); //Send the command to EPSP82966

}
}
CODE end------------------------------------------------------------------------------------------------------------
When I send "AT" via Serial monitor, however, it doesn't give a feedback "OK"
Then I add the follow code one above
ADDITIONAL CODE start----------------------------------------------------------------------------------------
#include <SoftwareSerial.h>
SoftwareSerial ESP8266(19,18); <-RX1 and TX1
Also i replace all Serial1 with ESP8266
ADDITIONAL CODE end----------------------------------------------------------------------------------------
It still does not work.
Then I replace ESP8266 with Serial1, here is the code
CODE START------------------------------------------------------------------------------------------
#include <SoftwareSerial.h>
SoftwareSerial ESP8266(19,18);
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
Serial.println("SoftSerial to ESP8266 AT commands test ...");
delay(100);
}

void loop()
{

if (Serial1.available())
{
Serial.write(Serial1.read());
}

if (Serial.available())
{
char command=Serial.read();
Serial1.write(command);

}
}
CODE END-------------------------------------------------------------------------------------------------------------
And it succeeds, the Serial monitor show the feedback "OK" when i send the command "AT"
I would like to know:
1. Is it a must to us SoftwareSerial? Mega2560 has 3 Serial ports, suppose it can use Serial1 2 or 3.
2. Why can't I use ESP8266 instead of Serial1(e.g ESP8266.write instead of Serial1.write) ? Even I make declear it >>>>>>>>SoftwareSerial ESP8266(19,18);, but it still can't work.

I have tried my best by still don't have any idea :(

Re: Problems of ESP8266 with Arduino Mega 2560

PostPosted: Sat Sep 10, 2016 10:00 pm
by izbin
In your last code you are using hardware serial 0 and 1 not software serial.
You can use software serial on the Mega but you can't use all the pins for RX since not all of them have interrrupts. see here from: https://www.arduino.cc/en/Reference/SoftwareSerial
"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)."