如何使用arduino Uno和ESP8266修复“ HTTP / 1.1 400错误的请求”-01

时间:2019-01-03 04:24:11

标签: https arduino get http-status-code-400 arduino-esp8266

我正在尝试从darksky api检索数据以用于小型arduino项目。我正在使用Arduino UNO和ESP8266开发板..我能够通过开发板连接到Internet,但是在提出获取请求时我非常困惑。

我尝试了许多不同的字符串组合来尝试使请求工作。.尽管尽了最大努力,但状态始终为“意外响应:HTTP / 1.1 400错误请求”。我在下面附加了我的代码。感谢您的帮助。

#include <ArduinoHttpClient.h>
#include "WiFiEsp.h"

#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"

// set up software serial to allow serial communication to our TX and RX pins
SoftwareSerial Serial1(10, 11);
#endif

// Set  baud rate of so we can monitor output from esp.
#define ESP8266_BAUD 9600


// CHANGE THIS TO MATCH YOUR SETTINGS
char ssid[] = "SSID";
char pass[] = "PASS";
char serverAddress[] = 
"https://api.darksky.net/forecast/KEY/44.64,-63.57?exclude=hourly,minutely,daily,alerts,flags";
int status = WL_IDLE_STATUS;

// Define an esp server that will listen on port 443 (https)
//WiFiEspServer server(443);
WiFiEspClient client;

void setup(){
// Open up communications for arduino serial and esp serial at same rate
Serial.begin(9600);
Serial1.begin(9600);

// Initialize the esp module
WiFi.init(&Serial1);

// Start connecting to wifi network and wait for connection to complete
while (status != WL_CONNECTED)
{
    Serial.print("Conecting to wifi network: ");
    Serial.println(ssid);

    status = WiFi.begin(ssid, pass);
}
digitalWrite(13,1);

// Once we are connected log the IP address of the ESP module
Serial.print("IP Address of ESP8266 Module is: ");
Serial.println(WiFi.localIP());
Serial.println("You're connected to the network");


client.setTimeout(10000);
if (!client.connect("api.darksky.net", 443)) {
  Serial.println(F("Connection failed"));
  return;
}
else{
  Serial.println("--- connected to api.darksky.net on port 443 ---");
}

  // Send HTTP request
client.println(F("GET /forecast/KEY/44.64,-63.57 HTTP/1.0"));
client.println(F("Host: api.darksky.net.org"));
client.println(F("Connection: close"));
if (client.println() == 0) {
  Serial.println(F("Failed to send request"));
  return;
}
else{
  Serial.println("--- Request sent ---");
}

// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
  Serial.print(F("Unexpected response: "));
  Serial.println(status);
  return;
}

}

void loop(){
}

感谢您的帮助。我真的很感谢一些见识。我不确定问题是否出在我的字符串中。或即时通讯使用HTTP客户端访问HTTPS资料。再次感谢。

0 个答案:

没有答案