Arduino Uno + ESP8266-使用AT命令获取请求

时间:2018-12-21 01:14:41

标签: http get arduino-uno esp8266

我的项目包括将某些传感器读取的一些数据传递给具有Arduino Uno板和ESP8266模块的服务器。我已经设法使用Thingspeak.com发送数据,但是当我尝试使用自己的服务器时,连接失败。我认为错误是网址中的某个地方。该服务器可以正常运行,并尝试使用PostMan。

此刻,我只是尝试将1作为值传递。

整个URL为:http://arduino.otherperceptions.com/api/statistic/store?product_id=1&sensor_humidity=1&sensor_temperature=1&sensor_pressure=1&sensor_audio=1&sensor_vibration=1

串行监视器的输出:

AT+CIPSTART=4,"TCP","arduino.otherperceptions.com",80

4,CONNE
AT+CIPSEND=4,143

OK
>
Recv 143 bytes

SEND OK

+IPD,AT+CIPMUX=1

代码:

#include <SoftwareSerial.h>
#include <dht.h>


#define RX 10
#define TX 11
String AP = "***";
String PASS = "****";
String PORT = "80";

SoftwareSerial esp8266(RX,TX); 

void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);

}
void loop() {


 esp8266.println("AT+CIPMUX=1");

    printResponse();

    esp8266.println("AT+CIPSTART=4,\"TCP\",\"arduino.otherperceptions.com\",80");
    delay(1000);
    printResponse();

    String cmd = "GET /api/statistic/store?product_id=1&sensor_humidity=1&sensor_temperature=100&sensor_pressure=1&sensor_audio=1&sensor_vibration=1 HTTP/1.1";
    esp8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
    delay(1000);

    esp8266.println(cmd);
    delay(1000);
    esp8266.println(""); 
     delay(3000);
}


void printResponse() {
  while (esp8266.available()) {
    Serial.println(esp8266.readString()); 
  }
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }

    countTimeCommand++;
  }

  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
 }

0 个答案:

没有答案