ESP8266 GET请求无效

时间:2017-12-12 11:24:42

标签: http arduino-uno esp8266

我正在尝试通过PHP将数据从使用ESP8266的Arduino Uno发送到Firebase。 Firebase数据库和PHP之间的连接似乎没问题,因为我可以通过浏览器手动向其发送数据。但是,我无法将数据从Arduino发送到PHP。

这是Arduino代码:

#include "SoftwareSerial.h"

String ssid ="xxxx";
String password="xxxx";

String server = "firstfirebase.000webhostapp.com"; // www.example.com
String uri = "/firebaseTest.php";// our example is /esppost.php

SoftwareSerial esp(2, 3);// RX, TX

void setup() {
  esp.begin(9600);
  Serial.begin(9600);
  reset();
  connectWifi();
}

void reset() {
  esp.println("AT+RST");
  delay(1000);
  if(esp.find("OK"))
    Serial.println("Module Reset");
}

void connectWifi() {
  String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  esp.println(cmd);
  delay(4000);
  if(esp.find("OK")) {
    Serial.println("Connected!");
  } else {
    connectWifi();
    Serial.println("Cannot connect to wifi");
  }
}

void loop() {
  httppost();
  delay(1000);
}

void httppost () {
  esp.println("AT+CIPSTART=\"TCP\",\"firstfirebase.000webhostapp.com\",80");//start a TCP connection.
  if( esp.find("OK")) {
    Serial.println("TCP connection ready");
  }
  delay(1000);
  String getRequest = "GET firebaseTest.php?arduino_data=56.00 HTTP/1.1\r\nHost: firstfirebase.000webhostapp.com\r\n\r\n";
  String sendCmd = "AT+CIPSEND";//determine the number of caracters to be sent.
  esp.print(sendCmd);
  esp.println(getRequest.length() );
  delay(500);
  if(esp.find(">")) {
    Serial.println("Sending..");
    esp.print(getRequest+"\r\n\r\n");
    delay(500);
    esp.println("AT+CIPSTATUS");
    if( esp.find("SEND OK")) {
      Serial.println("Packet sent");
      while (esp.available()) {
        String tmpResp = esp.readString();
        Serial.println(tmpResp);
      }
      // close the connection
      esp.println("AT+CIPCLOSE");
    }
  }
}

以下是串口监视器的屏幕截图:

2 个答案:

答案 0 :(得分:1)

您错过了路径前的/

应该是:

 String getRequest = "GET /firebaseTest.php?arduino_data=56.00 HTTP/1.1\r\nHost: firstfirebase.000webhostapp.com\r\n\r\n";

答案 1 :(得分:0)

用户代理很重要:

AT+CIPSTART="TCP","somedomain.it",80

AT+CIPSEND=EXACT NUMBER OF GET REQ

GET /Arduino/ciao.php HTTP/1.1\r\n
Host: 192.168.0.111\r\n
User-Agent: wget/1.12\r\n\r\n