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

Add ability to use several WiFi networks #155

Open
wants to merge 1 commit into
base: main
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
2 changes: 1 addition & 1 deletion platformio/include/client_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <WiFiClientSecure.h>
#endif

wl_status_t startWiFi(int &wifiRSSI);
wl_status_t startWiFi(int &wifiRSSI, wifi_network_t wifi);
void killWiFi();
bool waitForSNTPSync(tm *timeInfo);
bool printLocalTime(tm *timeInfo);
Expand Down
8 changes: 6 additions & 2 deletions platformio/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ extern const uint8_t PIN_BME_SDA;
extern const uint8_t PIN_BME_SCL;
extern const uint8_t PIN_BME_PWR;
extern const uint8_t BME_ADDRESS;
extern const char *WIFI_SSID;
extern const char *WIFI_PASSWORD;
extern const unsigned long WIFI_TIMEOUT;
extern const unsigned HTTP_CLIENT_TCP_TIMEOUT;
extern const String OWM_APIKEY;
Expand Down Expand Up @@ -309,6 +307,12 @@ extern const unsigned long LOW_BATTERY_SLEEP_INTERVAL;
extern const unsigned long VERY_LOW_BATTERY_SLEEP_INTERVAL;
extern const uint32_t MAX_BATTERY_VOLTAGE;
extern const uint32_t MIN_BATTERY_VOLTAGE;
extern uint8_t WIFI_NETWORKS_COUNT;
struct wifi_network_t {
const char *ssid;
const char *password;
};
extern struct wifi_network_t wifi_networks[];

// CONFIG VALIDATION - DO NOT MODIFY
#if !( defined(DISP_BW_V2) \
Expand Down
8 changes: 4 additions & 4 deletions platformio/src/client_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
*
* Returns WiFi status.
*/
wl_status_t startWiFi(int &wifiRSSI)
wl_status_t startWiFi(int &wifiRSSI, wifi_network_t wifi)
{
WiFi.mode(WIFI_STA);
Serial.printf("%s '%s'", TXT_CONNECTING_TO, WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.printf("%s '%s'", TXT_CONNECTING_TO, wifi.ssid);
WiFi.begin(wifi.ssid, wifi.password);

// timeout if WiFi does not connect in WIFI_TIMEOUT ms from now
unsigned long timeout = millis() + WIFI_TIMEOUT;
Expand All @@ -81,7 +81,7 @@ wl_status_t startWiFi(int &wifiRSSI)
}
else
{
Serial.printf("%s '%s'\n", TXT_COULD_NOT_CONNECT_TO, WIFI_SSID);
Serial.printf("%s '%s'\n", TXT_COULD_NOT_CONNECT_TO, wifi.ssid);
}
return connection_status;
} // startWiFi
Expand Down
10 changes: 8 additions & 2 deletions platformio/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ const uint8_t PIN_BME_PWR = 4; // Irrelevant if directly connected to 3.3V
const uint8_t BME_ADDRESS = 0x76; // If sensor does not work, try 0x77

// WIFI
const char *WIFI_SSID = "ssid";
const char *WIFI_PASSWORD = "password";

// List of Wi-Fi networks to try to connect to in the order of preference
wifi_network_t wifi_networks[] = {
{"ssid", "password"},
};

uint8_t WIFI_NETWORKS_COUNT = sizeof(wifi_networks) / sizeof(wifi_networks[0]);

const unsigned long WIFI_TIMEOUT = 10000; // ms, WiFi connection timeout.

// HTTP
Expand Down
9 changes: 8 additions & 1 deletion platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ void setup()

// START WIFI
int wifiRSSI = 0; // “Received Signal Strength Indicator"
wl_status_t wifiStatus = startWiFi(wifiRSSI);
wl_status_t wifiStatus = WL_DISCONNECTED;
for(int i = 0; i < WIFI_NETWORKS_COUNT; i++) {
wifiStatus = startWiFi(wifiRSSI, wifi_networks[i]);
if (wifiStatus == WL_CONNECTED) {
break;
}
}

if (wifiStatus != WL_CONNECTED)
{ // WiFi Connection Failed
killWiFi();
Expand Down