ESP8266客户端连接失败

时间:2018-12-24 23:35:32

标签: http arduino client esp8266

我在向GoPro摄像机发送HTTP GET请求时遇到问题。仅供参考,我已经将其用于3种不同的GoPro摄像机,只是较新的Hero 5出现了问题。这可能很简单,但是客户端连接有效,并且在相机上重置了wifi连接后,HTTP请求也可以正常工作,但是如果ESP8266在第一个WiFi连接之后被重置,则它可以正常连接到WiFi,但是所有HTTP请求返回-1 HTTP代码,客户端无法连接。

我在Visual Studio Code中使用Platformio扩展。

我尝试调用client.flush()以及我能想到的几乎所有其他方法。使命令再次起作用的唯一方法是重置相机上的WiFi连接。如果使用计算机进行连接,则可以从Google Chrome发送命令。我尝试使用常规的ESP8266WiFi库和ESP8266HTTPClient库,如下所示。任何帮助,将不胜感激!谢谢

#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// Use WiFiclient class to create TCP connections
WiFiClient client;
HTTPClient http;

const char* ssid     = "GP25000830";
const char* password = "vault9291";

String wifi_ssid = "GP25000830";
String wifi_password = "vault9291"; 

const char* host = "10.5.5.9";
const int httpPort = 80;


String StartURL = "/gp/gpControl/command/shutter?p=1";


void Trigger() {
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  //Command for starting recording
  client.print(String("GET ") + StartURL + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: RCRemote\r\n" +
               "Connection: close\r\n\r\n");
  Serial.println("Triggering");
  delay(20);
  client.stop();
}


void Trigger_http() {
  http.begin("http://10.5.5.9/gp/gpControl/command/shutter?p=1");
  int httpCode = http.Get();
  Serial.println(httpCode);
  String payload = http.getString();
  Serial.println(payload);
  http.end();
}


void setup() {
  WiFi.disconnect(true);
  WiFi.disconnect();

  Serial.begin(115200);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  client.flush();
}


void loop() {
  Trigger();
  // Trigger_http();
  delay(5000);
}

0 个答案:

没有答案