Skip to content

Instantly share code, notes, and snippets.

@bertmelis
Created January 19, 2023 13:47
Show Gist options
  • Select an option

  • Save bertmelis/ba0d7c1c341b7259ebc712139888b1a2 to your computer and use it in GitHub Desktop.

Select an option

Save bertmelis/ba0d7c1c341b7259ebc712139888b1a2 to your computer and use it in GitHub Desktop.
Get Open Weather Map data into Arduino, requires ArduinoJson and an OWM API key.
#include "openweathermap.h"
bool fc_ready = false;
size_t d1 = 0;
size_t d2 = 0;
size_t d3 = 0;
float fc_temp_d1 = 0;
float fc_temp_d2 = 0;
float fc_temp_d3 = 0;
char fc_icon_cur[4] = "01d";
char fc_descr_cur[50];
char fc_icon_d1[4] = "01d";
char fc_icon_d2[4] = "01d";
char fc_icon_d3[4] = "01d";
WiFiClientSecure socket;
HTTPClient https;
const char url[] = "https://api.openweathermap.org/data/2.5/onecall?lat=52.169613&lon=21.113685&units=metric&exclude=minutely,hourly&appid=<YOUR_OWM_API_KEY>";
const uint8_t fingerprint[] = {0x42, 0x94, 0x24, 0x9c, 0x7b, 0xe9, 0x07, 0xbf, 0xb8, 0xfc, 0xac, 0x61, 0xcb, 0xc7, 0x3d, 0x12, 0x1c, 0xe5, 0x16, 0xe6};
void onOWMData(char* data, size_t len) {
DynamicJsonDocument doc(8192);
DeserializationError error = deserializeJson(doc, data);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
} else {
Serial.print(F("OpenWeatherMap data received\n"));
strncpy(fc_descr_cur, doc["current"]["weather"][0]["description"], 49);
tm* tm;
time_t time;
time = doc["daily"][1]["dt"].as<int>();
tm = localtime(&time);
d1 = tm->tm_wday;
fc_temp_d1 = doc["daily"][1]["temp"]["day"];
strncpy(fc_icon_d1, doc["daily"][1]["weather"][0]["icon"].as<const char*>(), 3);
time = doc["daily"][2]["dt"].as<int>();
tm = localtime(&time);
d2 = tm->tm_wday;
fc_temp_d2 = doc["daily"][2]["temp"]["day"];
strncpy(fc_icon_d2, doc["daily"][2]["weather"][0]["icon"].as<const char*>(), 3);
time = doc["daily"][3]["dt"].as<int>();
tm = localtime(&time);
d3 = tm->tm_wday;
fc_temp_d3 = doc["daily"][3]["temp"]["day"];
strncpy(fc_icon_d3, doc["daily"][3]["weather"][0]["icon"].as<const char*>(), 3);
fc_ready = true;
}
}
void setupOWM() {
memset(fc_descr_cur, 0, 49); // make sure strings are terminated!
memset(fc_icon_cur, 0, 4);
memset(fc_icon_d1, 0, 4);
memset(fc_icon_d2, 0, 4);
memset(fc_icon_d3, 0, 4);
socket.setFingerprint(fingerprint);
}
void updateForecast() {
Serial.print("Requesting OpenWeatherMap data\n");
bool success = false;
char* payload = nullptr;
size_t size = 0;
if (https.begin(socket, url)) {
int httpCode = https.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
size = https.getSize();
if (size > 0) {
payload = new char[size + 1];
memcpy(payload, https.getString().c_str(), size);
payload[size] = 0;
success = true;
} else {
Serial.printf("[HTTP} Payload size invalid\n");
}
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
if (success) {
onOWMData(payload, size + 1);
delete payload;
}
} else {
Serial.println("Forecast connection failed");
}
}
#pragma once
#include <Arduino.h> // for PROGMEM
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
extern size_t d1;
extern size_t d2;
extern size_t d3;
extern float fc_temp_d1;
extern float fc_temp_d2;
extern float fc_temp_d3;
extern char fc_icon_cur[4];
extern char fc_icon_d1[4];
extern char fc_icon_d2[4];
extern char fc_icon_d3[4];
extern char fc_descr_cur[50];
extern bool fc_ready;
void setupOWM();
void updateForecast();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment