Paperd.Ink Library 0.0.5
Library for interacting with Paperd.Ink devices.
Loading...
Searching...
No Matches
PaperdinkUIWeatherClass Class Reference

#include <weather.h>

Inheritance diagram for PaperdinkUIWeatherClass:
PaperdinkUIBaseClass

Public Member Functions

int8_t fetch_data (const char *city, const char *country, const char *owm_api_id, const char *units="M")
 
void display_weather_text_center (GxEPD2_GFX &display, uint16_t x, uint16_t y, uint16_t w)
 
void display_weather_style1 (GxEPD2_GFX &display, uint16_t x, uint16_t y)
 
void display_weather_style2_center (GxEPD2_GFX &display, uint16_t x, uint16_t y, uint16_t w)
 

Public Attributes

char city [20]
 
char country [20]
 
char weather_string [10]
 
float day_max_temp
 
float day_min_temp
 
float rain
 
float snow
 
- Public Attributes inherited from PaperdinkUIBaseClass
const GFXfont * font = &PAPERDINK_FONT_SML
 
const GFXfont * font_bold = &PAPERDINK_FONT_SML_BOLD
 
int primary_color = GxEPD_BLACK
 
int secondary_color = GxEPD_WHITE
 
int tertiary_color = GxEPD_BLACK
 

Detailed Description

Definition at line 8 of file weather.h.

Member Function Documentation

◆ display_weather_style1()

void PaperdinkUIWeatherClass::display_weather_style1 ( GxEPD2_GFX & display,
uint16_t x,
uint16_t y )

Definition at line 133 of file weather.cpp.

134{
135 int16_t xt, yt;
136 uint16_t wt, ht, prev_width = x, prev_height = y;
137
138 display.setFont(font);
139 display.setTextColor(primary_color);
140
141 // Show min and max temperature
142 display.drawBitmap(prev_width, prev_height, thermometer_sml, thermometer_sml_width, thermometer_sml_height, primary_color);
143 prev_width += thermometer_sml_width;
144
145 display.getTextBounds("77", 0, 0, &xt, &yt, &wt, &ht);
146 display.setCursor(prev_width + 10, prev_height + ht + 2);
147 display.printf("%d / %d", (int16_t)Paperdink_Weather.day_max_temp, (int16_t)Paperdink_Weather.day_min_temp);
148 prev_height += thermometer_sml_height;
149
150 prev_width = x; // Reset width for next line
151
152 // TODO: OWM API provides rain/snow in mm only. Convert to in
153 // Show rain
154 display.drawBitmap(prev_width, prev_height + 10, umbrella_sml, umbrella_sml_width, umbrella_sml_height, primary_color);
155 prev_width += umbrella_sml_width;
156
157 display.setCursor(prev_width + 10, prev_height + 10 + ht + 2);
158 display.printf("%.2f mm", Paperdink_Weather.rain);
159 prev_height += umbrella_sml_height + 10;
160
161 prev_width = x; // Reset width for next line
162
163 // Show snow
164 display.drawBitmap(prev_width, prev_height + 10, snowflake_sml, snowflake_sml_width, snowflake_sml_height, primary_color);
165 prev_width += snowflake_sml_width;
166
167 display.setCursor(prev_width + 10, prev_height + 10 + ht + 2);
168 display.printf("%.2f mm", Paperdink_Weather.snow);
169}
const GFXfont * font
Definition ui_base.h:9
const unsigned char snowflake_sml[72]
#define snowflake_sml_height
#define snowflake_sml_width
#define thermometer_sml_height
const unsigned char thermometer_sml[72]
#define thermometer_sml_width
#define umbrella_sml_width
Definition umbrella_sml.h:1
#define umbrella_sml_height
Definition umbrella_sml.h:2
const unsigned char umbrella_sml[72]
Definition umbrella_sml.h:4
PaperdinkUIWeatherClass Paperdink_Weather
Definition weather.cpp:195

◆ display_weather_style2_center()

void PaperdinkUIWeatherClass::display_weather_style2_center ( GxEPD2_GFX & display,
uint16_t x,
uint16_t y,
uint16_t w )

Definition at line 171 of file weather.cpp.

172{
173 const uint8_t *bitmap;
174
175 if (!strcmp(weather_string, "Drizzle")) {
176 bitmap = drizzle_xlrg;
177 } else if (!strcmp(Paperdink_Weather.weather_string, "Thunderstorm") || !strcmp(Paperdink_Weather.weather_string, "Lightning")) {
178 bitmap = lightning_xlrg;
179 } else if (!strcmp(Paperdink_Weather.weather_string, "Rain")) {
180 bitmap = rain_xlrg;
181 } else if (!strcmp(Paperdink_Weather.weather_string, "Snow")) {
182 bitmap = snow_xlrg;
183 } else if (!strcmp(Paperdink_Weather.weather_string, "Clear")) {
184 bitmap = sun_xlrg;
185 } else if (!strcmp(Paperdink_Weather.weather_string, "Clouds")) {
186 bitmap = cloud_xlrg;
187 } else {
188 bitmap = alert_xlrg;
189 DEBUG.printf("Invalid weather: %s\r\n", Paperdink_Weather.weather_string);
190 }
191
192 display.drawBitmap(x + (w - alert_xlrg_width) / 2, y, bitmap, alert_xlrg_width, alert_xlrg_height, primary_color);
193}
#define alert_xlrg_height
Definition alert_xlrg.h:2
#define alert_xlrg_width
Definition alert_xlrg.h:1
const unsigned char alert_xlrg[288]
Definition alert_xlrg.h:4
char weather_string[10]
Definition weather.h:13
const unsigned char cloud_xlrg[288]
Definition cloud_xlrg.h:4
const unsigned char drizzle_xlrg[288]
Definition drizzle_xlrg.h:4
const unsigned char lightning_xlrg[288]
const unsigned char rain_xlrg[288]
Definition rain_xlrg.h:4
const unsigned char snow_xlrg[288]
Definition snow_xlrg.h:4
const unsigned char sun_xlrg[288]
Definition sun_xlrg.h:4
#define DEBUG
Definition weather.h:6

