Skip to content

Commit 5aa7325

Browse files
committed
Reduce memory footprint, allow setting the number of hours in constructor
Move from double -> float
1 parent 923a5f6 commit 5aa7325

File tree

5 files changed

+147
-65
lines changed

5 files changed

+147
-65
lines changed

README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ platform = espressif32
1111
board = esp32dev
1212
framework = arduino
1313
lib_deps =
14-
https://github.com/sebastian-xyz/ESPWeather.git
14+
https://github.com/sebastian-xyz/ESPWeather.git
1515
```
1616

1717

@@ -91,13 +91,15 @@ The `Weather` class provides an interface to retrieve and manage weather data fr
9191

9292
| **Return Type** | _Function_ | Description |
9393
|-------------------------|---------------------------------------------|--------------------------------------------------------------------|
94-
| | _Weather(double latitude, double longitude, String user_agent)_ | Constructor to initialize with latitude, longitude, and user agent.|
95-
| | _Weather(double latitude, double longitude, uint16_t altitude, String user_agent)_ | Constructor with latitude, longitude, altitude, and user agent. |
94+
| | _Weather(float latitude, float longitude)_ | Constructor to initialize with latitude and longitude. |
95+
| | _Weather(float latitude, float longitude, uint16_t altitude)_ | Constructor with latitude, longitude, and altitude. |
96+
| | _Weather(uint8_t num_hours, float latitude, float longitude)_ | Constructor with hours, latitude, longitude. |
97+
| | _Weather(uint8_t num_hours, float latitude, float longitude, uint16_t altitude)_ | Constructor with hours, latitude, longitude, altitude. |
9698
| | _~Weather()_ | Destructor. |
9799
| **bool** | _is_expired(void)_ | Checks if the weather data is expired. |
98100
| **void** | _update_data(void)_ | Updates the weather data from the API. |
99-
| **void** | _update_location(double latitude, double longitude)_ | Updates the location (latitude and longitude). |
100-
| **void** | _update_location(double latitude, double longitude, uint16_t altitude)_ | Updates the location with latitude, longitude, and altitude. |
101+
| **void** | _update_location(float latitude, float longitude)_ | Updates the location (latitude and longitude). |
102+
| **void** | _update_location(float latitude, float longitude, uint16_t altitude)_ | Updates the location with latitude, longitude, and altitude. |
101103
| **void** | _set_utc_offset(int8_t utf_offset)_ | Sets the UTC offset for the location. |
102104
| **void** | _set_daylight_saving(bool daylight_saving)_ | Sets the daylight saving status. |
103105
| **WeatherData\*** | _get_temperature()_ | Returns a pointer to temperature data. |
@@ -125,16 +127,16 @@ The `WeatherData` class represents weather parameter data for a set of hours, pr
125127
| **Return Type** | _Function_ | Description |
126128
|-----------------|-----------------------------------------|--------------------------------------------------------------|
127129
| | _WeatherData(uint8_t num_hours)_ | Constructor that initializes with the number of hours. |
128-
| | _WeatherData(double *vals)_ | Constructor that initializes with a pointer to values array. |
130+
| | _WeatherData(float *vals)_ | Constructor that initializes with a pointer to values array. |
129131
| | _~WeatherData()_ | Destructor. |
130-
| **void** | _update_vals(double *vals)_ | Updates the internal values array. |
131-
| **double** | _get_minimum()_ | Returns the minimum value. |
132-
| **double** | _get_maximum()_ | Returns the maximum value. |
133-
| **double** | _get_mean()_ | Returns the mean value. |
134-
| **double** | _get_std()_ | Returns the standard deviation. |
135-
| **double** | _get_variance()_ | Returns the variance. |
136-
| **double** | _get_val_at_hour(uint8_t hour)_ | Returns the value at the specified hour. |
137-
| **double** | _get_current()_ | Returns the value for the current hour. |
132+
| **void** | _update_vals(float *vals)_ | Updates the internal values array. |
133+
| **float** | _get_minimum()_ | Returns the minimum value. |
134+
| **float** | _get_maximum()_ | Returns the maximum value. |
135+
| **float** | _get_mean()_ | Returns the mean value. |
136+
| **float** | _get_std()_ | Returns the standard deviation. |
137+
| **float** | _get_variance()_ | Returns the variance. |
138+
| **float** | _get_val_at_hour(uint8_t hour)_ | Returns the value at the specified hour. |
139+
| **float** | _get_current()_ | Returns the value for the current hour. |
138140

139141
> **Note:**
140142
> The WeatherData class does not expose public data members; all access is through its public methods.

include/weather.hpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <Arduino.h>
55
#include "weather_data.hpp"
66

7-
#define NUM_HOURS 24
8-
#define ESPWeatherUserAgent "github.com/sebastian-xyz/ESPWeather/v1.0"
7+
#ifndef ESPWeatherNumHours
8+
#define ESPWeatherNumHours 24
9+
#endif
10+
#ifndef ESPWeatherUserAgent
11+
#define ESPWeatherUserAgent "github.com/sebastian-xyz/ESPWeather/v1.1"
12+
#endif
913

1014
class Weather
1115
{
@@ -15,12 +19,12 @@ class Weather
1519
String last_modified;
1620
tm *expired_time;
1721
tm *local_time;
18-
double longitude;
19-
double latitude;
22+
float longitude;
23+
float latitude;
2024
uint16_t altitude;
2125
int8_t utc_offset;
2226
const char *url = "https://api.met.no/weatherapi/locationforecast/2.0/complete";
23-
const uint8_t num_hours = NUM_HOURS;
27+
const uint8_t num_hours;
2428
WeatherData *dew_point;
2529
WeatherData *temperature;
2630
WeatherData *precipitation;
@@ -35,13 +39,16 @@ class Weather
3539
bool daylight_saving;
3640

3741
public:
38-
Weather(double latitude, double longitude, String user_agent);
39-
Weather(double latitude, double longitude, uint16_t altitude, String user_agent);
42+
Weather(float latitude, float longitude);
43+
Weather(float latitude, float longitude, uint16_t altitude);
44+
Weather(uint8_t num_hours, float latitude, float longitude);
45+
Weather(uint8_t num_hours, float latitude, float longitude, uint16_t altitude);
46+
Weather(float latitude, float longitude, uint16_t altitude);
4047
~Weather();
4148
bool is_expired(void);
4249
void update_data(void);
43-
void update_location(double latitude, double longitude);
44-
void update_location(double latitude, double longitude, uint16_t altitude);
50+
void update_location(float latitude, float longitude);
51+
void update_location(float latitude, float longitude, uint16_t altitude);
4552
void set_utc_offset(int8_t utf_offset);
4653
void set_daylight_saving(bool daylight_saving);
4754
WeatherData *get_temperature();

include/weather_data.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ class WeatherData
55
{
66

77
private:
8-
double *vals;
9-
double minimum;
10-
double maxmimum;
11-
double mean;
12-
double variance;
8+
float *vals;
9+
float minimum;
10+
float maxmimum;
11+
float mean;
12+
float variance;
1313
uint8_t num_hours;
1414

1515
public:
1616
WeatherData(uint8_t num_hours);
17-
WeatherData(double *vals);
17+
WeatherData(float *vals);
1818
~WeatherData();
19-
void update_vals(double *vals);
20-
double get_minimum();
21-
double get_maximum();
22-
double get_mean();
23-
double get_std();
24-
double get_variance();
25-
double get_val_at_hour(uint8_t hour);
26-
double get_current();
19+
void update_vals(float *vals);
20+
float get_minimum();
21+
float get_maximum();
22+
float get_mean();
23+
float get_std();
24+
float get_variance();
25+
float get_val_at_hour(uint8_t hour);
26+
float get_current();
2727
};
2828
#endif

src/weather.cpp

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include "weather.hpp"
21
#include "math.h"
32
#include <ArduinoJson.h>
43
#include <HTTPClient.h>
54
#include <WiFiClientSecure.h>
65
#include <memory>
76
#include <time.h>
7+
#include "weather.hpp"
88

99
const char *root_cert =
1010
"-----BEGIN CERTIFICATE-----\n"
@@ -42,8 +42,39 @@ const char *root_cert =
4242
"jjxDah2nGN59PRbxYvnKkKj9\n"
4343
"-----END CERTIFICATE-----\n";
4444

45-
Weather::Weather(double latitude, double longitude)
45+
Weather::Weather(float latitude, float longitude)
46+
{
47+
this->num_hours = ESPWeatherNumHours;
48+
this->user_agent = ESPWeatherUserAgent;
49+
this->longitude = longitude;
50+
this->latitude = latitude;
51+
this->altitude = 0;
52+
this->local_time = new tm;
53+
this->expired_time = new tm;
54+
this->temperature = new WeatherData(this->num_hours);
55+
this->dew_point = new WeatherData(this->num_hours);
56+
this->precipitation = new WeatherData(this->num_hours);
57+
this->wind_speeds = new WeatherData(this->num_hours);
58+
this->wind_direction = new WeatherData(this->num_hours);
59+
this->air_pressure = new WeatherData(this->num_hours);
60+
this->cloudiness = new WeatherData(this->num_hours);
61+
this->relative_humidity = new WeatherData(this->num_hours);
62+
this->last_modified = "";
63+
this->symbol_code_next_1h = "";
64+
this->symbol_code_next_12h = "";
65+
this->symbol_code_next_6h = "";
66+
this->utc_offset = 0;
67+
this->daylight_saving = false;
68+
this->expired_time->tm_year = 0; // Years since 1900
69+
this->expired_time->tm_mon = 0; // Months since January (0-11)
70+
this->expired_time->tm_mday = 0; // Day of the month
71+
this->expired_time->tm_hour = 0;
72+
this->expired_time->tm_min = 0;
73+
this->expired_time->tm_sec = 0;
74+
}
75+
Weather::Weather(uint8_t num_hours, float latitude, float longitude)
4676
{
77+
this->num_hours = num_hours;
4778
this->user_agent = ESPWeatherUserAgent;
4879
this->longitude = longitude;
4980
this->latitude = latitude;
@@ -64,9 +95,46 @@ Weather::Weather(double latitude, double longitude)
6495
this->symbol_code_next_6h = "";
6596
this->utc_offset = 0;
6697
this->daylight_saving = false;
98+
this->expired_time->tm_year = 0; // Years since 1900
99+
this->expired_time->tm_mon = 0; // Months since January (0-11)
100+
this->expired_time->tm_mday = 0; // Day of the month
101+
this->expired_time->tm_hour = 0;
102+
this->expired_time->tm_min = 0;
103+
this->expired_time->tm_sec = 0;
104+
}
105+
Weather::Weather(float latitude, float longitude, uint16_t altitude)
106+
{
107+
this->num_hours = ESPWeatherNumHours;
108+
this->user_agent = ESPWeatherUserAgent;
109+
this->longitude = longitude;
110+
this->latitude = latitude;
111+
this->altitude = altitude;
112+
this->local_time = new tm;
113+
this->expired_time = new tm;
114+
this->temperature = new WeatherData(this->num_hours);
115+
this->dew_point = new WeatherData(this->num_hours);
116+
this->precipitation = new WeatherData(this->num_hours);
117+
this->wind_speeds = new WeatherData(this->num_hours);
118+
this->wind_direction = new WeatherData(this->num_hours);
119+
this->air_pressure = new WeatherData(this->num_hours);
120+
this->cloudiness = new WeatherData(this->num_hours);
121+
this->relative_humidity = new WeatherData(this->num_hours);
122+
this->last_modified = "";
123+
this->symbol_code_next_1h = "";
124+
this->symbol_code_next_12h = "";
125+
this->symbol_code_next_6h = "";
126+
this->daylight_saving = false;
127+
this->utc_offset = 0;
128+
this->expired_time->tm_year = 0; // Years since 1900
129+
this->expired_time->tm_mon = 0; // Months since January (0-11)
130+
this->expired_time->tm_mday = 0; // Day of the month
131+
this->expired_time->tm_hour = 0;
132+
this->expired_time->tm_min = 0;
133+
this->expired_time->tm_sec = 0;
67134
}
68-
Weather::Weather(double latitude, double longitude, uint16_t altitude)
135+
Weather::Weather(uint8_t num_hours, float latitude, float longitude, uint16_t altitude)
69136
{
137+
this->num_hours = num_hours;
70138
this->user_agent = ESPWeatherUserAgent;
71139
this->longitude = longitude;
72140
this->latitude = latitude;
@@ -87,6 +155,12 @@ Weather::Weather(double latitude, double longitude, uint16_t altitude)
87155
this->symbol_code_next_6h = "";
88156
this->daylight_saving = false;
89157
this->utc_offset = 0;
158+
this->expired_time->tm_year = 0; // Years since 1900
159+
this->expired_time->tm_mon = 0; // Months since January (0-11)
160+
this->expired_time->tm_mday = 0; // Day of the month
161+
this->expired_time->tm_hour = 0;
162+
this->expired_time->tm_min = 0;
163+
this->expired_time->tm_sec = 0;
90164
}
91165

92166
void Weather::set_daylight_saving(bool daylight_saving)
@@ -113,12 +187,12 @@ Weather::~Weather()
113187
delete this->relative_humidity;
114188
}
115189

116-
void Weather::update_location(double latitude, double longitude)
190+
void Weather::update_location(float latitude, float longitude)
117191
{
118192
this->longitude = longitude;
119193
this->latitude = latitude;
120194
}
121-
void Weather::update_location(double latitude, double longitude,
195+
void Weather::update_location(float latitude, float longitude,
122196
uint16_t altitude)
123197
{
124198
this->longitude = longitude;
@@ -225,8 +299,7 @@ void Weather::update_data(void)
225299
}
226300

227301
JsonDocument doc;
228-
DeserializationError error = deserializeJson(
229-
doc, https.getStream(), DeserializationOption::Filter(filter));
302+
DeserializationError error = deserializeJson(doc, https.getStream(), DeserializationOption::Filter(filter));
230303

231304
#ifdef DEBUG_WEATHER
232305
Serial.println("Starting deserialization");
@@ -249,14 +322,14 @@ void Weather::update_data(void)
249322
return;
250323
}
251324
JsonArray timeseries = doc["properties"]["timeseries"];
252-
double *temps = new double[this->num_hours + 1];
253-
double *precipitation = new double[this->num_hours + 1];
254-
double *wind_speeds = new double[this->num_hours + 1];
255-
double *wind_directions = new double[this->num_hours + 1];
256-
double *air_pressure = new double[this->num_hours + 1];
257-
double *cloudiness = new double[this->num_hours + 1];
258-
double *relative_humidity = new double[this->num_hours + 1];
259-
double *dew_point = new double[this->num_hours + 1];
325+
float *temps = new float[this->num_hours + 1];
326+
float *precipitation = new float[this->num_hours + 1];
327+
float *wind_speeds = new float[this->num_hours + 1];
328+
float *wind_directions = new float[this->num_hours + 1];
329+
float *air_pressure = new float[this->num_hours + 1];
330+
float *cloudiness = new float[this->num_hours + 1];
331+
float *relative_humidity = new float[this->num_hours + 1];
332+
float *dew_point = new float[this->num_hours + 1];
260333
JsonObject current_timeseries_data;
261334
JsonObject current_timeseries_details;
262335
JsonObject current_timeseries_next_hour_details;

src/weather_data.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
33

44
WeatherData::WeatherData(uint8_t num_hours)
55
{
6-
this->vals = new double[num_hours + 1];
6+
this->vals = new float[num_hours + 1];
77
this->minimum = 0;
88
this->maxmimum = 0;
99
this->mean = 0;
1010
this->variance = 0;
1111
this->num_hours = num_hours;
1212
}
1313

14-
double WeatherData::get_minimum()
14+
float WeatherData::get_minimum()
1515
{
1616
return this->minimum;
1717
}
1818

19-
double WeatherData::get_maximum()
19+
float WeatherData::get_maximum()
2020
{
2121
return this->maxmimum;
2222
}
2323

24-
double WeatherData::get_mean()
24+
float WeatherData::get_mean()
2525
{
2626
return this->mean;
2727
}
2828

29-
double WeatherData::get_variance()
29+
float WeatherData::get_variance()
3030
{
3131
return this->variance;
3232
}
3333

34-
double WeatherData::get_std()
34+
float WeatherData::get_std()
3535
{
3636
return sqrt(this->variance);
3737
}
3838

39-
double WeatherData::get_val_at_hour(uint8_t hour)
39+
float WeatherData::get_val_at_hour(uint8_t hour)
4040
{
4141
return this->vals[hour];
4242
}
@@ -46,11 +46,11 @@ WeatherData::~WeatherData()
4646
delete[] this->vals;
4747
}
4848

49-
void WeatherData::update_vals(double *vals)
49+
void WeatherData::update_vals(float *vals)
5050
{
51-
double mean = 0.0;
52-
double minimum = vals[0];
53-
double maximum = vals[0];
51+
float mean = 0.0;
52+
float minimum = vals[0];
53+
float maximum = vals[0];
5454
for (uint8_t i = 0; i < this->num_hours + 1; ++i)
5555
{
5656
this->vals[i] = vals[i];
@@ -72,7 +72,7 @@ void WeatherData::update_vals(double *vals)
7272
this->minimum = minimum;
7373
if (this->num_hours > 1)
7474
{
75-
double var = 0.0;
75+
float var = 0.0;
7676
for (int i = 1; i < this->num_hours + 1; ++i)
7777
{
7878
var += pow(vals[i] - this->mean, 2);
@@ -83,7 +83,7 @@ void WeatherData::update_vals(double *vals)
8383
}
8484
}
8585

86-
double WeatherData::get_current()
86+
float WeatherData::get_current()
8787
{
8888
return this->vals[0];
8989
}

0 commit comments

Comments
 (0)