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

Feature/wifi manager #164

Draft
wants to merge 54 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
90d4bf4
add WiFiManager library ref
jfujitani Jul 8, 2023
b964dc6
integrate WiFiManager
jfujitani Jul 8, 2023
7022049
remove callback causing bootloop
jfujitani Jul 8, 2023
5c4db75
init display and fix display message
jfujitani Jul 8, 2023
b1f256c
restore and fix configModeCallback
jfujitani Jul 8, 2023
3e204ce
re-add default wifi
jfujitani Jul 30, 2023
b4e2d14
document wifi manager
jfujitani Jul 30, 2023
58381eb
clean up instructions
jfujitani Jul 30, 2023
ed97249
Merge pull request #1 from lmarzen/main
jfujitani Sep 24, 2023
8c065f6
Merge branch 'main' into feature/wifi-manager
jfujitani Sep 24, 2023
1bb248d
Merge pull request #2 from lmarzen/main
jfujitani Oct 26, 2023
5b7d0ea
merge upstream changes
jfujitani Oct 26, 2023
39e339d
fix formatting
jfujitani Oct 26, 2023
2aee5f6
Merge pull request #3 from lmarzen/main
jfujitani Nov 21, 2023
ed2ce6a
merge main
jfujitani Nov 21, 2023
70004be
fix auto formating
jfujitani Nov 21, 2023
d6e90c1
Merge pull request #4 from lmarzen/main
jfujitani Dec 4, 2023
742a1af
Merge branch 'main' into feature/wifi-manager
jfujitani Dec 4, 2023
4612454
Merge pull request #5 from lmarzen/main
jfujitani Dec 16, 2023
f02d188
Merge branch 'main' into feature/wifi-manager
jfujitani Dec 16, 2023
cab8d7d
Merge pull request #6 from lmarzen/main
jfujitani Dec 22, 2023
d9ada87
Merge branch 'main' into feature/wifi-manager
jfujitani Dec 22, 2023
fc685a3
Merge pull request #7 from lmarzen/main
jfujitani Jan 4, 2024
86f8a4b
Merge branch 'main' into feature/wifi-manager
jfujitani Jan 4, 2024
9c323a8
Merge pull request #8 from lmarzen/main
jfujitani Jan 13, 2024
adfc149
Merge branch 'main' into feature/wifi-manager
jfujitani Jan 13, 2024
a237916
Merge pull request #9 from lmarzen/main
jfujitani Mar 23, 2024
f72b03d
update packages
jfujitani Mar 23, 2024
ad06ebe
Merge pull request #10 from lmarzen/main
jfujitani Apr 13, 2024
4cf9d49
Merge pull request #11 from lmarzen/main
jfujitani Apr 13, 2024
23b383f
Merge branch 'main' into feature/wifi-manager
jfujitani Apr 13, 2024
e00d71d
Merge branch 'feature/wifi-manager' of github.com:jfujitani/esp32-wea…
jfujitani Apr 13, 2024
87fab08
remove inTemp offset
jfujitani Apr 13, 2024
d385ecc
Merge pull request #12 from lmarzen/main
jfujitani Apr 20, 2024
bfe8316
Merge pull request #13 from lmarzen/main
jfujitani Apr 20, 2024
ca59d41
Merge pull request #14 from lmarzen/main
jfujitani Apr 21, 2024
7dabe76
Merge pull request #15 from lmarzen/main
jfujitani Apr 21, 2024
32693ed
Merge pull request #16 from lmarzen/main
jfujitani Apr 27, 2024
0d1cb12
Merge pull request #17 from lmarzen/main
jfujitani Jun 9, 2024
7fe682b
merge main
jfujitani Jun 9, 2024
5686b8b
Merge pull request #18 from lmarzen/main
jfujitani Jun 22, 2024
d18f715
Merge pull request #19 from lmarzen/main
jfujitani Aug 3, 2024
287f66d
Merge branch 'main' into feature/wifi-manager
jfujitani Aug 3, 2024
e0e7714
Merge pull request #20 from lmarzen/main
jfujitani Sep 7, 2024
f182be2
merge up stream changes
jfujitani Sep 7, 2024
a624989
Merge pull request #21 from lmarzen/main
jfujitani Oct 5, 2024
79ab4f1
merge main
jfujitani Oct 5, 2024
871ea5d
Merge pull request #22 from lmarzen/main
jfujitani Nov 3, 2024
6477032
Merge branch 'main' into feature/wifi-manager
jfujitani Nov 3, 2024
3d52c17
Merge pull request #23 from lmarzen/main
jfujitani Mar 8, 2025
8cf0cf9
merge upstream
jfujitani Mar 8, 2025
de76df3
fix README
jfujitani Mar 8, 2025
30277be
Merge pull request #24 from lmarzen/main
jfujitani Mar 15, 2025
949ad7f
Merge branch 'main' into feature/wifi-manager
jfujitani Mar 15, 2025
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
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ PlatformIO for VSCode is used for managing dependencies, code compilation, and u

