Home:ALL Converter>Struggling with GET request between Arduino and ESP8266

Struggling with GET request between Arduino and ESP8266

Ask Time:2021-01-29T07:22:17         Author:DaniM

Json Formatter

Hello :) I know there are a few threads about the problem I'm going to explain. I've tried to understand similar problems like mine in Stackoverflow and other websites but I haven't succeeded, so please I'd be very thankful if somebody could help me.

I want to send some values from an Arduino through an ESP8266 to my website via GET request. Manually writing the GET request after the IP, the website works, so I discard problems with my php file (you can check the picture at the bottom).

However, through Arduino and ESP8266, I can establish TCP connection but my HTTP request is not sent.

    #include <SoftwareSerial.h>

    SoftwareSerial ESP_Serial(2,3) ; //SoftwareSerial(receivePin, transmitPin). RX ESP8266 = pin3 Arduino (TX), TX ESP8266 = pin2Arduino (RX)
    String ssid = "aaa"; //Wifi network
    String password = "xxx"; //Wifi password
    String server = "192.168.1.49"; //IP of the server 
    String uri = "/IoT/index.php"; // URI is a string that provides a unique address (either on the Internet or on another private network
    String data;
    const int max_size_response = 500; //maximum size of the response of the web server
    const int timeout_response = 10000; //timeout for HTTP response
    
    int T = 24;
    int H = 56;
    int C = 240;
    int P = 506;
    int S = 47;
      
    void setup() {
      Serial.begin(9600); //baudrate Serial Arduino
      ESP_Serial.begin(9600); //baudrate ESP8266
      resetESP(); //function to reset the ESP8266
      //checking ESP8266 response. SoftwareSerial inheritates from Stream class, that is why I can use setTimeout and find()
      ESP_Serial.setTimeout(3000);
      wifiConnection(); //function to connect ESP8266 to our Wifi
      delay(2000);
      ESP_Serial.setTimeout(2000);
    }
    
    void loop() {
      //replace it by a function for the main file
      String request; 
      //Connect to the web server
      ESP_Serial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//connect to the server, with a specific IP, establishing TCP connection (port 80) 
      
      if (ESP_Serial.find("OK") ) {
        Serial.println("TCP ready");
        //preparing the HTTP request
        data ="?T="+String(T)+"&H="+String(H)+"&C="+String(C)+"&P="+String(P)+"&S="+String(S);
        request = "GET "+uri+data+" HTTP/1.1\r\n";
        request += "Host: "+server+"\r\n\r\n";//+"Connection: keep-alive\r\n\r\n";
        //GET /IoT/index.php?T=23&H=43&C=300&P=200&S=25 HTTP/1.1\r\nHost: 192.168.1.49\r\n\r\n; 
        
        //sending the HTTP request (before sending, it is required to specify the size in chars). When we get ">", we can send
        ESP_Serial.print("AT+CIPSEND=");
        int length_req = request.length()-6;
        ESP_Serial.println(String(length_req)); //83char-6 (as \r or \n is 1 char, not 2) = 77
        
        if (ESP_Serial.find(">")) { //ready to send
          Serial.println("Sending HTTP request"); //delete when it works
          ESP_Serial.println(request);
          if (ESP_Serial.find("SEND OK")) { //when we get this, the request has been sent
            //We have to wait for the response. If timeout or size exceeded,close(bad).If no timeout or size and received CLOSED, ok
            Serial.println("HTTP request sent"); 
            long start_response_check = millis();
            String response = ""; //HTTP response
            while ((ESP_Serial.available()>0) && ((millis()-start_response_check) < timeout_response) && (response.length() < max_size_response)) {
              char ESP_Serial_ch = ESP_Serial.read();
              response.concat(ESP_Serial_ch); //appends the parameter to the string
            }
            if ((millis()-start_response_check) >= timeout_response){
              Serial.println("timeout exceeded");
              ESP_Serial.println("AT+CIPCLOSE");
            }
            if (response.length() < max_size_response){
              Serial.println("size of the response exceeded"); 
              ESP_Serial.println("AT+CIPCLOSE");
            }
            if (response.indexOf("CLOSED") > 0) { //indexOf is used to find a determined string within a string
              Serial.println("response received"); //delete it if code is big
            }
          }
          else {
            Serial.println("Request not sent");
            //ESP_Serial.println("AT+CIPCLOSE");
          }
        }
        else{
          Serial.println("Not ready to send the HTTP request");
          //ESP_Serial.println("AT+CIPCLOSE");
        }
      }
    }

    void resetESP() {
      ESP_Serial.println("AT+RST");
      delay(1000);
      if (ESP_Serial.find("OK")){
        Serial.println("Module has been reset");
      }
    }
    
    void wifiConnection() {
      bool connected = false;
      while (connected==false){
        ESP_Serial.println("AT");
        if (ESP_Serial.find("OK")) {
          Serial.println("ESP responding"); //delete if size exceeding
          //WIFI CONNECTION
          //ESP_Serial.println("AT+RST"); //reset the module
          ESP_Serial.println("AT+CWMODE=1"); //Setting ESP8266 to Station mode (for only Wifi client)
          ESP_Serial.println("AT+CWJAP=\"" +ssid+"\",\"" + password + "\"");// \" means ",I want to introduce a string within a string. CWJAP(Wifi name,password) is for connecting to a Wifi net 
          ESP_Serial.setTimeout(8000); 
          if (ESP_Serial.find("OK")) {
            Serial.println("Connected to the Wifi network"); //delete if size exceeding
          }
          else {
            Serial.println("NOT CONNECTED!!!"); //Add an attempt if the code is not big
          }
          
          //Disable multiconnections (enable=1 is for using ESP as server)
          ESP_Serial.setTimeout(2000); 
          ESP_Serial.println("AT+CIPMUX=0"); //we could add another OK if statement if code is not exceeded
          connected = true;
          delay(2000);
          }
        else {
          Serial.println("ESP NOT responding!!!"); //Add an attempt if the code is not big
        }
      }
    }

In SerialMonitor, I get this:

enter image description here

Manually introducing the GET request after the IP, it works:

enter image description here

Thank you very much in advance!

Author:DaniM,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65946632/struggling-with-get-request-between-arduino-and-esp8266
yy