Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for celsius or fahrenheit temperatures and different date formats #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions weather/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#define OPENWEATHER_SRV "api.openweathermap.org"
#define OPENWEATHER_PORT 80
#define OPENWEATHER_API "your openweathermap api key"
#define OPENWEATHER_UNITS "metric" // "imperial"

#define WEATHER_UNITS "C" // "F"

#define WIFI_SSID "your wifi ssid"
#define WIFI_PW "your wifi password"
21 changes: 17 additions & 4 deletions weather/Display.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void WeatherDisplay::DrawM5PaperInfo(int x, int y, int dx, int dy)

canvas.setTextSize(3);
DrawIcon(x + 35, y + 140, (uint16_t *) TEMPERATURE64x64);
canvas.drawString(String(myData.sht30Temperatur) + " C", x + 35, y + 210, 1);
canvas.drawString(String(myData.sht30Temperatur) + " " + (String)WEATHER_UNITS, x + 35, y + 210, 1);
DrawIcon(x + 145, y + 140, (uint16_t *) HUMIDITY64x64);
canvas.drawString(String(myData.sht30Humidity) + "%", x + 150, y + 210, 1);

Expand All @@ -314,7 +314,7 @@ void WeatherDisplay::DrawHourly(int x, int y, int dx, int dy, Weather &weather,

canvas.setTextSize(2);
canvas.drawCentreString(getHourString(time) + ":00", x + dx / 2, y + 10, 1);
canvas.drawCentreString(String(temp) + " C", x + dx / 2, y + 30, 1);
canvas.drawCentreString(String(temp) + " " + WEATHER_UNITS, x + dx / 2, y + 30, 1);
// canvas.drawCentreString(main, x + dx / 2, y + 70, 1);

int iconX = x + dx / 2 - 32;
Expand Down Expand Up @@ -431,9 +431,22 @@ void WeatherDisplay::Show()
DrawHourly(x, 286, 116, 122, myData.weather, i);
}

int min_tempC = -20;
int min_tempF = -10;
int max_tempC = 37;
int max_tempF = 100;

int min_temp = min_tempC;
int max_temp = max_tempC;

if (WEATHER_UNITS == "F") {
min_temp = min_tempF;
max_temp = max_tempF;
}

canvas.drawRect(15, 408, maxX - 30, 122, M5EPD_Canvas::G15);
DrawGraph( 15, 408, 232, 122, "Temperature (C)", 0, 7, -20, 30, myData.weather.forecastMaxTemp);
DrawGraph( 15, 408, 232, 122, "Temperature (C)", 0, 7, -20, 30, myData.weather.forecastMinTemp);
DrawGraph( 15, 408, 232, 122, "Temperature (" + (String)WEATHER_UNITS + ")", 0, 7, min_temp, max_temp, myData.weather.forecastMaxTemp);
DrawGraph( 15, 408, 232, 122, "Temperature (" + (String)WEATHER_UNITS + ")", 0, 7, min_temp, max_temp, myData.weather.forecastMinTemp);
DrawGraph(247, 408, 232, 122, "Rain (mm)", 0, 7, 0, myData.weather.maxRain, myData.weather.forecastRain);
DrawGraph(479, 408, 232, 122, "Humidity (%)", 0, 7, 0, 100, myData.weather.forecastHumidity);
DrawGraph(711, 408, 232, 122, "Pressure (hPa)", 0, 7, 800, 1400, myData.weather.forecastPressure);
Expand Down
10 changes: 9 additions & 1 deletion weather/SHT30.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ bool GetSHT30Values(MyData &myData)
{
M5.SHT30.UpdateData();
if(M5.SHT30.GetError() == 0) {
myData.sht30Temperatur = (int) M5.SHT30.GetTemperature();
String weatherUnits = (String)WEATHER_UNITS;

if(weatherUnits == "C") {
myData.sht30Temperatur = (int) M5.SHT30.GetTemperature();
} else if(weatherUnits == "F") {
float tempC = (float) M5.SHT30.GetTemperature();
int tempF = (int)((tempC * 1.8) + 32);
myData.sht30Temperatur = tempF;
}
myData.sht30Humidity = (int) M5.SHT30.GetRelHumidity();
return true;
}
Expand Down
52 changes: 44 additions & 8 deletions weather/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ String getRTCDateTimeString()
M5.RTC.getDate(&date_struct);
M5.RTC.getTime(&time_struct);

sprintf(buff,"%02d.%02d.%04d %02d:%02d:%02d",
date_struct.day, date_struct.mon, date_struct.year,
time_struct.hour, time_struct.min, time_struct.sec);
if (DATE_FORMAT == "MMDDYYYY") {
sprintf(buff,"%02d.%02d.%04d %02d:%02d:%02d",
date_struct.mon, date_struct.day, date_struct.year,
time_struct.hour, time_struct.min, time_struct.sec);
} else if (DATE_FORMAT == "YYYYMMDD") {
sprintf(buff,"%04d.%02d.%02d %02d:%02d:%02d",
date_struct.year, date_struct.mon, date_struct.day,
time_struct.hour, time_struct.min, time_struct.sec);
} else {
sprintf(buff,"%02d.%02d.%04d %02d:%02d:%02d",
date_struct.day, date_struct.mon, date_struct.year,
time_struct.hour, time_struct.min, time_struct.sec);
}

return (String) buff;
}
Expand Down Expand Up @@ -68,8 +78,16 @@ String getRTCDateString()