- Important settings to configure in config.cpp:

- WiFi credentials (ssid, password).

- WiFi credentials (ssid, password, ap ssid).
- Open Weather Map API key (it's free, see next section for important notes about obtaining an API key).

- Latitude and longitude.
Expand Down Expand Up @@ -238,7 +238,13 @@ PlatformIO for VSCode is used for managing dependencies, code compilation, and u

- If using a FireBeetle 2 ESP32-E and you receive the error `Wrong boot mode detected (0x13)! The chip needs to be in download mode.` unplug the power from the board, connect GPIO0 ([labeled 0/D5](https://wiki.dfrobot.com/FireBeetle_Board_ESP32_E_SKU_DFR0654#target_5)) to GND, and power it back up to put the board in download mode.

- If you are getting other errors during the upload process, you may need to install drivers to allow you to upload code to the ESP32.
7. Boot options.

- To support a change in WiFi networks without requiring a change in code and re-upload, [WiFi Manager](https://github.com/tzapu/WiFiManager) has been integrated. To use this feature, pull HW pin 27 `PIN_CONFIGURE_WIFI` down and press and release the reset button. This will cause the weather station to create an ad-hoc wifi network named "Weather_Station" `WIFI_AP_SSID`. Connect to this network to configure the WiFi network. Visit the WiFi Manger site for further details.
> **Note**
> Each time you reset using this method the previously configured WiFi network will be lost. After you perform this reset the compiled defaults can still be used by performing a normal reset without connecting to the ad-hoc network and configuring the WiFi.

- If you are getting other errors during the upload process, you may need to install drivers to allow you to upload code to the ESP32.
### OpenWeatherMap API Key

Sign up here to get an API key; it's free. <https://openweathermap.org/api>
Expand Down
2 changes: 2 additions & 0 deletions platformio/include/client_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#endif

wl_status_t startWiFi(int &wifiRSSI);
wl_status_t startDefaultWiFi(int &wifiRSSI);
wl_status_t configureWiFi(int &wifiRSSI);
void killWiFi();
bool waitForSNTPSync(tm *timeInfo);
bool printLocalTime(tm *timeInfo);
Expand Down
2 changes: 2 additions & 0 deletions platformio/include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ 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 uint8_t PIN_CONFIGURE_WIFI;
extern const char *WIFI_SSID;
extern const char *WIFI_PASSWORD;
extern const char *WIFI_AP_SSID;
extern const unsigned long WIFI_TIMEOUT;
extern const unsigned HTTP_CLIENT_TCP_TIMEOUT;
extern const String OWM_APIKEY;
Expand Down
5 changes: 4 additions & 1 deletion platformio/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ lib_deps =
adafruit/Adafruit BME280 Library @ ^2.2.4
adafruit/Adafruit BusIO @ ^1.16.2
adafruit/Adafruit Unified Sensor @ ^1.1.14
; WiFiManager seems to have some platformio build integration issues
; possibly related to release tag conventions.
; https://github.com/tzapu/WiFiManager/issues/1453
https://github.com/tzapu/[email protected]
bblanchon/ArduinoJson @ ^7.3.0
zinggjm/GxEPD2 @ ^1.6.1


[env:dfrobot_firebeetle2_esp32e]
board = dfrobot_firebeetle2_esp32e
monitor_speed = 115200
Expand Down
98 changes: 80 additions & 18 deletions platformio/src/client_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <SPI.h>
#include <time.h>
#include <WiFi.h>
#include <WiFiManager.h>

// additional libraries
#include <Adafruit_BusIO_Register.h>
Expand All @@ -49,7 +50,7 @@
static const uint16_t OWM_PORT = 443;
#endif

/* Power-on and connect WiFi.
/* Power-on and connect WiFi using WiFi Manager.
* Takes int parameter to store WiFi RSSI, or “Received Signal Strength
* Indicator"
*
Expand All @@ -58,33 +59,94 @@
wl_status_t startWiFi(int &wifiRSSI)
{
WiFi.mode(WIFI_STA);
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;
wl_status_t connection_status = WiFi.status();
WiFiManager wifi_manager;
wifi_manager.setConnectTimeout(10);

while ((connection_status != WL_CONNECTED) && (millis() < timeout))
// Turn off config AP fallback to save on battery on random errors
wifi_manager.setEnableConfigPortal(false);
if (wifi_manager.autoConnect(WIFI_AP_SSID))
{
Serial.print(".");
delay(50);
connection_status = WiFi.status();
wifiRSSI = WiFi.RSSI(); // get WiFi signal strength now, because the WiFi
// will be turned off to save power!
Serial.println("IP: " + WiFi.localIP().toString());
}
Serial.println();
return WiFi.status();
} // startWiFi

/* Power-on and connect WiFi.
* Takes int parameter to store WiFi RSSI, or “Received Signal Strength
* Indicator"
*
* Returns WiFi status.
*/
wl_status_t startDefaultWiFi(int &wifiRSSI)
{
WiFi.mode(WIFI_STA);
Serial.printf("Connecting to '%s'", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

// timeout if WiFi does not connect in 10s from now
unsigned long timeout = millis() + WIFI_TIMEOUT;
wl_status_t connection_status = WiFi.status();

while ((connection_status != WL_CONNECTED) && (millis() < timeout))
{
Serial.print(".");
delay(50);
connection_status = WiFi.status();
}
Serial.println();

if (connection_status == WL_CONNECTED)
if (connection_status == WL_CONNECTED)
{
wifiRSSI = WiFi.RSSI(); // get WiFi signal strength now, because the WiFi
// will be turned off to save power!
Serial.println("IP: " + WiFi.localIP().toString());
}
else
{
Serial.printf("Could not connect to '%s'\n", WIFI_SSID);
}
return connection_status;
} // startDefaultWiFi

/* Callback when WiFi config AP is started
* Takes in a WiFiManager instance.
*/
void configModeCallback(WiFiManager *myWiFiManager)
{
Serial.println("Entered Configuration Mode");
Serial.print("Config SSID: ");
Serial.println(myWiFiManager->getConfigPortalSSID());
Serial.print("Config IP Address: ");
Serial.println(WiFi.softAPIP());
}

/* Power-on and start WiFi config AP.
* Takes int parameter to store WiFi RSSI, or “Received Signal Strength
* Indicator"
*
* Returns WiFi status.
*/
wl_status_t configureWiFi(int &wifiRSSI)
{
WiFi.mode(WIFI_STA);

WiFiManager wifi_manager;
wifi_manager.setConnectTimeout(10);
wifi_manager.setConfigPortalTimeout(60 * 5);
wifi_manager.setAPCallback(configModeCallback);

// Start the configuration AP portal with 10 minute timeout
if (wifi_manager.startConfigPortal(WIFI_AP_SSID))
{
wifiRSSI = WiFi.RSSI(); // get WiFi signal strength now, because the WiFi
// will be turned off to save power!
Serial.println("IP: " + WiFi.localIP().toString());
}
else
{
Serial.printf("%s '%s'\n", TXT_COULD_NOT_CONNECT_TO, WIFI_SSID);
}
return connection_status;
} // startWiFi
return WiFi.status();
} // configureWiFi

/* Disconnect and power-off WiFi.
*/
Expand Down
43 changes: 24 additions & 19 deletions platformio/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,46 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <Arduino.h>
#include "config.h"
#include <Arduino.h>

// PINS
// The configuration below is intended for use with the project's official
// The configuration below is intended for use with the project's official
// wiring diagrams using the FireBeetle 2 ESP32-E microcontroller board.
//
// Note: LED_BUILTIN pin will be disabled to reduce power draw. Refer to your
// board's pinout to ensure you avoid using a pin with this shared
// board's pinout to ensure you avoid using a pin with this shared
// functionality.
//
// ADC pin used to measure battery voltage
const uint8_t PIN_BAT_ADC = A2; // A0 for micro-usb firebeetle
const uint8_t PIN_BAT_ADC = A2; // A0 for micro-usb firebeetle
// Pins for E-Paper Driver Board
const uint8_t PIN_EPD_BUSY = 14; // 5 for micro-usb firebeetle
const uint8_t PIN_EPD_CS = 13;
const uint8_t PIN_EPD_RST = 21;
const uint8_t PIN_EPD_DC = 22;
const uint8_t PIN_EPD_SCK = 18;
const uint8_t PIN_EPD_MISO = 19; // 19 Master-In Slave-Out not used, as no data from display
const uint8_t PIN_EPD_CS = 13;
const uint8_t PIN_EPD_RST = 21;
const uint8_t PIN_EPD_DC = 22;
const uint8_t PIN_EPD_SCK = 18;
const uint8_t PIN_EPD_MISO =
19; // 19 Master-In Slave-Out not used, as no data from display
const uint8_t PIN_EPD_MOSI = 23;
const uint8_t PIN_EPD_PWR = 26; // Irrelevant if directly connected to 3.3V
const uint8_t PIN_EPD_PWR = 26; // Irrelevant if directly connected to 3.3V
// I2C Pins used for BME280
const uint8_t PIN_BME_SDA = 17;
const uint8_t PIN_BME_SCL = 16;
const uint8_t PIN_BME_PWR = 4; // Irrelevant if directly connected to 3.3V
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_SSID = "ssid";
const char *WIFI_PASSWORD = "password";
const unsigned long WIFI_TIMEOUT = 10000; // ms, WiFi connection timeout.

// Pin used for WiFi config AP
const uint8_t PIN_CONFIGURE_WIFI = 27; // 27 is a HW button on the Firebeetle
// WIFI
const char *WIFI_AP_SSID = "Weather_Station";
// HTTP
// The following errors are likely the result of insuffient http client tcp
// The following errors are likely the result of insuffient http client tcp
// timeout:
// -1 Connection Refused
// -11 Read Timeout
Expand All @@ -58,7 +63,7 @@ const unsigned HTTP_CLIENT_TCP_TIMEOUT = 10000; // ms

// OPENWEATHERMAP API
// OpenWeatherMap API key, https://openweathermap.org/
const String OWM_APIKEY = "abcdefghijklmnopqrstuvwxyz012345";
const String OWM_APIKEY = "abcdefghijklmnopqrstuvwxyz012345";
const String OWM_ENDPOINT = "api.openweathermap.org";
// OpenWeatherMap One Call 2.5 API is deprecated for all new free users
// (accounts created after Summer 2022).
Expand All @@ -69,7 +74,8 @@ const String OWM_ENDPOINT = "api.openweathermap.org";
// product.
//
// Here’s how to subscribe and avoid any credit card changes:
// - Go to https://home.openweathermap.org/subscriptions/billing_info/onecall_30/base?key=base&service=onecall_30
// - Go to
// https://home.openweathermap.org/subscriptions/billing_info/onecall_30/base?key=base&service=onecall_30
// - Follow the instructions to complete the subscription.
// - Go to https://home.openweathermap.org/subscriptions and set the "Calls per
// day (no more than)" to 1,000. This ensures you will never overrun the free
Expand All @@ -92,12 +98,12 @@ const char *TIMEZONE = "EST5EDT,M3.2.0,M11.1.0";
// For more information about formatting see
// https://man7.org/linux/man-pages/man3/strftime.3.html
// const char *TIME_FORMAT = "%l:%M%P"; // 12-hour ex: 1:23am 11:00pm
const char *TIME_FORMAT = "%H:%M"; // 24-hour ex: 01:23 23:00
const char *TIME_FORMAT = "%H:%M"; // 24-hour ex: 01:23 23:00
// Time format used when displaying axis labels. (Max 11 characters)
// For more information about formatting see
// https://man7.org/linux/man-pages/man3/strftime.3.html
// const char *HOUR_FORMAT = "%l%P"; // 12-hour ex: 1am 11pm
const char *HOUR_FORMAT = "%H"; // 24-hour ex: 01 23
const char *HOUR_FORMAT = "%H"; // 24-hour ex: 01 23
// Date format used when displaying date in top-right corner.
// For more information about formatting see
// https://man7.org/linux/man-pages/man3/strftime.3.html
Expand All @@ -124,7 +130,7 @@ const int SLEEP_DURATION = 30; // minutes
// Bed Time Power Savings.
// If BED_TIME == WAKE_TIME, then this battery saving feature will be disabled.
// (range: [0-23])
const int BED_TIME = 00; // Last update at 00:00 (midnight) until WAKE_TIME.
const int BED_TIME = 00; // Last update at 00:00 (midnight) until WAKE_TIME.
const int WAKE_TIME = 06; // Hour of first update after BED_TIME, 06:00.
// Note that the minute alignment of SLEEP_DURATION begins at WAKE_TIME even if
// Bed Time Power Savings is disabled.
Expand Down Expand Up @@ -163,4 +169,3 @@ const uint32_t MIN_BATTERY_VOLTAGE = 3000; // (millivolts)
// FONTS
// ALERTS
// BATTERY MONITORING

42 changes: 37 additions & 5 deletions platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,48 @@ void setup()
uint32_t batteryVoltage = UINT32_MAX;
#endif

// All data should have been loaded from NVS. Close filesystem.
prefs.end();

String statusStr = {};
String tmpStr = {};
tm timeInfo = {};

// START WIFI
wl_status_t wifiStatus;
int wifiRSSI = 0; // “Received Signal Strength Indicator"
wl_status_t wifiStatus = startWiFi(wifiRSSI);

pinMode(PIN_CONFIGURE_WIFI, INPUT_PULLUP);
if (digitalRead(PIN_CONFIGURE_WIFI) == LOW)
{
prefs.putBool("prev_configured", false);
Serial.println("WIFI config pin detected");
initDisplay();
do
{
drawError(wifi_x_196x196, "Weather Station is in", "WIFI configuration mode");
} while (display.nextPage());
// Configure WIFI
Serial.println("Entering config mode");
wifiStatus = configureWiFi(wifiRSSI);
if (wifiStatus == WL_CONNECTED)
{
prefs.putBool("prev_configured", true);
}
}
else
{
if (prefs.getBool("prev_configured", false))
{
wifiStatus = startWiFi(wifiRSSI);
}
else
{
wifiStatus = startDefaultWiFi(wifiRSSI);
}
}


// All data should have been loaded from NVS. Close filesystem.
prefs.end();

if (wifiStatus != WL_CONNECTED)
{ // WiFi Connection Failed
killWiFi();
Expand All @@ -224,7 +256,7 @@ void setup()
}
powerOffDisplay();
beginDeepSleep(startTime, &timeInfo);
}
}

// TIME SYNCHRONIZATION
configTzTime(TIMEZONE, NTP_SERVER_1, NTP_SERVER_2);
Expand Down