I'm doing a project consisting of capturing temperature values and sending them to an API. The main problem I'm having is Arduino Uno send AT commands to Esp8266 to get it connected. Here's my code:
void loop(){
reset();
while(connection == false)
connectWifi();
connection = false;
httppost();
}
void connectWifi(){
String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"";
delay(300);
esp.println(cmd);
if(esp.find("OK")){
Serial.println("Wifi Connected");
connection = true;
}else{
Serial.println("Wifi not Connected");
connection = false;
}
}
void httppost(){
String postRequest =
"POST " + uri + " HTTP/1.1\r\n" +
"Host: " + server + ":" + port + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n" +
data;
String tcpStart = "AT+CIPSTART=\"TCP\",\"" + ip + "\"," + port;
String sendCmd = "AT+CIPSEND=" + postRequest.length();
esp.println(tcpStart); //start TCP connection
delay(300);
if(esp.find("OK")){
Serial.println("TCP connection OK");
esp.println(sendCmd); //send the data over TCP connection
delay(300);
if(esp.find(">")){
Serial.println("Sending packet");
esp.println(postRequest);
delay(300);
if(esp.find("SEND OK")){
Serial.println("Packet sent");
while(esp.available()){ //number of bytes/char available for reading
char tmpResp = esp.read(); //read one char at the time
Serial.print(tmpResp);
if (tmpResp == '\0') continue; //terminate the while when end of the data
}
esp.println("AT+CIPCLOSE"); //close TCP connection
}else{
Serial.println("An error occured while sending packet");
}
}else{
Serial.println("ESP8266 is not listening for incoming data");
}
}else{
Serial.println("Cannot initiate TCP connection");
}
}
Here's an output example I get:
So the problem seems to be the execution of the AT command: AT+CIPSTART. I've done several searches but I can't figure out why it isn't working.