I got a Mega2560 and a ESP-01 module. The sketch has been running for months but suddenly stopped working. There sketch has not changed.
Need some help/feedback on how to debug this problem.
When restarting the controller it issue the following AT commands (log from serial monitor):
10:47:54.062 -> Init Wifi.
10:47:54.062 -> ---> AT
10:47:54.062 -> Trying...
10:47:54.102 -> <--- DONE
10:47:54.102 -> ---> AT+CWMODE=1
10:47:54.102 -> Trying...
10:47:54.102 -> <--- DONE
10:47:54.142 -> Connecting Wifi...
10:47:54.142 -> ---> AT+CWJAP="mySSID","mysecretpassword"
10:47:54.222 -> Trying...
10:47:54.222 -> <--- DONE
10:47:54.262 -> ---> AT+CIPMUX=1
10:47:54.262 -> Trying...
10:47:54.302 -> <--- DONE
10:47:54.302 -> WiFi OK
10:47:54.382 -> *****************************************************
10:47:54.422 -> ********** Open TCP connection
10:47:54.462 -> ---> AT+CIPSTART=0,"TCP","192.168.2.22",80
10:47:54.502 -> Trying...
10:47:55.542 -> Trying...
10:47:56.382 -> <--- DONE
10:47:56.382 -> ---> AT+CIPSEND=0,38
10:47:56.422 -> Trying...
10:47:57.422 -> Trying...
10:48:00.222 -> Trying...
10:48:01.302 -> Trying...
10:48:02.382 -> Trying...
10:48:03.422 -> Trying...
10:48:04.502 -> Trying...
10:48:05.582 -> Trying...
10:48:06.662 -> Trying...
10:48:07.742 -> Trying...
10:48:08.822 -> <--- FAILED, error count=1
10:48:08.822 -> ********** Close TCP Connection
10:48:08.862 -> *****************************************************
I have tried different wifi modules but without success. Also tried to access two different WiFi routers.
I can see in the routers DHCP table that the module has got an IP address.
I also have a nano using the same module and it works fine. I see the requests from the nano in the webserver's log. The nano is using the same router.
Suggestions?
BTW, part of code in use:
HardwareSerial &wifi = Serial3;
uint32_t wifiErrorCount = 0;
bool sendATcmd(const char *AT_cmd, int AT_cmd_maxTime, const char *readReplay)
{
int trycnt = 0;
bool status = false;
Serial.print(F("---> "));
Serial.println(AT_cmd);
while (trycnt < AT_cmd_maxTime) {
Serial.println(F("Trying..."));
wifi.println(AT_cmd);
if (wifi.find(*readReplay)) {
AT_cmd_result = true;
break;
}
++trycnt;
}
Serial.print(F("<--- "));
if (true == AT_cmd_result) {
Serial.println(F("DONE"));
status = true;
} else {
++wifiErrorCount;
Serial.print(F("FAILED, error count="));
Serial.println(wifiErrorCount);
}
AT_cmd_result = false;
return status;
}
void setup()
{
Serial.begin(9600);
Serial.println(F("\n\nInitializing"));
bool status = false;
char atcmd[100];
Serial.println(F("Init Wifi."));
wifi.begin(9600);
for (int i = 0; !status && 10 > i; i++) {
status = sendATcmd("AT", 5, "OK");
if (status) {
status = sendATcmd("AT+CWMODE=1", 5, "OK");
} else {
delay(1000);
Serial.println(F("- Failed"));
}
}
if (status) {
debugprintF(2, F("Connecting Wifi..."));
Serial.println(ssid);
sprintf_P(atcmd, PSTR("AT+CWJAP=\"%s\",\"%s\""), ssid, password);
status = sendATcmd(atcmd, 20, "OK");
}
if (status) {
status = sendATcmd("AT+CIPMUX=1", 10, "OK");
}
wifiOk = status;
if (wifiOk) {
Serial.println(F("WiFi OK"));
}
// TEST CODE.
if (wifiOk) {
Serial.println(F("Testing Wifi..."));
for (int i = 0; 3 > i; i++) {
Serial.println(F("\n\n*****************************************************"));
Serial.println(F("********** Open TCP connection "));
sprintf_P(atcmd, PSTR("AT+CIPSTART=0,\"TCP\",\"%s\",%s"), host, port);
status = sendATcmd(atcmd, 20, "OK");
// Create the URL for the request.
if (status) {
char atcmd2[20];
sprintf_P(atcmd, PSTR("GET /foo/bar/mega.php?&v1=%d&v2=%d"), random(10, 100), random(10, 100));
sprintf_P(atcmd2, PSTR("AT+CIPSEND=0,%d"), strlen(atcmd) + 4);
status = sendATcmd(atcmd2, 10, ">");
if (status) {
sendATcmd(atcmd, 10, "OK");
status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
}
Serial.println(F("********** Close TCP Connection "));
Serial.println(F("*****************************************************"));
}
}
}
}