1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

[BREAKING] Disable WiFi at boot by default (#7902)

* Disable WiFi at boot by default

* +define WIFI_IS_OFF_AT_BOOT

* remove now useless example

* mv enableWiFiAtBootTime() to core_esp8266_features.h

* sync with master

* per @earlephilhower review: a file was missing

* doc

* WiFi persistence is now false by default

* fix doc

* ditto

* doc: remove sphinx warnings (fix links and formatting)

* fix link name

* fix doc

* legacy: restore persistence

* undeprecate preinit()

* move force modem up to when mode has changed (per @mcspr review)

* do not wake up from sleep when mode if OFF

* fix doc per review
This commit is contained in:
david gauchard
2021-04-09 23:01:11 +02:00
committed by GitHub
parent da6ec83b5f
commit 1cc6960a55
22 changed files with 107 additions and 142 deletions

View File

@ -1,59 +0,0 @@
#include <ESP8266WiFi.h>
#include <AddrList.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
// preinit() is called before system startup
// from nonos-sdk's user entry point user_init()
void preinit() {
// Global WiFi constructors are not called yet
// (global class instances like WiFi, Serial... are not yet initialized)..
// No global object methods or C++ exceptions can be called in here!
//The below is a static class method, which is similar to a function, so it's ok.
ESP8266WiFiClass::preinitWiFiOff();
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("sleeping 5s");
// during this period, a simple amp meter shows
// an average of 20mA with a Wemos D1 mini
// a DSO is needed to check #2111
delay(5000);
Serial.println("waking WiFi up, sleeping 5s");
WiFi.forceSleepWake();
// amp meter raises to 75mA
delay(5000);
Serial.println("connecting to AP " STASSID);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
for (bool configured = false; !configured;) {
for (auto addr : addrList)
if ((configured = !addr.isLocal() && addr.ifnumber() == STATION_IF)) {
Serial.printf("STA: IF='%s' hostname='%s' addr= %s\n",
addr.ifname().c_str(),
addr.ifhostname(),
addr.toString().c_str());
break;
}
Serial.print('.');
delay(500);
}
// amp meter cycles within 75-80 mA
}
void loop() {
}

View File

@ -24,11 +24,6 @@ WiFiState state;
const char* ssid = STASSID;
const char* password = STAPSK;
void preinit(void) {
// Make sure, wifi stays off after boot.
ESP8266WiFiClass::preinitWiFiOff();
}
void setup() {
Serial.begin(74880);
//Serial.setDebugOutput(true); // If you need debug output

View File

@ -83,7 +83,7 @@ struct WiFiEventHandlerOpaque
static std::list<WiFiEventHandler> sCbEventList;
bool ESP8266WiFiGenericClass::_persistent = true;
bool ESP8266WiFiGenericClass::_persistent = false;
WiFiMode_t ESP8266WiFiGenericClass::_forceSleepLastMode = WIFI_OFF;
ESP8266WiFiGenericClass::ESP8266WiFiGenericClass()
@ -418,12 +418,6 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
DEBUG_WIFI("core: state is useless without SHUTDOWN or RESUME\n");
}
if (wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
// wifi may have been put asleep by ESP8266WiFiGenericClass::preinitWiFiOff
wifi_fpm_do_wakeup();
wifi_fpm_close();
}
if(_persistent){
if(wifi_get_opmode() == (uint8) m && wifi_get_opmode_default() == (uint8) m){
return true;
@ -432,6 +426,12 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m, WiFiState* state) {
return true;
}
if (m != WIFI_OFF && wifi_fpm_get_sleep_type() != NONE_SLEEP_T) {
// wifi starts asleep by default
wifi_fpm_do_wakeup();
wifi_fpm_close();
}
bool ret = false;
ETS_UART_INTR_DISABLE();
if(_persistent) {
@ -855,25 +855,7 @@ bool ESP8266WiFiGenericClass::resumeFromShutdown (WiFiState* state)
return true;
}
//meant to be called from user-defined ::preinit()
void ESP8266WiFiGenericClass::preinitWiFiOff () {
// https://github.com/esp8266/Arduino/issues/2111#issuecomment-224251391
// WiFi.persistent(false);
// WiFi.mode(WIFI_OFF);
// WiFi.forceSleepBegin();
//WiFi.mode(WIFI_OFF) equivalent:
// datasheet:
// Set Wi-Fi working mode to Station mode, SoftAP
// or Station + SoftAP, and do not update flash
// (not persistent)
wifi_set_opmode_current(WIFI_OFF);
//WiFi.forceSleepBegin(/*default*/0) equivalent:
// sleep forever until wifi_fpm_do_wakeup() is called
wifi_fpm_set_sleep_type(MODEM_SLEEP_T);
wifi_fpm_open();
wifi_fpm_do_sleep(0xFFFFFFF);
// use WiFi.forceSleepWake() to wake WiFi up
// It was meant to be called from user-defined ::preinit()
// It is now deprecated by enableWiFiAtBootTime() and __disableWiFiAtBootTime()
}

View File

@ -120,7 +120,7 @@ class ESP8266WiFiGenericClass {
void setOutputPower(float dBm);
void persistent(bool persistent);
static void persistent(bool persistent);
bool mode(WiFiMode_t, WiFiState* state = nullptr);
WiFiMode_t getMode();
@ -133,7 +133,7 @@ class ESP8266WiFiGenericClass {
static uint32_t shutdownCRC (const WiFiState* state);
static bool shutdownValidCRC (const WiFiState* state);
static void preinitWiFiOff (); //meant to be called in user-defined preinit()
static void preinitWiFiOff () __attribute__((deprecated("WiFi is off by default at boot, use enableWiFiAtBoot() for legacy behavior")));
protected:
static bool _persistent;

View File

@ -0,0 +1,28 @@
/*
* empty wrappers to play with linker and reenable wifi at boot time
*/
#include "coredecls.h"
#include <ESP8266WiFi.h>
extern "C" void enableWiFiAtBootTime()
{
/*
* Called by user from anywhere, does nothing and allows overriding
* the core_esp8266_main.cpp's default disableWiFiAtBootTime() by the
* one below, at link time.
*/
}
extern "C" void __disableWiFiAtBootTime()
{
// overrides the default __disableWiFiAtBootTime:
// Does (almost) nothing: WiFi is enabled by default in nonos-sdk
// ... but restores legacy WiFi credentials persistence to true at boot time
// (can be still overriden by user before setting up WiFi, like before)
// (note: c++ ctors not called yet at this point)
ESP8266WiFiClass::persistent(true);
}

View File

@ -57,12 +57,6 @@ void error(const char* what) {
}
}
void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);

View File

@ -98,12 +98,6 @@ void proxyEspSync() {
//
////////////////////////////////////////////////////////////////////////////////
void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}
void setup() {
// For `proxyEspSync()` to work, the Serial.begin() speed needs to be
// 115200bps. This is the data rate used by esptool.py. It expects the Boot

View File

@ -9,7 +9,6 @@
//or #include <ENC28J60lwIP.h>
#include <WiFiClient.h> // WiFiClient (-> TCPClient)
#include <ESP8266WiFi.h> // ESP8266WiFiClass::preinitWiFiOff()
const char* host = "djxmmx.net";
const uint16_t port = 17;
@ -19,12 +18,6 @@ using TCPClient = WiFiClient;
#define CSPIN 16 // wemos/lolin/nodemcu D0
Wiznet5500lwIP eth(CSPIN);
void preinit() {
// (no C++ in function)
// disable wifi
ESP8266WiFiClass::preinitWiFiOff();
}
void setup() {
Serial.begin(115200);