Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
         }
       }
share|improve this question
1  
Well, you seem to be wired into Serial1 yet you're using Serial2 to communicate... – Majenko Sep 18 '15 at 10:17
    
I am using Serial2 with pin 16, 17. this diagram is my reference but using serial2 – Shivam S.Kara Sep 21 '15 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 '15 at 9:12
    
Yes Majenko I am using the same one. – Shivam S.Kara Sep 21 '15 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 '15 at 9:18

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;
}

}
share|improve this answer

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.

share|improve this answer

You have connected to serial1 and in the program, you have called Serial2.

share|improve this answer
    
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review – Greenonline May 16 at 8:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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