◆ display_weather_text_center()

void PaperdinkUIWeatherClass::display_weather_text_center ( GxEPD2_GFX & display,
uint16_t x,
uint16_t y,
uint16_t w )

Definition at line 121 of file weather.cpp.

122{
123 int16_t xt, yt;
124 uint16_t wt, ht;
125
126 display.setFont(font);
127 display.setTextColor(primary_color);
128 display.getTextBounds(Paperdink_Weather.weather_string, 0, 0, &xt, &yt, &wt, &ht);
129 display.setCursor(x + ((w - wt) / 2), y + ht);
130 display.print(weather_string);
131}

◆ fetch_data()

int8_t PaperdinkUIWeatherClass::fetch_data ( const char * city,
const char * country,
const char * owm_api_id,
const char * units = "M" )

Definition at line 46 of file weather.cpp.

47{
48 int httpCode, ret = 0;
49
50 WiFiClientSecure *client = new WiFiClientSecure;
51 client->setCACert(cert);
52
53 snprintf(Paperdink_Weather.city, 10, "%s", city);
54 snprintf(Paperdink_Weather.country, 10, "%s", country);
55
56 owm_url.replace("{0}", city);
57 owm_url.replace("{1}", country);
58 owm_url.replace("{2}", owm_api_id);
59
60 if (units[0] == 'M')
61 owm_url += "&units=metric";
62 else
63 owm_url += "&units=imperial";
64
65 DEBUG.printf("OWM URL: %s\r\n", owm_url.c_str());
66
67 {
68 HTTPClient https;
69 https.begin(*client, owm_url.c_str());
70 https.addHeader("Content-Type", "application/json", 0, 0);
71
72 httpCode = https.GET();
73
74 if (httpCode == HTTP_CODE_OK) { // Check for the return code
75 // Parser updates weather_string directly
76 DEBUG.printf("[HTTP] GET SUCCESS\r\n");
77 DynamicJsonDocument json(35 * 1024);
78
79 // Deserialize the JSON document
80 // ReadLoggingStream can be used to debug the stream
81 //ReadLoggingStream loggingStream(https.getStream(), Serial);
82 //DeserializationError error = deserializeJson(json, loggingStream);
83 DeserializationError error = deserializeJson(json, https.getStream());
84 // Test if parsing succeeds.
85 if (error) {
86 Serial.print(F("deserializeJson() failed: "));
87 Serial.println(error.c_str());
88 ret = -1;
89 } else {
90 // convert it to a JsonObject
91 JsonObject root = json.as<JsonObject>();
92
93 snprintf(Paperdink_Weather.weather_string, 10, "%s", root["list"][0]["weather"][0]["main"].as<const char *>());
94 Paperdink_Weather.day_max_temp = root["list"][0]["main"]["temp_max"].as<float>();
95 Paperdink_Weather.day_min_temp = root["list"][0]["main"]["temp_min"].as<float>();
96 Paperdink_Weather.rain = root["list"][0]["rain"]["1h"].as<float>();
97 Paperdink_Weather.snow = root["list"][0]["snow"]["1h"].as<float>();
98 if (Paperdink_Weather.rain == 0)
99 Paperdink_Weather.rain = root["list"][0]["rain"]["3h"].as<float>();
100 if (Paperdink_Weather.snow == 0)
101 Paperdink_Weather.snow = root["list"][0]["snow"]["3h"].as<float>();
102 }
103
104 //ArudinoStreamParser parser;
105 //parser.setListener(&weather_listener);
106 //https.writeToStream(&parser);
107 } else {
108 DEBUG.printf("[HTTP] Failed, error: %s\r\n", https.errorToString(httpCode).c_str());
109 ret = -1;
110 }
111
112 https.end();
113 }
114
115 DEBUG.printf("[HTTP] COMPLETED \r\n");
116
117 delete client;
118 return ret;
119}

Member Data Documentation

◆ city

char PaperdinkUIWeatherClass::city[20]

Definition at line 11 of file weather.h.

◆ country

char PaperdinkUIWeatherClass::country[20]

Definition at line 12 of file weather.h.

◆ day_max_temp

float PaperdinkUIWeatherClass::day_max_temp

Definition at line 14 of file weather.h.

◆ day_min_temp

float PaperdinkUIWeatherClass::day_min_temp

Definition at line 15 of file weather.h.

◆ rain

float PaperdinkUIWeatherClass::rain

Definition at line 16 of file weather.h.

◆ snow

float PaperdinkUIWeatherClass::snow

Definition at line 17 of file weather.h.

◆ weather_string

char PaperdinkUIWeatherClass::weather_string[10]

Definition at line 13 of file weather.h.


The documentation for this class was generated from the following files: