0

I am having Issue while connecting ESP8266 with Arduino Mega. It always says "Module have no response."

Please check and correct me if I am wrong anywhere.

I am using below code and and wiring diagram is

enter image description here

My Code:

//#include <SoftwareSerial.h>
   //use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).
   #define SSID "SopraSteria"
   #define PASS "1234567890"
   #define DST_IP "220.181.111.85" //baidu.com
   //SoftwareSerial dbgSerial(10, 11); // RX, TX
   void setup()
   {
     // Open serial communications and wait for port to open:
     //serial 2 is to esp8266 
     Serial2.begin(115200);//9600 (mine), 57600, 115200
     Serial2.setTimeout(2000);

     //serial 0 is to usb
     Serial.begin(115200);


     while(!Serial); 
     while(!Serial2);

     //dbgSerial.begin(9600); //can't be faster than 19200 for softserial
     //dbgSerial.println("ESP8266 Demo");
     Serial.println("ESP8266 Demo on Mega2560");

    while(Serial2.available()>0)
    Serial2.read();

     delay(1000);
       //test if the module is ready
     Serial2.println("AT+RST");
     //delay(1000);
     //delay(1000);
     Serial.println("Resetting module");
     Serial2.flush();
     //Serial.println(Serial2.read());
     //if(Serial2.find("ready"))
     if(Serial2.find("Ready")||Serial2.find("ready"))
     {
       //dbgSerial.println("Module is ready");
       Serial.println("Module is ready");
     }
     else
     {
       //dbgSerial.println("Module have no response.");
       Serial.println("Module have no response.");
       //while(1);
     }
     delay(1000);
     //connect to the wifi
     boolean connected=false;
     for(int i=0;i<5;i++)
     {
       if(connectWiFi())
       {
         connected = true;
         break;
       }
     }
     if (!connected){
      //while(1);
      Serial.println("Not Connected.");
     }
     delay(1000);
     //print the ip addr

   Serial2.println("AT+CIFSR");
     Serial.println("ip address:");
     while (Serial2.available())
     Serial.write(Serial2.read());


     //set the single connection mode
     Serial2.println("AT+CIPMUX=0");
   }
   void loop()
   {
    //connectWiFi();
     String cmd = "AT+CIPSTART=\"TCP\",\"";
     cmd += DST_IP;
     cmd += "\",80";
     Serial2.println(cmd);
     Serial.println(cmd);
     Serial.println(cmd);
     if(Serial2.find("Error")) return;
     cmd = "GET / HTTP/1.0\r\n\r\n";
     Serial2.print("AT+CIPSEND=");
     Serial2.println(cmd.length());
     if(Serial2.find(">"))
     {
       //dbgSerial.print(">");
       Serial.print(">");
       }else
       {
         Serial2.println("AT+CIPCLOSE");
         //dbgSerial.println("connect timeout");
         Serial.println("connect timeout");
         delay(1000);
         return;
       }
       Serial2.print(cmd);
       delay(2000);
       //Serial.find("+IPD");
       while (Serial2.available())
       {
         char c = Serial2.read();
         //dbgSerial.write(c);
         Serial.write(c);
         //if(c=='\r') dbgSerial.print('\n');
         if(c=='\r') Serial.print('\n');
       }
       //dbgSerial.println("====");
       Serial.println("====");
       delay(1000);
     }
     boolean connectWiFi()
     {
       Serial2.println("AT+CWMODE=1");
       String cmd="AT+CWJAP=\"";
       cmd+=SSID;
       cmd+="\",\"";
       cmd+=PASS;
       cmd+="\"";
       //dbgSerial.println(cmd);
       Serial2.println(cmd);
       Serial.println(cmd);
       delay(2000);
       if(Serial2.find("OK"))
       {
         //dbgSerial.println("OK, Connected to WiFi.");
         Serial.println("OK, Connected to WiFi.");
         return true;
         }else
         {
           //dbgSerial.println("Can not connect to the WiFi.");
           Serial.println("Can not connect to the WiFi.");
           return false;
         }
       }
6
  • 2
    Well, you seem to be wired into Serial1 yet you're using Serial2 to communicate...
    – Majenko
    Sep 18, 2015 at 10:17
  • I am using Serial2 with pin 16, 17. this diagram is my reference but using serial2 Sep 21, 2015 at 6:23
  • And are you using the same ESP module? There's lots of different ones. The one pictured there is the ESP-01
    – Majenko
    Sep 21, 2015 at 9:12
  • Yes Majenko I am using the same one. Sep 21, 2015 at 9:14
  • Have you tried different baud rates? Different versions of the AT firmware have different baud rates set as default. You need to make sure you match them up.
    – Majenko
    Sep 21, 2015 at 9:18

3 Answers 3

1

Your transmitter and receiver pins need to be crossed, rx->tx and tx->rx. As a mnemonic, the two ends of the antenna point to the board edge with the receiver pin and it is that pin that needs the voltage divider. While you're at it, it would be a good idea to tie the RST pin high, as well as the CH_PD. I use one 10K pullup for both.

