0

Darksky weather forecast, AIO service, location setup. 400 e
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Darksky weather forecast, AIO service, location setup. 400 e

by chrim on Mon Sep 02, 2019 3:10 am

Hi,

I'm trying to use the darksky weather service without success and I've no doubt it will be down to me. I can very happily send json data and display it in a dashboard (https://io.adafruit.com/chrim/dashboard ... ation-data)..

I'm trying to make the call to set up the weather location as documented as:

Code: Select all | TOGGLE FULL SIZE
POST /api/v2/:username/integrations/weather
Create a new weather integration record. Request body should be a JSON record in the form:

{
  "weather": {
    "location": locationValue
  }
}
Where locationValue is a location string in latitude,longitude format.


with the following function, it's a tester so hard coded location (to one of my favourite rollercoasters...). IO_HOST, SECRET_IO_USERNAME, SECRET_IO_KEY are all configured correctly and used elsewhere in the sketch to send the other dashboard data. The generated is json is displayed correctly in the monitor:

Code: Select all | TOGGLE FULL SIZE
{
  "weather": {
    "location": "53.599084,-2.839255"
  }
}


However, the function generates a 400 error, "HTTP/1.0 400 Bad request" response, no errors appear in the monitor (https://io.adafruit.com/chrim/monitor) - and no location is created. Function follows, client is a connected instance of WiFiClient. serialMessageHeader() is a little function that helps me neaten up debug messages. Any thoughts would be welcome! Thanks

Code: Select all | TOGGLE FULL SIZE
void setupAIOWeatherLocation() {
/* -
https://io.adafruit.com/services/weather
POST /api/v2/:username/integrations/weather
Create a new weather integration record. Request body should be a JSON record in the form:

{
  "weather": {
    "location": locationValue
  }
}
Where locationValue is a location string in latitude,longitude format.
//
*/
  const int capacity = 2*JSON_OBJECT_SIZE(1);
  StaticJsonDocument<capacity> doc;
 
  JsonObject weather = doc.createNestedObject("weather");
  weather["location"] = "52.986923,-1.883297";
 
  serialMessageHeader();
  Serial.println("Sending weather location to " IO_HOST);
 
  client.setTimeout(10000);
  if (!client.connect(IO_HOST, 80)) {
    serialMessageHeader();
    Serial.println("Connection to " IO_HOST " failed");
    return;
  }
 
  // Send the first line of the request
  // POST /api/v2/:username/integrations/weather
 
  client.println("POST /api/v2/" SECRET_IO_USERNAME "/integrations/weather");
 
  // Send the first part of the HTTP headers
  client.println("Host: " IO_HOST);
  client.println(F("Connection: close"));
 
  client.print(F("Content-Length: "));
  client.println(measureJson(doc));
 
  client.println(F("Content-Type: application/json"));
  client.println(F("X-AIO-Key: " SECRET_IO_KEY));
  client.println();  // Terminate headers with a blank line

  // Send the JSON document in body
  serializeJson(doc, client);
 
  //debug show the json in serial
  serializeJsonPretty(doc, Serial);
 
  // Check the  HTTP status (should be "HTTP/1.1 200 OK")
  char status[32] = {0};
  client.readBytesUntil('\r', status, sizeof(status));
  if (strcmp(status + 9, "200 OK") != 0) {
    serialMessageHeader();
    Serial.print(F("Unexpected status from AIO: "));
    Serial.println(status);
    return;
  }

  // Close the connection
  client.stop();

  serialMessageHeader();
  Serial.println("Done.");


}

chrim
 
Posts: 3
Joined: Mon Feb 16, 2015 8:40 am

Please be positive and constructive with your questions and comments.