Get location forecast data from the API of MET Norway.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
https://github.com/sebastian-xyz/ESPWeather.git
ESPWeather provides two main class variants for weather data:
-
Standard classes:
Weather
,WeatherData
These usefloat
arrays for storing weather data and are suitable for most ESP32 applications. -
Reduced FootPrint (RFP) classes:
WeatherRFP
,WeatherDataRFP
These are designed for environments with limited memory.- Data is stored in
int16_t
arrays and scaled by afactor
(see theESPWeatherRFPFactor*
defines in the code). - The RFP classes use the same API as the standard classes, so you can switch between them with minimal code changes.
- To use the RFP classes, include
weather_rfp.hpp
instead of the standard headers and replaceWeather
with `WeatherRFP. - Additionally
WeatherDataRFP
providesint16_6 get_current_raw()
andint16_t get_val_at_hour_raw(uint8_t hour)
to gain direct access to the stored values
- Data is stored in
RFP Defines and Default Values:
Define | Default Value | Description |
---|---|---|
ESPWeatherRFPFactorTemperature |
500.0f | Temperature (°C × 500) |
ESPWeatherRFPFactorDewPoint |
500.0f | Dew point (°C × 500) |
ESPWeatherRFPFactorPrecipitation |
1000.0f | Precipitation (mm × 1000) |
ESPWeatherRFPFactorWindSpeed |
100.0f | Wind speed (m/s × 100) |
ESPWeatherRFPFactorWindDirection |
50.0f | Wind direction (degrees × 100) |
ESPWeatherRFPFactorAirPressure |
10.0f | Air pressure (hPa × 10) |
ESPWeatherRFPFactorCloudiness |
100.0f | Cloudiness (% × 100) |
ESPWeatherRFPFactorRelativeHumidity |
100.0f | Relative humidity (% × 100) |
These defines set the scaling factors for each weather parameter, allowing the library to store values as scaled integers and save RAM. For example, a temperature of 23.456°C is stored as 11728
when using a factor of 500.
How to change the scaling factors:
To override any of these default values, simply#define
the desired value before includingweather_rfp.hpp
in your code.
For example:#define ESPWeatherRFPFactorTemperature 100.0f #include <weather_rfp.hpp>
Example sketch:
#include <Arduino.h>
#include <weather.hpp>
#include <WiFi.h>
Weather *weather_status;
void setup() {
Serial.begin();
// get connected
...
// get local time set
struct tm local;
configTzTime(TIMEZONE, "pool.ntp.org");
getLocalTime(&local, 10000);
weather_status = new Weather(LONGITUDE, LATITUDE, ALTITUDE);
weather_status->set_utc_offset(1);
if (WiFi.isConnected()) {
weather_status->update_data();
}
}
void loop() {
if (weather_status->is_expired()) {
weather_status->update_data();
}
Serial.print("Current temperature: ");
Serial.print(weather_status->get_temperature()->get_current());
Serial.println("°C);
Serial.print("Maximum temperature next 24 hours: ");
Serial.print(weather_status->get_temperature()->get_maximum());
Serial.println("°C);
Serial.print("Maximum temperature next 24 hours: ");
Serial.print(weather_status->get_temperature()->get_minimum());
Serial.println("°C);
Serial.print("Mean temperature next 24 hours: ");
Serial.print(weather_status->get_temperature()->get_mean());
Serial.println("°C);
}
Data Type | Unit |
---|---|
Temperature | °C |
Precipitation | mm |
Dew point | °C |
Air pressure | hPa |
Wind speed | m/s |
Wind direction | deg |
Cloudiness | % |
Relative humidity | % |
The Weather
class provides an interface to retrieve and manage weather data from the api.met.no weather API.
Return Type | Function | Description |
---|---|---|
Weather(float latitude, float longitude) | Constructor to initialize with latitude and longitude. | |
Weather(float latitude, float longitude, uint16_t altitude) | Constructor with latitude, longitude, and altitude. | |
Weather(uint8_t num_hours, float latitude, float longitude) | Constructor with hours, latitude, longitude. | |
Weather(uint8_t num_hours, float latitude, float longitude, uint16_t altitude) | Constructor with hours, latitude, longitude, altitude. | |
~Weather() | Destructor. | |
bool | is_expired(void) | Checks if the weather data is expired. |
void | update_data(void) | Updates the weather data from the API. |
void | update_location(float latitude, float longitude) | Updates the location (latitude and longitude). |
void | update_location(float latitude, float longitude, uint16_t altitude) | Updates the location with latitude, longitude, and altitude. |
void | set_utc_offset(int8_t utf_offset) | Sets the UTC offset for the location. |
void | set_daylight_saving(bool daylight_saving) | Sets the daylight saving status. |
WeatherData* | get_temperature() | Returns a pointer to temperature data. |
WeatherData* | get_precipitation() | Returns a pointer to precipitation data. |
WeatherData* | get_wind_speeds() | Returns a pointer to wind speed data. |
WeatherData* | get_wind_direction() | Returns a pointer to wind direction data. |
WeatherData* | get_air_pressure() | Returns a pointer to air pressure data. |
WeatherData* | get_cloudiness() | Returns a pointer to cloudiness data. |
WeatherData* | get_relative_humidity() | Returns a pointer to relative humidity data. |
WeatherData* | get_dew_point() | Returns a pointer to dew point data. |
void | setExpiredTime(tm *time) | Sets a pointer to the expiration time structure. |
tm* | getExpiredTime() | Returns a pointer to the expiration time structure. |
void | set_symbol_code_next_1h(String symbol) | Sets a stored symbol code for the next 1 hour. |
void | set_symbol_code_next_6h(String symbol) | Sets a stored symbol code for the next 6 hour. |
void | set_symbol_code_next_12h(String symbol) | Sets a stored symbol code for the next 12 hours. |
String | get_symbol_code_next_1h() | Returns the symbol code for the next 1 hour. |
String | get_symbol_code_next_6h() | Returns the symbol code for the next 6 hours. |
String | get_symbol_code_next_12h() | Returns the symbol code for the next 12 hours. |
Note:
The Weather class does not expose public data members; all interaction is through its public methods.
The WeatherData
class represents weather parameter data for a set of hours, providing statistical and value access methods.
Return Type | Function | Description |
---|---|---|
WeatherData(uint8_t num_hours) | Constructor that initializes with the number of hours. | |
WeatherData(float *vals) | Constructor that initializes with a pointer to values array. | |
~WeatherData() | Destructor. | |
void | update_vals(float *vals) | Updates the internal values array. |
void | update_vals(int16_t *vals) | Updates the internal values array. |
float | get_minimum() | Returns the minimum value. |
float | get_maximum() | Returns the maximum value. |
float | get_mean() | Returns the mean value. |
float | get_std() | Returns the standard deviation. |
float | get_variance() | Returns the variance. |
float | get_val_at_hour(uint8_t hour) | Returns the value at the specified hour. |
int16_t | get_val_at_hour_raw(uint8_t hour) | Returns the value at the specified hour. |
float | get_current() | Returns the value for the current hour. |
int16_t | get_current_raw() | Returns the value for the current hour. |
Note:
The WeatherData class does not expose public data members; all access is through its public methods.
-
Enable Debug Output:
Uncomment or define#define DEBUG_WEATHER
at the top of your source files to enable debug print statements throughout the code. This will give you more insight into what your code is doing at runtime. -
Use Serial Monitor:
Make sure to initializeSerial.begin(115200);
in yoursetup()
function. UseSerial.print()
andSerial.println()
to output variable values and checkpoints. -
Check WiFi Connectivity:
Ensure the ESP32 is connected to WiFi before making HTTP requests. Print the WiFi status and IP address for confirmation.
Tip:
If you encounter persistent issues, try running a minimal example sketch that only tests the failing module or function.