jscrane / WifiWeatherGuy

WeatherGuy for ESP8266
Apache License 2.0
1 stars 0 forks source link

Getting nearest weather station #12

Closed jscrane closed 6 years ago

jscrane commented 6 years ago
jscrane commented 6 years ago
const size_t bufferSize = JSON_OBJECT_SIZE(14) + 290;
DynamicJsonBuffer jsonBuffer(bufferSize);

const char* json = "{\"as\":\"AS6830 Liberty Global Operations B.V.\",\"city\":\"Dublin\",\"country\":\"Ireland\",\"countryCode\":\"IE\",\"isp\":\"Virgin Media Ireland\",\"lat\":53.3331,\"lon\":-6.2489,\"org\":\"Virgin Media Ireland\",\"query\":\"89.101.216.154\",\"region\":\"L\",\"regionName\":\"Leinster\",\"status\":\"success\",\"timezone\":\"Europe/Dublin\",\"zip\":\"D02\"}";

JsonObject& root = jsonBuffer.parseObject(json);

const char* as = root["as"]; // "AS6830 Liberty Global Operations B.V."
const char* city = root["city"]; // "Dublin"
const char* country = root["country"]; // "Ireland"
const char* countryCode = root["countryCode"]; // "IE"
const char* isp = root["isp"]; // "Virgin Media Ireland"
float lat = root["lat"]; // 53.3331
float lon = root["lon"]; // -6.2489
const char* org = root["org"]; // "Virgin Media Ireland"
const char* query = root["query"]; // "89.101.216.154"
const char* region = root["region"]; // "L"
const char* regionName = root["regionName"]; // "Leinster"
const char* status = root["status"]; // "success"
const char* timezone = root["timezone"]; // "Europe/Dublin"
const char* zip = root["zip"]; // "D02"
jscrane commented 6 years ago
    // if cfg.use_nearest...
    const __FlashStringHelper *host = F("ip-api.com");
    if (client.connect(host, 80)) {
        client.print(F("GET /json HTTP/1.1\r\nHost: "));
        client.print(host);
        client.print(F("\r\nConnection: close\r\n\r\n"));
        if (client.connected()) {
            unsigned long now = millis();
            while (!client.available())
                if (millis() - now > 5000)
                    return false;
            client.find("\r\n\r\n");
            // ...
            JsonObject &root = buffer.parseObject(client);
            // if success, decode...
            float lat = root["lat"];
            float lon = root["lon"];
            snprintf(cfg.station, sizeof(cfg.station), "%f,%f", lat, lon);
        }
    }