This commit is contained in:
e2002
2022-04-11 13:18:33 +03:00
parent 520b3455d1
commit b840b4418a
41 changed files with 661 additions and 1131 deletions

View File

@@ -5,23 +5,19 @@
**************************************************************/
#if DSP_MODEL==DSP_ST7735
// 3600s = 60 minutes to not flooding
#define WEATHER_REQUEST_INTERVAL 3600 // 60min
#if (DSP_MODEL==DSP_ST7735) || (DSP_MODEL==DSP_ST7789) || (DSP_MODEL==DSP_SSD1327) || (DSP_MODEL==DSP_ILI9341)
#include <JSON_Decoder.h> // https://github.com/Bodmer/OpenWeather
#include <OpenWeather.h> // https://github.com/Bodmer/JSON_Decoder
#define WEATHER_REQUEST_INTERVAL 1800 //30min
#define WEATHER_REQUEST_INTERVAL_FAULTY 30
#include <WiFiClient.h>
#include <Ticker.h>
String api_key = "********************************"; // openweathermap.org API key
const char* host = "api.openweathermap.org";
const char* lat = "55.7512";
const char* lon = "37.6184";
const char* key = "********************************";
String latitude = "55.7512";
String longitude = "37.6184";
String units = "metric";
String language = "ru";
OW_Weather ow;
Ticker ticker;
/***********************************************
@@ -33,14 +29,100 @@ char weather[140] = { 0 };
bool weatherRequest = false;
TaskHandle_t weatherUpdateTaskHandle;
bool getForecast() {
WiFiClient client;
if (!client.connect(host, 80)) {
Serial.println("## OPENWEATHERMAP ###: connection failed");
return false;
}
char httpget[250] = {0};
sprintf(httpget, "GET /data/2.5/weather?lat=%s&lon=%s&units=metric&lang=ru&appid=%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", lat, lon, key, host);
client.print(httpget);
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 2000UL) {
Serial.println("## OPENWEATHERMAP ###: client available timeout !");
client.stop();
return false;
}
}
timeout = millis();
String line = "";
if (client.connected()) {
while (client.available())
{
line = client.readStringUntil('\n');
if (strstr(line.c_str(), "\"temp\"") != NULL) {
client.stop();
break;
}
if ((millis() - timeout) > 500)
{
client.stop();
Serial.println("## OPENWEATHERMAP ###: client read timeout !");
return false;
}
}
}
if (strstr(line.c_str(), "\"temp\"") == NULL) {
Serial.println("## OPENWEATHERMAP ###: weather not found !");
return false;
}
char *tmpe;
char *tmps;
const char* cursor = line.c_str();
char desc[120], temp[20], hum[20], press[20];
tmps = strstr(cursor, "\"description\":\"");
if (tmps == NULL) { Serial.println("## OPENWEATHERMAP ###: description not found !"); return false;}
tmps += 15;
tmpe = strstr(tmps, "\",\"");
if (tmpe == NULL) { Serial.println("## OPENWEATHERMAP ###: description not found !"); return false;}
strlcpy(desc, tmps, tmpe - tmps + 1);
cursor = tmpe + 3;
tmps = strstr(cursor, "\"temp\":");
if (tmps == NULL) { Serial.println("## OPENWEATHERMAP ###: temp not found !"); return false;}
tmps += 7;
tmpe = strstr(tmps, ",\"");
if (tmpe == NULL) { Serial.println("## OPENWEATHERMAP ###: temp not found !"); return false;}
strlcpy(temp, tmps, tmpe - tmps + 1);
cursor = tmpe + 2;
float tempf = atof(temp);
tmps = strstr(cursor, "\"pressure\":");
if (tmps == NULL) { Serial.println("## OPENWEATHERMAP ###: pressure not found !"); return false;}
tmps += 11;
tmpe = strstr(tmps, ",\"");
if (tmpe == NULL) { Serial.println("## OPENWEATHERMAP ###: pressure not found !"); return false;}
strlcpy(press, tmps, tmpe - tmps + 1);
cursor = tmpe + 2;
int pressi = (float)atoi(press) / 1.333;
tmps = strstr(cursor, "humidity\":");
if (tmps == NULL) { Serial.println("## OPENWEATHERMAP ###: humidity not found !"); return false;}
tmps += 10;
tmpe = strstr(tmps, ",\"");
if (tmpe == NULL) { Serial.println("## OPENWEATHERMAP ###: humidity not found !"); return false;}
strlcpy(hum, tmps, tmpe - tmps + 1);
Serial.printf("## OPENWEATHERMAP ###: description: %s, temp:%.1f C, pressure:%dmmHg, humidity:%s%%\n", desc, tempf, pressi, hum);
sprintf(weather, "%s, %.1f C * давление: %d мм * влажность: %s%%", desc, tempf, pressi, hum);
return true;
}
void getWeather( void * pvParameters ) {
OW_current *current = new OW_current;
OW_hourly *hourly = new OW_hourly;
OW_daily *daily = new OW_daily;
delay(5000);
ow.getForecast(current, hourly, daily, api_key, latitude, longitude, units, language);
sprintf(weather, "TEMP: %.1f C * PRESS: %d HG * HUM: %d%%", current->temp, (int)(current->pressure / 1.333), current->humidity);
weatherRequest = true;
delay(200);
if (getForecast()) {
weatherRequest = true;
ticker.detach();
ticker.attach(WEATHER_REQUEST_INTERVAL, updateWeather);
} else {
ticker.detach();
ticker.attach(WEATHER_REQUEST_INTERVAL_FAULTY, updateWeather);
}
vTaskDelete( NULL );
}
@@ -48,18 +130,17 @@ void updateWeather() {
xTaskCreatePinnedToCore(
getWeather, /* Task function. */
"getWeather1", /* name of task. */
8192, /* Stack size of task */
1024 * 4, /* Stack size of task */
NULL, /* parameter of the task */
0, /* priority of the task */
&weatherUpdateTaskHandle, /* Task handle to keep track of created task */
0); /* pin task to core CORE_FOR_LOOP_CONTROLS */
1); /* pin task to core CORE_FOR_LOOP_CONTROLS */
}
/***********************************************
Occurs when the network is connected
***********************************************/
void network_on_connect() {
ticker.attach(WEATHER_REQUEST_INTERVAL, updateWeather);
updateWeather();
}
@@ -75,17 +156,20 @@ void dsp_on_start(DspCore *dsp) {
Occurs when the display is initialized
***********************************************/
void dsp_on_init() {
hello.init(5, " * ", 1, TFT_LINEHGHT*4+6, 0, ORANGE, TFT_BG);
Serial.println(TFT_LINEHGHT*4+6);
if (DSP_MODEL == DSP_ST7735 || (DSP_MODEL == DSP_SSD1327)) {
hello.init(5, " * ", 1, TFT_LINEHGHT * 4 + 6, 0, ORANGE, TFT_BG);
} else {
hello.init(5, " * ", 2, TFT_LINEHGHT * 9 + 5, 0, ORANGE, TFT_BG);
}
}
/************************
The loop cycle
************************/
void dsp_on_loop() {
void dsp_on_loop(DspCore *dsp) {
if (weatherRequest) {
weatherRequest = false;
hello.setText(weather);
hello.setText(dsp->utf8Rus(weather, true));
}
if (display.mode == PLAYER) hello.loop();
}