improved wifi management
- Possibility to create an access point on the ESP32 - Possibility to make the ESP32 try to connect to multiple access points
This commit is contained in:
parent
362789f2a2
commit
c2ccafbcde
@ -2,6 +2,7 @@
|
|||||||
#include "IDECompat.h"
|
#include "IDECompat.h"
|
||||||
|
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include <WiFiMulti.h>
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
@ -24,6 +25,11 @@ const float wheelCircumferenceMeters = wheelDiameterInches * 0.0254f * 3.1415f /
|
|||||||
int16_t batteryVoltage = -1; // in mV
|
int16_t batteryVoltage = -1; // in mV
|
||||||
int16_t batteryCurrent = -1; // in mV
|
int16_t batteryCurrent = -1; // in mV
|
||||||
|
|
||||||
|
WiFiMulti wifiMulti;
|
||||||
|
wl_status_t wifi_STA_status = WL_NO_SHIELD;
|
||||||
|
unsigned long wifiConnexionBegin = 0;
|
||||||
|
const unsigned long retryWifiConnexionDelay = 60000; // in milliseconds
|
||||||
|
|
||||||
volatile bool debugLedState = true;
|
volatile bool debugLedState = true;
|
||||||
|
|
||||||
volatile bool speedSensorState = false;
|
volatile bool speedSensorState = false;
|
||||||
@ -81,6 +87,25 @@ float getSpeed()
|
|||||||
return speed;
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void connectWifi()
|
||||||
|
{
|
||||||
|
wifiMulti = WiFiMulti();
|
||||||
|
|
||||||
|
const int numSSIDs = sizeof(wifi_STA_credentials)/sizeof(wifi_STA_credentials[0]);
|
||||||
|
if(numSSIDs > 0)
|
||||||
|
{
|
||||||
|
Serial.println("Connecting to wifi...");
|
||||||
|
|
||||||
|
for(int idx = 0; idx < numSSIDs; ++idx)
|
||||||
|
{
|
||||||
|
wifiMulti.addAP(wifi_STA_credentials[idx].SSID, wifi_STA_credentials[idx].password);
|
||||||
|
}
|
||||||
|
|
||||||
|
wifiConnexionBegin = millis();
|
||||||
|
wifiMulti.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(speedSensorPin, INPUT_PULLUP);
|
pinMode(speedSensorPin, INPUT_PULLUP);
|
||||||
@ -96,17 +121,23 @@ void setup()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to Wi-Fi
|
// Set WiFi mode to both AccessPoint and Station
|
||||||
WiFi.begin(wifi_ssid, wifi_password);
|
WiFi.mode(WIFI_AP_STA);
|
||||||
while (WiFi.status() != WL_CONNECTED)
|
|
||||||
|
// Create the WiFi Access Point
|
||||||
|
if(wifi_AP_ssid != nullptr)
|
||||||
{
|
{
|
||||||
delay(1000);
|
Serial.println("Creating wifi access point...");
|
||||||
Serial.println("Connecting to Wifi...");
|
WiFi.softAP(wifi_AP_ssid, wifi_AP_password);
|
||||||
|
|
||||||
|
Serial.print("Wifi access point created, SSID=");
|
||||||
|
Serial.print(wifi_AP_ssid);
|
||||||
|
Serial.print(", IP=");
|
||||||
|
Serial.println(WiFi.softAPIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print ESP Local IP Address
|
// Also connect as a station (if the configured remote access point is in range)
|
||||||
Serial.print("Wifi connected, ip=");
|
connectWifi();
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
|
|
||||||
server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
int v = batteryVoltage;
|
int v = batteryVoltage;
|
||||||
@ -131,6 +162,45 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
wl_status_t newWifiStatus = WiFi.status();
|
||||||
|
if(newWifiStatus != wifi_STA_status)
|
||||||
|
{
|
||||||
|
if(newWifiStatus == WL_CONNECTED)
|
||||||
|
{
|
||||||
|
Serial.print("Connected to wifi (");
|
||||||
|
Serial.print(WiFi.SSID().c_str());
|
||||||
|
Serial.print("), ip=");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
else if(newWifiStatus == WL_DISCONNECTED)
|
||||||
|
{
|
||||||
|
char codeStr[16];
|
||||||
|
sprintf(codeStr, "%d", (int)newWifiStatus);
|
||||||
|
Serial.print("Lost wifi connexion (");
|
||||||
|
Serial.print(codeStr);
|
||||||
|
Serial.println(")");
|
||||||
|
|
||||||
|
connectWifi();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char codeStr[16];
|
||||||
|
sprintf(codeStr, "%d", (int)newWifiStatus);
|
||||||
|
Serial.print("Wifi state: ");
|
||||||
|
Serial.println(codeStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
wifi_STA_status = newWifiStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(wifi_STA_status != WL_CONNECTED)
|
||||||
|
{
|
||||||
|
unsigned long now = millis();
|
||||||
|
unsigned long elapsed = now > wifiConnexionBegin ? now - wifiConnexionBegin : (4294967295 - wifiConnexionBegin) + now;
|
||||||
|
if(elapsed > retryWifiConnexionDelay)
|
||||||
|
connectWifi();
|
||||||
|
}
|
||||||
|
|
||||||
const int numSamples = 100;
|
const int numSamples = 100;
|
||||||
|
|
||||||
float averageV = 0.0f;
|
float averageV = 0.0f;
|
||||||
|
@ -1,7 +1,22 @@
|
|||||||
// To avoid saving your wifi credentials in the git repository, wifi-credentials.h.template is ignored by git
|
// To avoid saving your wifi credentials in the git repository, wifi-credentials.h.template is ignored by git
|
||||||
|
|
||||||
// Copy this file and name it wifi-credentials.h
|
// Copy this file and name it wifi-credentials.h
|
||||||
// Then edit your wifi SSID and password
|
// Then edit your wifi configuration
|
||||||
|
|
||||||
const char* wifi_ssid = "REPLACE_WITH_YOUR_SSID";
|
struct WifiCredentials
|
||||||
const char* wifi_password = "REPLACE_WITH_YOUR_PASSWORD";
|
{
|
||||||
|
const char* SSID;
|
||||||
|
const char* password;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Station config: for making the ESP32 connect to an existing remote access point
|
||||||
|
// You can add one or more SSID/password pairs here
|
||||||
|
// To disable station mode, add a single entry { NULL, NULL }
|
||||||
|
WifiCredentials wifi_STA_credentials[] = {
|
||||||
|
{ "REPLACE_WITH_YOUR_SSID", "REPLACE_WITH_YOUR_PASSWORD" }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Access point config: for turning the ESP32 into an access point (the ESP32 can work in both modes at the same time)
|
||||||
|
// Note that if you connect a smartphone to this AP, it will detect it has no internet access, and then won't send requests to it (which will result in a timeout). A workaround is to turn off mobile data to force the phone use the access point (but then you effectively don't have internet on the phone). A more practical setup is to create an access point on your phone, and have the ESP32 connect to it (in "station" mode)
|
||||||
|
const char* wifi_AP_ssid = "REPLACE_WITH_YOUR_SSID"; // set to NULL to disable access point mode
|
||||||
|
const char* wifi_AP_password = "REPLACE_WITH_YOUR_PASSWORD"; // set to NULL for an open access point (not recommended)
|
||||||
|
Loading…
Reference in New Issue
Block a user