Added scrolling text

This commit is contained in:
Tomislav Kopić 2024-11-07 19:56:56 +01:00
parent 9594bd84ee
commit 9f36c1c42d

View File

@ -168,148 +168,135 @@ void powerSaveCheck() {
} }
int icon = 0; int icon = 0;
String weatherState;
float temperature;
int humidity;
float pressure;
float wind_speed;
int forecastIcons[3];
int currentYear, currentMonth, currentDay;
bool fetchWeatherData() { bool fetchWeatherData() {
// Get current time // Get current time
time_t epochTime = timeClient.getEpochTime(); time_t epochTime = timeClient.getEpochTime();
struct tm *ptm = gmtime((time_t *)&epochTime); struct tm *ptm = gmtime((time_t *)&epochTime);
int currentDay = ptm->tm_mday; currentDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon + 1; currentMonth = ptm->tm_mon + 1;
int currentYear = ptm->tm_year + 1900; currentYear = ptm->tm_year + 1900;
HTTPClient http; HTTPClient http;
int attempt;
bool dataFetched = false;
// Request current weather
for (attempt = 0; attempt < 3; attempt++) {
// Request current weather // Request current weather
for (int attempt = 0; attempt < 3; attempt++) {
http.begin(client, "http://api.openweathermap.org/data/2.5/weather?q=" + Location + "&APPID=" + API_Key); http.begin(client, "http://api.openweathermap.org/data/2.5/weather?q=" + Location + "&APPID=" + API_Key);
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode > 0) { if (httpCode > 0) {
String payload = http.getString(); String payload = http.getString();
DynamicJsonDocument doc(1024); DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload); if (!deserializeJson(doc, payload)) {
if (!error) { weatherState = doc["weather"][0]["description"].as<String>();
const char* state = doc["weather"][0]["description"]; capitalizeFirstLetter(const_cast<char*>(weatherState.c_str()));
capitalizeFirstLetter(const_cast<char*>(state)); temperature = doc["main"]["temp"].as<float>() - 273.15;
humidity = doc["main"]["humidity"];
float temp = doc["main"]["temp"].as<float>() - 273.15; pressure = doc["main"]["pressure"].as<float>() / 1000;
int humidity = doc["main"]["humidity"]; wind_speed = doc["wind"]["speed"].as<float>();
float pressure = doc["main"]["pressure"].as<float>() / 1000;
float wind_speed = doc["wind"]["speed"].as<float>();
int statusSize = strlen(state) > 12 ? 1 : 2;
// Display data
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.printf("%d-%d-%d\r\n", currentYear, currentMonth, currentDay);
display.setCursor(0, 9);
display.printf("%s", Location.c_str());
display.setTextSize(statusSize);
display.setCursor(0, 21);
display.printf("%s\r\n", state);
display.setCursor(0, 39);
display.setTextSize(1);
display.printf(" %5.2f C %d%%\r\n", temp, humidity);
display.drawRect(43, 39, 3, 3, WHITE); // Degree symbol
display.setCursor(0, 52);
display.printf(" %.3fbar %.1fm/s \r\n", pressure, wind_speed);
display.drawLine(0, 18, 127, 18, 1);
display.drawLine(65, 18, 65, 0, 1);
display.drawBitmap(0, 38, temperature_icon, 10, 10, WHITE);
display.drawBitmap(74, 38, humidity_icon, 10, 10, WHITE);
display.drawBitmap(0, 51, pressure_icon, 10, 10, WHITE);
display.drawBitmap(74, 51, wind_icon, 10, 10, WHITE);
display.display();
http.end(); http.end();
dataFetched = true; break;
break; // Exit the loop after successful fetch
} }
} }
// Display attempt number delay(1000); // Retry after 1 second
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.printf("Attempt %d/3", attempt + 1);
display.display();
delay(1000); // Retry after 1 second if the request fails
}
if (!dataFetched) {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.printf("Failed to fetch weather");
display.display();
http.end();
return false; // Return false if the fetch fails after 3 attempts
} }
// Request forecast // Request forecast
for (attempt = 0; attempt < 3; attempt++) { for (int attempt = 0; attempt < 3; attempt++) {
// Request forecast
http.begin(client, "http://api.openweathermap.org/data/2.5/forecast?q=" + Location + "&APPID=" + API_Key + "&cnt=3"); http.begin(client, "http://api.openweathermap.org/data/2.5/forecast?q=" + Location + "&APPID=" + API_Key + "&cnt=3");
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode > 0) { if (httpCode > 0) {
String payload = http.getString(); String payload = http.getString();
DynamicJsonDocument forecastDoc(1024); DynamicJsonDocument forecastDoc(1024);
DeserializationError error = deserializeJson(forecastDoc, payload); if (!deserializeJson(forecastDoc, payload)) {
if (!error) {
int pos = 69;
for (int day = 0; day <= 2; day++) { for (int day = 0; day <= 2; day++) {
int state = forecastDoc["list"][day]["weather"][0]["id"]; int state = forecastDoc["list"][day]["weather"][0]["id"];
// Determine icon based on state ID if (state >= 200 && state <= 232) forecastIcons[day] = 6; // Thunderstorm
if (state >= 200 && state <= 232) { else if (state >= 300 && state <= 321) forecastIcons[day] = 5; // Drizzle
icon = 6; // Thunderstorm else if (state >= 500 && state <= 531) forecastIcons[day] = 3; // Rain
} else if (state >= 300 && state <= 321) { else if (state >= 600 && state <= 622) forecastIcons[day] = 4; // Snow
icon = 5; // Drizzle else if (state >= 701 && state <= 781) forecastIcons[day] = 2; // Mist
} else if (state >= 500 && state <= 531) { else if (state == 800) forecastIcons[day] = 0; // Clear
icon = 3; // Rain else if (state >= 801 && state <= 804) forecastIcons[day] = 1; // Clouds
} else if (state >= 600 && state <= 622) { else forecastIcons[day] = 0;
icon = 4; // Snow
} else if (state >= 701 && state <= 781) {
icon = 2; // Mist
} else if (state == 800) {
icon = 0; // Clear
} else if (state >= 801 && state <= 804) {
icon = 1; // Clouds
} else {
icon = 0; // Default icon
}
display.drawBitmap(pos, 0, bitmap_icons[icon], 16, 16, WHITE);
pos += 20;
display.display();
} }
http.end(); http.end();
break; // Exit the loop after successful fetch return true;
} }
} }
// Display attempt number for forecast delay(1000); // Retry after 1 second
}
return false; // Return false if data fetch fails
}
int scrollPos = SCREEN_WIDTH; // Global variable to keep track of the scroll position
unsigned long lastScrollTime = 0; // To control the scroll speed
const int scrollDelay = 75; // Delay in milliseconds between scroll updates
void displayWeatherData() {
display.clearDisplay(); display.clearDisplay();
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0, 0); display.setCursor(0, 0);
display.printf("Forecast attempt %d/3", attempt + 1); display.printf("%d-%d-%d\r\n", currentYear, currentMonth, currentDay);
display.display(); display.setCursor(0, 9);
delay(1000); // Retry after 1 second if the request fails display.printf("%s", Location.c_str());
}
if (!dataFetched) { int stateWidth = strlen(weatherState.c_str()) * 12; // Approximate width of the status text in pixels
display.clearDisplay();
// Clear the area for the file name to avoid overlap
display.fillRect(0, 21, SCREEN_WIDTH, 11, BLACK);
// Scroll the text to the left
if (!is_display_off) {
if (millis() - lastScrollTime > scrollDelay) {
// Update scroll position
scrollPos -= 1; // Move left by 1 pixel each time
lastScrollTime = millis();
// If the text has completely scrolled off, reset scroll position to start from the right
if (scrollPos < -stateWidth) {
scrollPos = SCREEN_WIDTH;
}
}
}
// Draw the file name with current scroll position
display.setTextSize(2);
display.setCursor(scrollPos, 21);
display.print(weatherState);
// Clear the area for "Time left" display before printing
display.fillRect(0, 37, SCREEN_WIDTH, 27, BLACK); // Clear area for "Time left"
display.setCursor(0, 40);
display.setTextSize(1); display.setTextSize(1);
display.setCursor(0, 0); display.printf(" %5.2f C %d%%\r\n", temperature, humidity);
display.printf("Failed to fetch forecast"); display.drawRect(43, 40, 3, 3, WHITE); // Degree symbol
display.display();
return false; // Return false if the forecast fetch fails after 3 attempts display.setCursor(0, 52);
display.printf(" %.3fbar %.1fm/s \r\n", pressure, wind_speed);
display.drawLine(0, 18, 127, 18, 1);
display.drawLine(65, 18, 65, 0, 1);
display.drawBitmap(0, 38, temperature_icon, 10, 10, WHITE);
display.drawBitmap(74, 38, humidity_icon, 10, 10, WHITE);
display.drawBitmap(0, 51, pressure_icon, 10, 10, WHITE);
display.drawBitmap(74, 51, wind_icon, 10, 10, WHITE);
int pos = 69;
for (int i = 0; i < 3; i++) {
display.drawBitmap(pos, 0, bitmap_icons[forecastIcons[i]], 16, 16, WHITE);
pos += 20;
} }
return true; // Return true if both data fetches were successful display.display();
} }
void setup(void) { void setup(void) {
@ -330,6 +317,7 @@ void loop() {
fetchWeatherData(); fetchWeatherData();
lastWeatherUpdate = millis(); lastWeatherUpdate = millis();
} }
displayWeatherData();
powerSaveCheck(); powerSaveCheck();
commonButtonHandler(); commonButtonHandler();
} }