Added scrolling text
This commit is contained in:
parent
9594bd84ee
commit
9f36c1c42d
202
src/main.cpp
202
src/main.cpp
@ -168,148 +168,135 @@ void powerSaveCheck() {
|
||||
}
|
||||
|
||||
int icon = 0;
|
||||
String weatherState;
|
||||
float temperature;
|
||||
int humidity;
|
||||
float pressure;
|
||||
float wind_speed;
|
||||
int forecastIcons[3];
|
||||
int currentYear, currentMonth, currentDay;
|
||||
|
||||
bool fetchWeatherData() {
|
||||
// Get current time
|
||||
time_t epochTime = timeClient.getEpochTime();
|
||||
struct tm *ptm = gmtime((time_t *)&epochTime);
|
||||
int currentDay = ptm->tm_mday;
|
||||
int currentMonth = ptm->tm_mon + 1;
|
||||
int currentYear = ptm->tm_year + 1900;
|
||||
currentDay = ptm->tm_mday;
|
||||
currentMonth = ptm->tm_mon + 1;
|
||||
currentYear = ptm->tm_year + 1900;
|
||||
HTTPClient http;
|
||||
|
||||
int attempt;
|
||||
bool dataFetched = false;
|
||||
|
||||
// Request current weather
|
||||
for (attempt = 0; attempt < 3; attempt++) {
|
||||
// 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);
|
||||
int httpCode = http.GET();
|
||||
if (httpCode > 0) {
|
||||
String payload = http.getString();
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
if (!error) {
|
||||
const char* state = doc["weather"][0]["description"];
|
||||
capitalizeFirstLetter(const_cast<char*>(state));
|
||||
|
||||
float temp = doc["main"]["temp"].as<float>() - 273.15;
|
||||
int humidity = doc["main"]["humidity"];
|
||||
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();
|
||||
|
||||
if (!deserializeJson(doc, payload)) {
|
||||
weatherState = doc["weather"][0]["description"].as<String>();
|
||||
capitalizeFirstLetter(const_cast<char*>(weatherState.c_str()));
|
||||
temperature = doc["main"]["temp"].as<float>() - 273.15;
|
||||
humidity = doc["main"]["humidity"];
|
||||
pressure = doc["main"]["pressure"].as<float>() / 1000;
|
||||
wind_speed = doc["wind"]["speed"].as<float>();
|
||||
http.end();
|
||||
dataFetched = true;
|
||||
break; // Exit the loop after successful fetch
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Display attempt number
|
||||
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
|
||||
delay(1000); // Retry after 1 second
|
||||
}
|
||||
|
||||
// Request forecast
|
||||
for (attempt = 0; attempt < 3; attempt++) {
|
||||
// Request forecast
|
||||
for (int attempt = 0; attempt < 3; attempt++) {
|
||||
http.begin(client, "http://api.openweathermap.org/data/2.5/forecast?q=" + Location + "&APPID=" + API_Key + "&cnt=3");
|
||||
int httpCode = http.GET();
|
||||
if (httpCode > 0) {
|
||||
String payload = http.getString();
|
||||
DynamicJsonDocument forecastDoc(1024);
|
||||
|
||||
DeserializationError error = deserializeJson(forecastDoc, payload);
|
||||
if (!error) {
|
||||
int pos = 69;
|
||||
if (!deserializeJson(forecastDoc, payload)) {
|
||||
for (int day = 0; day <= 2; day++) {
|
||||
int state = forecastDoc["list"][day]["weather"][0]["id"];
|
||||
|
||||
// Determine icon based on state ID
|
||||
if (state >= 200 && state <= 232) {
|
||||
icon = 6; // Thunderstorm
|
||||
} else if (state >= 300 && state <= 321) {
|
||||
icon = 5; // Drizzle
|
||||
} else if (state >= 500 && state <= 531) {
|
||||
icon = 3; // Rain
|
||||
} else if (state >= 600 && state <= 622) {
|
||||
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();
|
||||
if (state >= 200 && state <= 232) forecastIcons[day] = 6; // Thunderstorm
|
||||
else if (state >= 300 && state <= 321) forecastIcons[day] = 5; // Drizzle
|
||||
else if (state >= 500 && state <= 531) forecastIcons[day] = 3; // Rain
|
||||
else if (state >= 600 && state <= 622) forecastIcons[day] = 4; // Snow
|
||||
else if (state >= 701 && state <= 781) forecastIcons[day] = 2; // Mist
|
||||
else if (state == 800) forecastIcons[day] = 0; // Clear
|
||||
else if (state >= 801 && state <= 804) forecastIcons[day] = 1; // Clouds
|
||||
else forecastIcons[day] = 0;
|
||||
}
|
||||
http.end();
|
||||
break; // Exit the loop after successful fetch
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Display attempt number for forecast
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setCursor(0, 0);
|
||||
display.printf("Forecast attempt %d/3", attempt + 1);
|
||||
display.display();
|
||||
delay(1000); // Retry after 1 second if the request fails
|
||||
delay(1000); // Retry after 1 second
|
||||
}
|
||||
|
||||
if (!dataFetched) {
|
||||
display.clearDisplay();
|
||||
display.setTextSize(1);
|
||||
display.setCursor(0, 0);
|
||||
display.printf("Failed to fetch forecast");
|
||||
display.display();
|
||||
return false; // Return false if the forecast fetch fails after 3 attempts
|
||||
}
|
||||
return false; // Return false if data fetch fails
|
||||
}
|
||||
|
||||
return true; // Return true if both data fetches were successful
|
||||
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.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());
|
||||
|
||||
int stateWidth = strlen(weatherState.c_str()) * 12; // Approximate width of the status text in pixels
|
||||
|
||||
// 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.printf(" %5.2f C %d%%\r\n", temperature, humidity);
|
||||
display.drawRect(43, 40, 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);
|
||||
|
||||
int pos = 69;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
display.drawBitmap(pos, 0, bitmap_icons[forecastIcons[i]], 16, 16, WHITE);
|
||||
pos += 20;
|
||||
}
|
||||
|
||||
display.display();
|
||||
}
|
||||
|
||||
void setup(void) {
|
||||
@ -330,6 +317,7 @@ void loop() {
|
||||
fetchWeatherData();
|
||||
lastWeatherUpdate = millis();
|
||||
}
|
||||
displayWeatherData();
|
||||
powerSaveCheck();
|
||||
commonButtonHandler();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user