Recent versions of these boards are shipped with their baud rates set to 115200. Since you're using a hardware UART on the Mega board, you can send/receive to it at that rate.

Note to readers using a software UART (SoftwareSerial): First thing, change the board's baud rate to 9600 and communicate with it at that rate. You can use SoftwareSerial to make the change as it sends at 115200 reasonably reliably, but its receive function can't keep up at that rate).

You can confirm your connections and baud rate with:
EspSerial.println("AT");
You should receive `OK' in reply.

0

I took your code as a starting point and got it to work with some small modifications. I am providing my version below for your review. I had to add a bunch of delays. I am also communicating with the ESP8266 at 19200 baud.

I have not optimized the timing yet but I was able to run this code for 24 hours straight with no issues. I am using Pins 6 and 7 on the Arduino for COMM to the ESP8266.

#include <SoftwareSerial.h>
#define SSID "YOURSSID"
#define PASS "YOURPW"
#define DST_IP "220.181.111.85"

SoftwareSerial Serial2(6, 7); // RX, TX

void setup() {

// Open serial communications w ESP8266
Serial2.begin(19200);
Serial2.setTimeout(5000);

// serial 0 is to usb
Serial.begin(115200);
while(!Serial); 
while(!Serial2);

Serial.println("ESP8266 Demo on Mega2560");

while (Serial2.available() > 0)
Serial2.read();
delay(1000);
while (Serial2.available() > 0)
Serial2.read();

//test if the module is ready
Serial.println("RESETTING ESP8266 WIFI CHIP");
Serial2.write("AT+RST\r\n");
delay(2500);
while (Serial2.available() > 0)
Serial.write(Serial2.read());

delay(2500);
while (Serial2.available() > 0)
Serial.write(Serial2.read());
Serial.println("");

//  connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++) {
if(connectWiFi())
{
connected = true;
break;
}
}

if (!connected){
Serial.println("Not Connected.");
}

Serial2.println("AT+CIFSR");
delay(2000);
while (Serial2.available())
Serial.write(Serial2.read());
delay(2000);
while (Serial2.available())
Serial.write(Serial2.read());

//  set the single connection mode
Serial2.println("AT+CIPMUX=0");
delay(2000);
while (Serial2.available())
Serial.write(Serial2.read());
delay(2000);
while (Serial2.available())
Serial.write(Serial2.read());
}

void loop() {

String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
Serial2.println(cmd);
Serial.println(cmd);
delay(5000);

if(Serial2.find("Error")) return;

cmd="GET / HTTP/1.0\r\n\r\n";
Serial2.print("AT+CIPSEND=");
Serial2.println(cmd.length());
delay(2000);

if(Serial2.find(">"))      {
Serial.print(">"); 
}
else {
Serial2.println("AT+CIPCLOSE");
Serial.println("connect timeout");
delay(2000);
return;
}
Serial2.print(cmd);
delay(2500);

while (Serial2.available()) {
char c = Serial2.read();
Serial.write(c);;
if(c=='\r') Serial.print('\n');
delay(5);
}

Serial.println("");
Serial.println("====");
delay(2000);
}

boolean connectWiFi() {

String cmd;
int iIP;

iIP = 0;
Serial2.println("AT+CWMODE=1");
delay(5000);
while (Serial2.available())
Serial.write(Serial2.read());

cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";

Serial2.println(cmd);

delay(2000);
Serial.println("Line 1");
while (Serial2.available() > 0)
Serial.write(Serial2.read());

delay(2000);
Serial.println("Line 2");
while (Serial2.available() > 0)
Serial.write(Serial2.read());
if (Serial2.find("OK")) {
Serial.println("WIFI OK was received!");
iIP = 1;
}  

if( iIP == 1 ) {
Serial.println("OK, Connected to WiFi.");
return true;
}
else  {
Serial.println("Can not connect to the WiFi.");
return false;
}

}
-1

As shown in the Fritzing diagram, you have connected the ESP8266 to the Mega's Serial1 pins (18 and 19), but in the program, you try to interact with Serial2 (pins 16 an 17). For this to work, your wiring and your program must match.

3
  • @ChrisStratton - I think that, prior to your edit, the answer was somewhat short and seemed more appropriate as a comment, hence my comment. Now, with the expanded and "explicit" edit, my comment is no longer valid. :-) well done for spotting it (as Majenko also did) and I shall give the answer a vote up Oct 18, 2016 at 1:51
  • Good point, it's only repeating something already observed in comments long before. Upvote removed. Oct 18, 2016 at 1:57
  • @ChrisStratton - Also... it seems (after re-reading the reply to Majenko's comment) that the Fritzing diagram is not totally accurate, and that pins 16 and 17 are actually being used, rather than 18 and 19 as shown in the diagram: I am using Serial2 with pin 16, 17. this diagram is my reference but using Serial2. So... that renders this answer as being invalid. I shall attempt to remove my upvote as well. Oct 22, 2016 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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