M5.RTC.getDate(&date_struct);

sprintf(buff,"%02d.%02d.%04d",
date_struct.day, date_struct.mon, date_struct.year);
if(DATE_FORMAT == "MMDDYYYY") {
sprintf(buff,"%02d.%02d.%04d",
date_struct.mon, date_struct.day, date_struct.year);
} else if (DATE_FORMAT == "YYYYMMDD") {
sprintf(buff,"%04d.%02d.%02d",
date_struct.year, date_struct.mon, date_struct.day);
} else {
sprintf(buff,"%02d.%02d.%04d",
date_struct.day, date_struct.mon, date_struct.year);
}

return (String) buff;
}
Expand All @@ -92,10 +110,20 @@ String getRTCTimeString()
String getDateTimeString(time_t rawtime)
{
char buff[32];


if(DATE_FORMAT == "MMDDYYYY") {
sprintf(buff,"%02d.%02d.%04d %02d:%02d:%02d",
month(rawtime), day(rawtime), year(rawtime),
hour(rawtime), minute(rawtime), second(rawtime));
} else if (DATE_FORMAT == "YYYYMMDD") {
sprintf(buff,"%04d.%02d.%02d %02d:%02d:%02d",
year(rawtime), month(rawtime), day(rawtime),
hour(rawtime), minute(rawtime), second(rawtime));
} else {
sprintf(buff,"%02d.%02d.%04d %02d:%02d:%02d",
day(rawtime), month(rawtime), year(rawtime),
hour(rawtime), minute(rawtime), second(rawtime));
}

return (String) buff;
}
Expand All @@ -104,9 +132,17 @@ String getDateTimeString(time_t rawtime)
String getDateString(time_t rawtime)
{
char buff[32];

sprintf(buff,"%02d.%02d.%04d",

if(DATE_FORMAT == "MMDDYYYY") {
sprintf(buff,"%02d.%02d.%04d",
month(rawtime), day(rawtime), year(rawtime));
} else if(DATE_FORMAT == "YYYYMMDD") {
sprintf(buff,"%04d.%02d.%02d",
year(rawtime), month(rawtime), day(rawtime));
} else {
sprintf(buff,"%02d.%02d.%04d",
day(rawtime), month(rawtime), year(rawtime));
}

return (String) buff;
}
Expand Down
11 changes: 6 additions & 5 deletions weather/Weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class Weather
uri += "/data/2.5/onecall";
uri += "?lat=" + String((float) LATITUDE, 5);
uri += "&lon=" + String((float) LONGITUDE, 5);
uri += "&units=metric&lang=en&exclude=minutely";
uri += "&units=" + (String)OPENWEATHER_UNITS;
uri += "&lang=en&exclude=minutely";
uri += "&appid=" + (String) OPENWEATHER_API;

client.stop();
Expand Down Expand Up @@ -114,14 +115,14 @@ class Weather
JsonArray hourly_list = root["hourly"];
hourlyTime[0] = LocalTime(root["current"]["dt"].as<int>());
hourlyMaxTemp[0] = root["current"]["temp"].as<float>();
hourlyMain[0] = root["current"]["weather"][0]["main"].as<char *>();
hourlyIcon[0] = root["current"]["weather"][0]["icon"].as<char *>();
hourlyMain[0] = root["current"]["weather"][0]["main"].as<const char *>();
hourlyIcon[0] = root["current"]["weather"][0]["icon"].as<const char *>();
for (int i = 1; i < MAX_HOURLY; i++) {
if (i < hourly_list.size()) {
hourlyTime[i] = LocalTime(hourly_list[i - 1]["dt"].as<int>());
hourlyMaxTemp[i] = hourly_list[i - 1]["temp"].as<float>();
hourlyMain[i] = hourly_list[i - 1]["weather"][0]["main"].as<char *>();
hourlyIcon[i] = hourly_list[i - 1]["weather"][0]["icon"].as<char *>();
hourlyMain[i] = hourly_list[i - 1]["weather"][0]["main"].as<const char *>();
hourlyIcon[i] = hourly_list[i - 1]["weather"][0]["icon"].as<const char *>();
}
}

Expand Down