OpenWeatherMap provide easy-to-work weather APIs. Before you can use the APIs, you have to sign-up your account with email, for free.
After sign-up, and email varified, you can get various weather info using APIs with your api key. For example to get current weather:
http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=<your key here>
This example with your api key will be sent you in the confirm email. You can paste it in browser, to receive the response in JSON form.
Copy the returned JSON to the input box in ArduinoJson Assistant, it will return you the Memory pool size, and example to Parsing program.
ArduinoJson is a simple and efficient JSON library for embedded C++. Support various platforms of Arduino framework. Already included in Platform IO.
For connection between ESP8266 and the I2C OLED, refer last post "ESP8266 (NodeMCU/ESP8266 DevKitC) + SSD1306 I2C OLED, Platform IO".
Here is my exercise run on ESP8266 (NodeMCU), to get current weather data from OpenWeatherMap, using libraries ESP8266WiFi, ESP8266HTTPClient, ArduinoJson, and display on I2C OLED using library ESP8266_SSD1306.
#include <Arduino.h>
#include "SSD1306Wire.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "station";
const char* password = "password";
const String api_1 = "http://api.openweathermap.org/data/2.5/weather?q=";
const String qLocation = "London,uk";
const String api_2 = "&units=metric"; //request return temp in celsius
const String api_3 = "&APPID=";
const String api_key = "09816ba66b72ec172f3b8e1982c962ff"; //your api key
const String rqs = api_1 + qLocation + api_2 + api_3 + api_key;
SSD1306Wire display(0x3c, D3, D5);
//Visit ArduinoJson Assistant (https://arduinojson.org/v6/assistant/)
//To get Memory pool size
StaticJsonDocument<800> doc;
void sendRqs(){
HTTPClient http;
http.begin(rqs);
int httpCode = http.GET();
if (httpCode > 0) {
String response = http.getString();
//display.clear();
//display.drawString(0, 0, "response:");
//display.drawString(0, 10, response);
//display.display();
Serial.println(response);
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, response);
// Test if parsing succeeds.
if (error) {
String errorStr = error.c_str();
display.clear();
display.drawString(0, 0, "deserializeJson() error:");
display.drawString(0, 10, errorStr);
display.display();
Serial.println(errorStr);
delay(10000);
}else{
Serial.println("deserializeJson() no error.");
//--- Copy from ArduinoJson Assistant
float coord_lon = doc["coord"]["lon"];
float coord_lat = doc["coord"]["lat"];
JsonObject weather_0 = doc["weather"][0];
int weather_0_id = weather_0["id"];
const char* weather_0_main = weather_0["main"];
const char* weather_0_description = weather_0["description"];
const char* weather_0_icon = weather_0["icon"];
const char* base = doc["base"];
JsonObject main = doc["main"];
float main_temp = main["temp"];
float main_feels_like = main["feels_like"];
float main_temp_min = main["temp_min"];
float main_temp_max = main["temp_max"];
int main_pressure = main["pressure"];
int main_humidity = main["humidity"];
int visibility = doc["visibility"];
float wind_speed = doc["wind"]["speed"];
int wind_deg = doc["wind"]["deg"];
int clouds_all = doc["clouds"]["all"];
long dt = doc["dt"];
JsonObject sys = doc["sys"];
int sys_type = sys["type"];
int sys_id = sys["id"];
const char* sys_country = sys["country"];
long sys_sunrise = sys["sunrise"];
long sys_sunset = sys["sunset"];
int timezone = doc["timezone"];
long id = doc["id"];
const char* name = doc["name"];
int cod = doc["cod"];
//--- End of ArduinoJson Assistant ---
// Print values.
Serial.println("temp_min: " + String(main_temp_min));
Serial.println("temp_max: " + String(main_temp_max));
display.clear();
display.drawString(0, 0, name);
display.drawString(0, 10, "temp_min: " + String(main_temp_min));
display.drawString(0, 20, "temp_max: " + String(main_temp_max));
display.display();
}
}else{
display.clear();
display.drawString(0, 0, "http.GET() == 0");
display.display();
Serial.println("http.GET() == 0");
}
http.end(); //Close connection
}
void setup() {
Serial.begin(9600);
display.init();
display.setContrast(255);
display.clear();
display.drawString(0, 0, "ESP32/OLED");
display.display();
WiFi.begin(ssid, password);
display.drawString(0, 10, "Connecting...");
display.display();
while (WiFi.status() != WL_CONNECTED) {
delay(800);
}
}
void loop() {
display.clear();
if (WiFi.status() == WL_CONNECTED) {
display.drawString(0, 0, "CONNECTED");
display.display();
sendRqs();
}else{
display.drawString(0, 0, "NOT CONNECTED");
display.display();
}
Serial.println("DONE");
delay(10000);
}