Hello,
I'm trying to use a ESP8266 to send data from an Uno to a web-server. The problem is that the CPISTART does not work all the time. It fail from time to time.
Any suggestions what could cause this?
The sketch is based on code from https://github.com/asksensors/AskSensors-Arduino-WiFi:
#include <SoftwareSerial.h>
// serial config.
#define RX 10
#define TX 11
SoftwareSerial AT(RX,TX);
// User config.
String ssid = "ngc7000";
String password = "secret password";
String apiKeyIn = "dahls/Ymse/uno.php";
// write interval (in ms)
const unsigned int writeInterval = 25000;
// API host config.
String host = "192.168.2.22";
String port = "80";
int AT_cmd_time;
boolean AT_cmd_result = false;
void setup()
{
bool status;
Serial.begin(9600);
// open serial
Serial.println(F("*****************************************************"));
Serial.println(F("********** Program Start : Connect Arduino WiFi to AskSensors"));
AT.begin(115200);
Serial.println(F("Initiate AT commands with ESP8266 "));
status = sendATcmd("AT", 5, "OK");
while (!status) ;
status = sendATcmd("AT+CWMODE=1", 5, "OK");
while (!status) ;
Serial.print(F("Connecting to WiFi:"));
Serial.println(ssid);
status = sendATcmd("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"", 20, "OK");
while (!status) ;
}
void loop()
{
static int success = 0, failed = 0;
bool status;
char buffer[30];
// Create the URL for the request
String url = "GET /";
url += apiKeyIn;
url += "?v1=";
url += random(10, 100);
url += "&v2=";
url += random(10, 100);
Serial.println(F("\n\n*****************************************************"));
Serial.println(F("********** Open TCP connection "));
status = sendATcmd("AT+CIPMUX=1", 10, "OK");
if (status) {
status = sendATcmd("AT+CIPSTART=0,\"TCP\",\"" + host + "\"," + port, 20, "OK");
}
if (status) {
status = sendATcmd("AT+CIPSEND=0," + String(url.length() + 4), 10, ">");
}
if (status) {
Serial.print(F("********** requesting URL: "));
Serial.println(url);
AT.println(url);
delay(2000);
status = sendATcmd("AT+CIPCLOSE=0", 10, "OK");
}
Serial.println(F("********** Close TCP Connection "));
Serial.println(F("*****************************************************"));
if (status) {
++success;
} else {
++failed;
}
sprintf(buffer, "OK: %d, Failed: %d", success, failed);
Serial.println(buffer);
delay(writeInterval);
}
// sendATcmd
bool sendATcmd(String AT_cmd, int AT_cmd_maxTime, char readReplay[])
{
bool status = false;
Serial.print(F("AT command:"));
Serial.println(AT_cmd);
while (AT_cmd_time < AT_cmd_maxTime) {
AT.println(AT_cmd);
if (AT.find(readReplay)) {
AT_cmd_result = true;
break;
}
AT_cmd_time++;
}
Serial.print(F(" Result:"));
if (true == AT_cmd_result) {
Serial.println(F("DONE"));
AT_cmd_time = 0;
status = true;
} else {
Serial.println(F("FAILED"));
AT_cmd_time = 0;
}
AT_cmd_result = false;
return status;
}
Output from the serial monitor:
10:55:14.928 -> *****************************************************
10:55:15.021 -> ********** Open TCP connection
10:55:15.021 -> AT command:AT+CIPMUX=1
10:55:15.068 -> Result:DONE
10:55:15.068 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
10:55:15.115 -> Result:DONE
10:55:15.162 -> AT command:AT+CIPSEND=0,39
10:55:15.162 -> Result:DONE
10:55:15.209 -> ********** requesting URL: GET /dahls/Ymse/uno.php?v1=17&v2=66
10:55:17.174 -> AT command:AT+CIPCLOSE=0
10:55:17.221 -> Result:DONE
10:55:17.221 -> ********** Close TCP Connection
10:55:17.268 -> *****************************************************
10:55:17.315 -> OK: 5, Failed: 11
10:55:42.306 ->
10:55:42.306 ->
10:55:42.306 -> *****************************************************
10:55:42.353 -> ********** Open TCP connection
10:55:42.399 -> AT command:AT+CIPMUX=1
10:55:42.399 -> Result:DONE
10:55:42.399 -> AT command:AT+CIPSTART=0,"TCP","192.168.2.22",80
10:55:42.493 -> Result:DONE
10:55:42.493 -> AT command:AT+CIPSEND=0,39
10:55:42.540 -> Result:DONE
10:55:42.540 -> ********** requesting URL: GET /dahls/Ymse/uno.php?v1=42&v2=47
10:55:44.552 -> AT command:AT+CIPCLOSE=0
10:55:44.552 -> Result:DONE
10:55:44.599 -> ********** Close TCP Connection
10:55:44.599 -> *****************************************************
10:55:44.693 -> OK: 6, Failed: 11