首页 > 解决方案 > 在 Arduino 和 ESP8266 之间处理 GET 请求

问题描述

您好 :) 我知道有一些关于我要解释的问题的主题。我试图在 Stackoverflow 和其他网站上理解类似的问题,但我没有成功,所以如果有人能帮助我,我会非常感激。

我想通过 ESP8266 将一些值从 Arduino 通过 GET 请求发送到我的网站。在IP之后手动写GET请求,网站正常,所以我丢弃了我的php文件的问题(你可以检查底部的图片)。

但是,通过 Arduino 和 ESP8266,我可以建立 TCP 连接,但我的 HTTP 请求没有发送。

    #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
        }
      }
    }

在 SerialMonitor 中,我得到了这个:

在此处输入图像描述

在IP后面手动引入GET请求,就可以了:

在此处输入图像描述

非常感谢您!

标签: arduinoesp8266software-serial

解决方案


推荐阅读