honey_the_codewitch

open_weather api

Jun 12th, 2022 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. // open_weather.hpp
  2. #pragma once
  3. #ifndef ESP32
  4. #error "This library only supports the ESP32 MCU."
  5. #endif
  6. #include <Arduino.h>
  7. namespace arduino {
  8. struct open_weather_info final {
  9.     float temperature;
  10.     float feels_like;
  11.     float pressure;
  12.     float humidity;
  13.     float visiblity;
  14.     float wind_speed;
  15.     float wind_direction;
  16.     float wind_gust;
  17.     float cloudiness;
  18.     float rain_last_hour;
  19.     float snow_last_hour;
  20.     long utc_offset;
  21.     time_t timestamp;
  22.     time_t sunrise;
  23.     time_t sunset;
  24.     char main[32];
  25.     char city[64];
  26.     char description[128];
  27. };
  28. struct open_weather final {
  29.     static bool fetch(float latitude, float longitude, open_weather_info* out_info);
  30. };
  31. }
  32. // open_weather.cpp
  33. #include <open_weather.hpp>
  34. #include <ArduinoJson.hpp>
  35. #include <HTTPClient.h>
  36. using namespace ArduinoJson;
  37. namespace arduino {
  38. bool open_weather::fetch(float latitude, float longitude,open_weather_info* out_info) {
  39.     constexpr static const char *url_format =
  40.         "http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=metric&lang=en&appid=APIKEY";
  41.     if(out_info==nullptr) {
  42.         return false;
  43.     }
  44.     char url[512];
  45.     sprintf(url,url_format,latitude,longitude);
  46.     HTTPClient client;
  47.     Serial.println(url);
  48.     client.begin(url);
  49.     if(0>=client.GET()) {
  50.         return false;
  51.     }
  52.     DynamicJsonDocument doc(8192);
  53.     deserializeJson(doc,client.getString());
  54.     client.end();
  55.     JsonObject obj = doc.as<JsonObject>();
  56.     String str = obj[F("name")];
  57.     strncpy(out_info->city,str.c_str(),64);
  58.     out_info->visiblity = obj[F("visibility")];
  59.     out_info->utc_offset = (long)obj[F("timezone")];
  60.     out_info->timestamp = (time_t)(long)obj[F("dt")];
  61.     JsonObject so = obj[F("weather")].as<JsonArray>().getElement(0).as<JsonObject>();
  62.     str=so[F("main")].as<String>();
  63.     strncpy(out_info->main,str.c_str(),32);
  64.     str=so[F("description")].as<String>();
  65.     strncpy(out_info->description,str.c_str(),128);
  66.     so = obj[F("main")].as<JsonObject>();
  67.     out_info->temperature = so[F("temp")];
  68.     out_info->feels_like  = so[F("feels_like")];
  69.     out_info->pressure = so[F("pressure")];
  70.     out_info->humidity = so[F("humidity")];
  71.     so = obj[F("wind")].as<JsonObject>();
  72.     out_info->wind_speed = so[F("speed")];
  73.     out_info->wind_direction = so[F("deg")];
  74.     out_info->wind_gust = so[F("gust")];
  75.     so = obj[F("clouds")].as<JsonObject>();
  76.     out_info->cloudiness = so[F("all")];
  77.     so = obj[F("sys")];
  78.     out_info->sunrise = (time_t)(long)so[F("sunrise")];
  79.     out_info->sunset = (time_t)(long)so[F("sunset")];
  80.     return true;
  81. }
  82. }
Add Comment
Please, Sign In to add comment