Skip to content

Commit

Permalink
Merge pull request #6 from renatobo/feature/togglewifi
Browse files Browse the repository at this point in the history
Feature/togglewifi
  • Loading branch information
renatobo authored Dec 14, 2020
2 parents 54039f9 + 3509028 commit 4ba82de
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ This code is developed specifically for ESP32, and tested with [PlatformIO](http
- [Uptime Library](https://github.com/YiannisBourkelis/Uptime-Library)
- [EasyButton](https://easybtn.earias.me/)

You can reduce flash size by ~30kb of Nimble-Arduino removing role roles `CONFIG_BT_NIMBLE_ROLE_CENTRAL` and `CONFIG_BT_NIMBLE_ROLE_OBSERVER` in `nimconfig.h` by simply commenting the two defines.

### Optional libraries depending on #define options
- [Task Scheduler](https://github.com/arkhipenko/TaskScheduler) [included by default]
- [NeoGPS](https://github.com/SlashDevin/NeoGPS) [not included right now, but coded and available for some additional cases]
Expand Down
2 changes: 0 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ build_flags =
-D CORE_DEBUG_LEVEL=4
-D OTA_AVAILABLE=-1
-D BUILD_ENV_NAME='"test build"'
-D MYNEWT_VAL_BLE_MAX_CONNECTIONS=1

[env:release]
build_flags =
Expand All @@ -46,7 +45,6 @@ build_flags =
-D CORE_DEBUG_LEVEL=0
-D BUILD_ENV_NAME='"production build"'
-D NO_GLOBAL_ARDUINOOTA
-D MYNEWT_VAL_BLE_MAX_CONNECTIONS=1

[ota]
upload_protocol = espota
Expand Down
100 changes: 96 additions & 4 deletions src/bonogps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ EasyButton button(WIFI_MODE_BUTTON);
********************************/

void WebConfig_start();
void WebConfig_stop();
void gps_initialize_settings();
void restart_after_sleep();
void wifi_AP();
void wifi_OFF();
#ifdef ENABLE_OTA
void handle_OTA();
void OTA_start();
Expand Down Expand Up @@ -388,6 +390,9 @@ void ReadNVMPreferences()
case WIFI_STA:
string_wifi_mode = "WIFI_STA";
break;
case WIFI_OFF:
string_wifi_mode = "WIFI_OFF";
break;
default:
string_wifi_mode = "WIFI_STA";
break;
Expand All @@ -403,11 +408,21 @@ void ReadNVMPreferences()
stored_preferences.wifi_mode = WIFI_STA;
log_d("Preference read as WIFI_STA");
}
else
else if (string_wifi_mode == "WIFI_AP")
{
stored_preferences.wifi_mode = WIFI_AP;
log_d("Preference read as WIFI_AP");
}
else if (string_wifi_mode == "WIFI_OFF")
{
stored_preferences.wifi_mode = WIFI_OFF;
log_d("Preference read as WIFI_OFF");
}
else
{
stored_preferences.wifi_mode = WIFI_AP;
log_d("Error reading WiFi, Preference set as WIFI_AP");
}

// this being a char*, we pass size
prefs.getString("wifikey", stored_preferences.wifi_key, WIFI_KEY_MAXLEN);
Expand Down Expand Up @@ -449,6 +464,9 @@ void StoreNVMPreferences(bool savewifi = false)
case WIFI_STA:
string_wifi_mode = "WIFI_STA";
break;
case WIFI_OFF:
string_wifi_mode = "WIFI_OFF";
break;
default:
string_wifi_mode = "WIFI_STA";
break;
Expand Down Expand Up @@ -653,7 +671,9 @@ void wifi_STA()
if (WiFi.status() == WL_CONNECTED)
{
log_i("Connected to SSID %s", stored_preferences.wifi_ssid);
#ifdef TASK_SCHEDULER
tLedBlink.setInterval(250);
#endif
wifi_connected = true;
#ifdef ENABLE_OTA
log_i("Start OTA service");
Expand All @@ -672,7 +692,7 @@ void wifi_STA()
MyIP = WiFi.localIP();

#ifdef BUTTON
button.onPressed(wifi_AP);
button.onPressed(wifi_OFF);
#endif
stored_preferences.wifi_mode = WIFI_STA;
}
Expand Down Expand Up @@ -747,6 +767,52 @@ void wifi_AP()
stored_preferences.wifi_mode = WIFI_AP;
}

void wifi_OFF()
{

#ifdef TASK_SCHEDULER
log_d("Flash blinking");
tLedBlink.setInterval(50);
#endif

#ifdef ENABLE_OTA
log_i("Stop OTA service");
OTA_stop();
#endif
#ifdef NUMERICAL_BROADCAST_PROTOCOL
// WLAN Server for GNSS data
stop_NBP_server();
#endif
// WiFi Access
if (stored_preferences.nmeaTcpServer)
stop_NMEA_server();

log_i("Stop Web Portal");
WebConfig_stop();

#ifdef MDNS_ENABLE
MDNS.end();
#endif

WiFi.mode(WIFI_OFF);
log_i("Stopped WiFi service");

#ifdef TASK_SCHEDULER
log_d("Stop blinking");
tLedBlink.setInterval(0);
tLedBlink.disable();
ledon=HIGH;
led_blink();
#endif

#ifdef BUTTON
button.onPressed(wifi_AP);
#endif

stored_preferences.wifi_mode = WIFI_OFF;

}

/********************************
Web Configuration portal
Expand All @@ -771,7 +837,7 @@ const char WEBPORTAL_FOOTER[] PROGMEM = "\n<footer>Version: <a style='font-size:
#else
const char WEBPORTAL_FOOTER[] PROGMEM = "\n<footer>Version: " BONO_GPS_VERSION "</footer>\n</body>\n</html>";
#endif
const char WEBPORTAL_ROOT_OPTIONS[] PROGMEM = "\n</details>\n<details><summary>Device</summary>\n<article>Suspend GPS for <a href='/powersave/1800'>30'</a> <a href='/powersave/3600'>1 hr</a></article>\n<article><a href='/preset'>Load Preset</a></article>\n<article><a href='/savecfg'>Save config</a></article>\n<article><a href='/status'>Information</a></article>\n<article><a href='/savewificreds'>Set WiFi credentials</a></article>\n<article><a href='/restart'>Restart</a><br><br></article></details>";
const char WEBPORTAL_ROOT_OPTIONS[] PROGMEM = "\n</details>\n<details><summary>Device</summary>\n<article>Suspend GPS for <a href='/powersave/1800'>30'</a> <a href='/powersave/3600'>1 hr</a></article>\n<article>Disable<a href='/wifioff'>WiFi</a></article>\n<article><a href='/preset'>Load Preset</a></article>\n<article><a href='/savecfg'>Save config</a></article>\n<article><a href='/status'>Information</a></article>\n<article><a href='/savewificreds'>Set WiFi credentials</a></article>\n<article><a href='/restart'>Restart</a><br><br></article></details>";

// Helpers to populate a page with style sheet
String generate_html_header(bool add_menu = true)
Expand Down Expand Up @@ -1586,6 +1652,17 @@ void handle_wifi_mode()
webserver.send(400, html_text, generate_html_body(String("Wrong WiFi mode selected")));
}
}
void handle_wifioff()
{
webserver.send(200, html_text, generate_html_body(F("Please confirm disabling WiFi - you will need to power cycle the unit or use the BOOT button to re-enable it<form action='/wifioff' method='post'><input type='submit' value='Turn WiFi OFF'></form>")));
}
void handle_wifioff_post()
{
log_i("WiFi OFF");
webserver.send(200, html_text, generate_html_body(F("Powering off WiFi - power cycle the unit or use BOOT to re-enable it")));
delay(1000);
wifi_OFF();
}

void handle_restart()
{
Expand Down Expand Up @@ -1685,6 +1762,12 @@ void handle_status()
message += String(ESP.getPsramSize());
message += F("<br>Free PSRAM: ");
message += String(ESP.getFreePsram());
message += F("<p>SDK version: ");
message += ESP.getSdkVersion();
message += F("<br>Chip Revision: ");
message += String(ESP.getChipRevision());
message += F("<br>CPU Freq: ");
message += String(ESP.getCpuFreqMHz());

webserver.send(200, html_text, generate_html_body(message));
}
Expand Down Expand Up @@ -1820,6 +1903,8 @@ void WebConfig_start()
webserver.on("/wifi/{}", handle_wifi_mode);
webserver.on("/restart", HTTP_GET, handle_restart);
webserver.on("/restart", HTTP_POST, handle_restart_execute);
webserver.on("/wifioff", HTTP_GET, handle_wifioff);
webserver.on("/wifioff", HTTP_POST, handle_wifioff_post);
webserver.on("/savecfg", handle_saveconfig);
webserver.on("/savecfg/{}", handle_saveconfig_wifi);
webserver.on("/savewifi/{}", handle_saveconfig_wifimode);
Expand All @@ -1846,6 +1931,11 @@ void WebConfig_start()
#endif
}

void WebConfig_stop()
{
webserver.close();
}

/********************************
OTA
Expand Down Expand Up @@ -2189,8 +2279,10 @@ void setup()
case WIFI_STA:
wifi_STA();
break;
case WIFI_OFF:
break;
default:
wifi_STA();
wifi_AP();
break;
}

Expand Down

0 comments on commit 4ba82de

Please sign in to comment.