-
Notifications
You must be signed in to change notification settings - Fork 0
Feature menu wifi ssid #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab7a0d5
Wifi ssid
Mickyleitor 3fecfcb
Safer eeprom scheduled writes
Mickyleitor 9b85ddc
Some fixes given by copilot
Mickyleitor 2daf469
better defines
Mickyleitor 1a8b1f4
better encapsulation of pending configuration in lcd
Mickyleitor 3af121a
Refactor WiFi settings and update handling; add able to disable/enabl…
Mickyleitor 6931750
Merge branch 'master' of https://github.com/Mickyleitor/RollerShutter…
Mickyleitor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,21 +6,29 @@ | |||||
| #include "basic_defines.h" | ||||||
| #include "persistentVars.h" | ||||||
|
|
||||||
| #define HTTP_SERVER_PING_ADDRESS "1.1.1.1" | ||||||
| #define HTTP_SERVER_PING_INTERVAL_MS (10000) | ||||||
|
|
||||||
| #define WIFI_CONNECTION_TIMEOUT_MS (10000) | ||||||
| #define WIFI_CONNECTION_INTERVAL_MS (60 * 60 * 1000) // 60 min | ||||||
| #define WIFI_CONNECTION_MAX_CONSECUTIVE_ATTEMPTS (5) | ||||||
|
|
||||||
| #define WEATHER_UPDATE_INTERVAL_MS (30 * 60 * 1000) // 30 min | ||||||
| #define WEATHER_UPDATE_MAX_CONSECUTIVE_ATTEMPTS (10) | ||||||
| #define WEATHER_SERVER_REQUEST_TIMEOUT (2000) | ||||||
|
|
||||||
| #define WIFI_SCAN_NOT_SEEN_MAX_COUNT (5) | ||||||
| #define WIFI_SCAN_MINIMUM_RSSI_FOR_TRACKING (-80) | ||||||
| #define WIFI_SCAN_MAX_TRACKED_NETWORKS_COUNT (10) | ||||||
|
|
||||||
| #define TEMPERATURE_DEGREE_INVALID (65535) | ||||||
| #define TEMPERATURE_DEGREE_INVALID (0xFFFF) | ||||||
|
|
||||||
| extern struct Settings settings; | ||||||
| enum WifiErrorType { | ||||||
| WIFI_ERROR_NONE = (0 << 0), | ||||||
| WIFI_ERROR_CONNECTION_FAILED_MASK = (1 << 0), | ||||||
| WIFI_ERROR_WEATHER_UPDATE_FAILED_MASK = (1 << 1), | ||||||
| }; | ||||||
|
|
||||||
| struct WeatherData { | ||||||
| double TemperatureDegree = TEMPERATURE_DEGREE_INVALID; | ||||||
| } MyWeather; | ||||||
| typedef uint8_t WifiErrorType_t; | ||||||
|
|
||||||
| extern struct Settings settings; | ||||||
|
|
||||||
| struct WifiNetworkInfo { | ||||||
| String ssid; | ||||||
|
|
@@ -47,132 +55,71 @@ struct WifiNetworkInfo { | |||||
| }; | ||||||
|
|
||||||
| static std::vector<WifiNetworkInfo> wifiNetworks; | ||||||
| static bool wifiScanInProgress = false; | ||||||
| static unsigned long lastConnectAttemptMs = 0; | ||||||
|
|
||||||
| bool ESP8266Utils_Connect_STA( | ||||||
| const char* ssid, | ||||||
| const char* password, | ||||||
| const char* hostname, | ||||||
| int32_t timeout_ms = 10000) { | ||||||
| Serial.println(""); | ||||||
| WiFi.mode(WIFI_STA); | ||||||
| WiFi.begin(ssid, password); | ||||||
|
|
||||||
| while (WiFi.status() != WL_CONNECTED && timeout_ms > 0) { | ||||||
| delay(100); | ||||||
| timeout_ms -= 100; | ||||||
| yield(); | ||||||
| static bool wifiScanInProgress = false; | ||||||
| static unsigned long lastConnectUpdateMs = WIFI_CONNECTION_TIMEOUT_MS; | ||||||
| static unsigned long lastWeatherUpdateMs = WEATHER_UPDATE_INTERVAL_MS; | ||||||
| static double TemperatureDegree = TEMPERATURE_DEGREE_INVALID; | ||||||
| static bool wifiError = false; | ||||||
| static bool weatherError = false; | ||||||
|
|
||||||
| bool ESP8266Utils_get_TemperatureDegree(double* temperature) { | ||||||
| if ((temperature == nullptr) || (TemperatureDegree == TEMPERATURE_DEGREE_INVALID)) { | ||||||
| return false; // Invalid temperature or null pointer | ||||||
| } | ||||||
|
|
||||||
| if (timeout_ms <= 0) { | ||||||
| Serial.println("STA Failed"); | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| Serial.println(""); | ||||||
| Serial.print("Iniciado STA:\t"); | ||||||
| Serial.println(ssid); | ||||||
| Serial.print("IP address:\t"); | ||||||
| Serial.println(WiFi.localIP()); | ||||||
|
|
||||||
| *temperature = TemperatureDegree; | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| bool ESP8266Utils_is_STA_connected() { return WiFi.isConnected(); } | ||||||
| bool ESP8266Utils_update_WeatherData(void) { | ||||||
| bool success = false; | ||||||
|
|
||||||
| bool ESP8266Utils_is_STA_online() { | ||||||
| if (!ESP8266Utils_is_STA_connected()) { | ||||||
| return false; | ||||||
| } | ||||||
| static bool isOnline; | ||||||
| static uint32_t timeout_ms = 0; | ||||||
| if ((millis() - timeout_ms) > HTTP_SERVER_PING_INTERVAL_MS) { | ||||||
| timeout_ms = millis(); | ||||||
| isOnline = true; | ||||||
| WiFiClient client; | ||||||
| // Ping the server to see if it is online | ||||||
| client.setTimeout(HTTP_SERVER_REQUEST_TIMEOUT); | ||||||
| if (!client.connect(HTTP_SERVER_PING_ADDRESS, 80)) { | ||||||
| isOnline = false; | ||||||
| } | ||||||
| client.stop(); | ||||||
| } | ||||||
| return isOnline; | ||||||
| } | ||||||
|
|
||||||
| int8_t ESP8266Utils_get_STA_RSSI() { return WiFi.RSSI(); } | ||||||
|
|
||||||
| String ESP8266Utils_get_STA_SSID() { return WiFi.SSID(); } | ||||||
| Serial.println("Updating weather data..."); | ||||||
|
|
||||||
| bool ESP8266Utils_update_WeatherData(struct Settings* myData) { | ||||||
| if (!ESP8266Utils_is_STA_online()) { | ||||||
| Serial.println("No internet connection"); | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| String default_appid = DEFAULT_OPENWEATHERMAP_APPID; | ||||||
| if ((String(myData->openWeatherMapSettings.appid) == default_appid) | ||||||
| || (String(myData->openWeatherMapSettings.appid) == String(""))) { | ||||||
| Serial.println("Invalid Weather API key : " + String(myData->openWeatherMapSettings.appid)); | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| WiFiClient client; | ||||||
| // Connect to HTTP server | ||||||
| client.setTimeout(HTTP_SERVER_REQUEST_TIMEOUT); | ||||||
| if (!client.connect(OPENWEATHERMAP_HOST, OPENWEATHERMAP_PORT)) { | ||||||
| Serial.println("Connection failed"); | ||||||
| // Disconnect | ||||||
| client.stop(); | ||||||
| return false; | ||||||
| } | ||||||
| if (String(settings.openWeatherMapSettings.appid) == String("")) { | ||||||
| Serial.println("Empty Weather API key"); | ||||||
| } else { | ||||||
| WiFiClient client; | ||||||
| client.setTimeout(WEATHER_SERVER_REQUEST_TIMEOUT); | ||||||
|
|
||||||
| // Send HTTP request | ||||||
| String HTTPrequest = DEFAULT_OPENWEATHERMAP_HTTP_REQUEST( | ||||||
| myData->openWeatherMapSettings.appid, | ||||||
| myData->openWeatherMapSettings.lat, | ||||||
| myData->openWeatherMapSettings.lon); | ||||||
| client.println(HTTPrequest); | ||||||
| client.println("Host: " + String(OPENWEATHERMAP_HOST)); | ||||||
| client.println("Connection: close"); | ||||||
| if (client.println() == 0) { | ||||||
| Serial.println("Failed to send request"); | ||||||
| // Disconnect | ||||||
| client.stop(); | ||||||
| return false; | ||||||
| } | ||||||
| // Skip HTTP headers | ||||||
| if (!client.find("\r\n\r\n")) { | ||||||
| Serial.println("Invalid response"); | ||||||
| // Disconnect | ||||||
| client.stop(); | ||||||
| return false; | ||||||
| } | ||||||
| if (client.find("\"temp\":")) { | ||||||
| double NewTemp = client.readStringUntil(',').toDouble(); | ||||||
| if (NewTemp > 273) { | ||||||
| NewTemp -= 273.15; | ||||||
| if (!client.connect(OPENWEATHERMAP_HOST, OPENWEATHERMAP_PORT)) { | ||||||
| Serial.println("Connection failed"); | ||||||
| } else { | ||||||
| String HTTPrequest = OPENWEATHERMAP_HTTP_REQUEST( | ||||||
| settings.openWeatherMapSettings.appid, | ||||||
| settings.openWeatherMapSettings.lat, | ||||||
| settings.openWeatherMapSettings.lon); | ||||||
| client.println(HTTPrequest); | ||||||
| client.println("Host: " + String(OPENWEATHERMAP_HOST)); | ||||||
| client.println("Connection: close"); | ||||||
|
|
||||||
| if (client.println() == 0) { | ||||||
| Serial.println("Failed to send request"); | ||||||
| } else if (!client.find("\r\n\r\n")) { | ||||||
| Serial.println("Invalid response"); | ||||||
| } else if (client.find("\"temp\":")) { | ||||||
| double NewTemp = client.readStringUntil(',').toDouble(); | ||||||
| if (NewTemp > 273) { | ||||||
| NewTemp -= 273.15; | ||||||
| } | ||||||
| Serial.print("Temperature: "); | ||||||
| Serial.println(NewTemp); | ||||||
| TemperatureDegree = NewTemp; | ||||||
|
|
||||||
| if (client.find("\"timezone\":")) { | ||||||
| long timezoneshift = strtol(client.readStringUntil(',').c_str(), NULL, 10); | ||||||
| Serial.print("Timezone shift: "); | ||||||
| Serial.println(timezoneshift); | ||||||
| struct rtcTime_t rtcTime | ||||||
| = { .myTime = time(NULL), .timezoneShift = timezoneshift }; | ||||||
| persistentVars_store_rtcTime(&rtcTime); | ||||||
| success = true; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Serial.print("Temperature: "); | ||||||
| Serial.println(NewTemp); | ||||||
| MyWeather.TemperatureDegree = NewTemp; | ||||||
| } else { | ||||||
| Serial.println("No Temperature JSON object found"); | ||||||
| } | ||||||
| if (client.find("\"timezone\":")) { | ||||||
| long timezoneshift = strtol(client.readStringUntil(',').c_str(), NULL, 10); | ||||||
| Serial.print("Timezone shift: "); | ||||||
| Serial.println(timezoneshift); | ||||||
| struct rtcTime_t rtcTime = { .myTime = time(NULL), .timezoneShift = timezoneshift }; | ||||||
| persistentVars_store_rtcTime(&rtcTime); | ||||||
| } else { | ||||||
| Serial.println("No timezoneshift JSON object found"); | ||||||
| client.stop(); | ||||||
| } | ||||||
|
|
||||||
| // Disconnect | ||||||
| client.stop(); | ||||||
| return true; | ||||||
| lastWeatherUpdateMs = millis(); | ||||||
| return success; | ||||||
| } | ||||||
|
|
||||||
| void ESP8266Utils_clearWifiNetworksList() { | ||||||
|
|
@@ -316,17 +263,99 @@ int ESP8266Utils_getIndexBySsid(const String& targetSsid) { | |||||
| void ESP8266Utils_connectToWifi(const String& ssid, const String& password) { | ||||||
| WiFi.mode(WIFI_STA); | ||||||
| WiFi.begin(ssid.c_str(), password.c_str()); | ||||||
| lastConnectAttemptMs = millis(); | ||||||
| lastConnectUpdateMs = millis(); | ||||||
| } | ||||||
|
|
||||||
| bool ESP8266Utils_isWifiConnected() { return WiFi.status() == WL_CONNECTED; } | ||||||
| bool ESP8266Utils_isWifiConnected(void) { return WiFi.status() == WL_CONNECTED; } | ||||||
|
|
||||||
| int ESP8266Utils_getWifiConnectionPercentage() { | ||||||
| if (lastConnectAttemptMs == 0) { | ||||||
| int ESP8266Utils_getWifiConnectionPercentage(void) { | ||||||
| if (lastConnectUpdateMs == 0) { | ||||||
| return 0; | ||||||
| } | ||||||
| unsigned long elapsed = millis() - lastConnectAttemptMs; | ||||||
| unsigned long elapsed = millis() - lastConnectUpdateMs; | ||||||
| return (elapsed * 100) / WIFI_CONNECTION_TIMEOUT_MS; | ||||||
| } | ||||||
|
|
||||||
| void Wifi_handler() {} | ||||||
| bool ESP8266Utils_getWifiConnectAttemptTmo(void) { | ||||||
| if (lastConnectUpdateMs == 0) { | ||||||
| return true; // No hay intento previo, se considera un timeout | ||||||
| } | ||||||
| return (millis() - lastConnectUpdateMs) >= WIFI_CONNECTION_INTERVAL_MS; | ||||||
| } | ||||||
|
|
||||||
| bool ESP8266Utils_getWifiWeatherAttemptTmo(void) { | ||||||
| if (lastWeatherUpdateMs == 0) { | ||||||
| return true; // No hay intento previo, se considera un timeout | ||||||
|
||||||
| return true; // No hay intento previo, se considera un timeout | |
| return true; // No previous attempt, considered a timeout |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed language comments: 'No hay intento previo, se considera un timeout' is in Spanish while most other comments are in English. Consider maintaining consistency in comment language.