Hi All,
I've searched through many of the other threads on this issue but I'm still stuck. Can someone help me understand what I'm doing wrong? I'm attempting to make a call to the Dark Sky API directly from my ESP. I'm able to connect to the host, create the GET request, but I keep getting a "400 Bad Request" error. Something might be of interest is the fact that I have to comment out "Host: " + host + "\r\n" +
just to get a response which is unlike other calls I've made to other APIs. If I leave that line in the SendGetRequest function, the client is always unavailable, which tells me I don't need that line? But maybe without that line, that's what's throwing me the 400 error? So that is really confusing to me: with that line - nothing; without it - the error. What am I missing?
Here's my code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char* ssid = "mySSID";
const char* password = "myPassword";
const char* api_key = "myKey";
const char* host = "api.darksky.net";
String url = "https://api.darksky.net/forecast/myKey/38.8951,-77.0367?exclude=minutely,hourly,daily,alerts,flags";
const int httpPort = 80;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(1000);
ConnectToWifi(ssid, password);
}
void loop() {
ConnectToHost(host, httpPort, client);
SendGetRequest(url, host, api_key, client);
ReadGetResponse(client);
delay(10000);
}
//this works...
void ConnectToWifi(const char* ssid, const char* password) {
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.disconnect();
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());
}
//this works too...
void ConnectToHost(const char* host, const int httpPort, WiFiClient& client) {
Serial.print("Connecting to ");
Serial.println(host);
client.connect(host, httpPort);
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed!");
return;
}
}
//if I uncomment out the "Host: " + host +"\r\n" line, the client is always unavailable and no response is received.
void SendGetRequest(String url, const char* host, const char* api_key, WiFiClient& client) {
Serial.print("Requesting URL: ");
Serial.print(url);
client.println(String("GET ") + url + " HTTP/1.1\r\n" +
//"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
delay(500);
}
void ReadGetResponse(WiFiClient& client) {
if (client.available()) {
Serial.println("client is available");
while (client.connected()) {
String line = client.readStringUntil('\r');
Serial.print(line);
delay(10);
}
Serial.println();
Serial.println("closing connection");
} else {
Serial.println("client NOT avaialable");
}
}
Here is the output to my serial monitor (when that line above is commented out):
Connecting to api.darksky.net
Requesting URL: How Dark Sky users can use the Apple Weather app - Apple Support
client is available
HTTP/1.1 400 Bad Request
Server: nginx/1.10.0 (Ubuntu)
Date: Fri, 16 Dec 2016 20:42:39 GMT
Content-Type: text/html
Content-Length: 182
Connection: close
400 Bad Request
nginx/1.10.0 (Ubuntu)
closing connection
Here is the output when the line is active:
Connecting to api.darksky.net
Requesting URL: How Dark Sky users can use the Apple Weather app - Apple Support
client NOT available
closing connection