0

My project aims to send HTTP GET request to web page(using Arduino UNO and ESP8266-01) then receive response, extract wanted values from response and react with(for example turn on LED1,LED3) then resend led's status to another web page to be inserted in mysql database using HTTP POST in order to make sure that Arduino received the command and executed successfully (in an infinitive loop). Each request HTTP GET or HTTP POST works fine alone, but when trying to do that in a sequence HTTP GET then HTTP POST inside the loop I got bad result shown in serial monitor like unknown strange characters. Here is my loop :

void loop() {
  if (millis() - lastTimeMillis > 40000) {
    lastTimeMillis = millis();
    ESP8266.println("AT+CIPMUX=1");
    delay(1000);
    printResponse();
    ESP8266.println("AT+CIPSTART=4,\"TCP\",\"" + server + "\",80");
    delay(1000);
    printResponse();
    String cmd = "GET /ard/sensor.php HTTP/1.1\r\nHost: localhost";
    ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
    delay(1000);
    ESP8266.println(cmd);
    delay(1000);
    ESP8266.println("");    
  }
    if (ESP8266.available()){
    Serial.write(ESP8266.read());
    }
  Value1=(digitalRead (LED_PIN1));
  Value2=(digitalRead (LED_PIN2));
  Value3=(digitalRead (LED_PIN3));
  Value4=(digitalRead (LED_PIN4)); 
 data = "Value1=" + Value1 + "&Value2=" + Value2 + "&Value3=" + Value3  + "&Value4=" + Value4;
  httppost();
}

And here is

httppost()

void httppost () {

    ESP8266.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");
    if( ESP8266.find("OK")) {
    Serial.println("TCP connection ready");
    }
    String postRequest =
    "POST " + uri + " HTTP/1.0\r\n" +
    "Host: " + server + "\r\n" +
    "Accept: *" + "/" + "*\r\n" +
    "Content-Length: " + data.length() + "\r\n" +
    "Content-Type: application/x-www-form-urlencoded\r\n" +
    "\r\n" + data;
    String sendCmd = "AT+CIPSEND=";
    ESP8266.print(sendCmd);
    ESP8266.println(postRequest.length() );
    delay(5000);
    if(ESP8266.find(">")) { Serial.println("Sending.."); ESP8266.print(postRequest);    
    if( ESP8266.find("SEND OK")) { Serial.println("Packet sent");    
    while (ESP8266.available()) {    
    String tmpResp = ESP8266.readString();    
    Serial.println(tmpResp);    
    }    
    ESP8266.println("AT+CIPCLOSE");
    }
    }
}

When uploading this code it seems like only HTTP POST works fine in a loop while HTTP GET having a trouble. So how to make them both work fine in a loop? Thanks in advance

Your Answer

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

Browse other questions tagged or ask your own question.