mirror of
https://github.com/easytarget/esp32-cam-webserver.git
synced 2024-02-16 18:18:24 +08:00
Wifi persistance (#46)
* Making the WiFi reconnect more robust. (#44) * Configurable wifi watchdog * Fix logic, Better user feedback for connect/disconnect Co-authored-by: Athrogate <AthrogatePEQ@users.noreply.github.com>
This commit is contained in:
@ -70,6 +70,10 @@ extern void startCameraServer(int hPort, int sPort);
|
||||
int streamPort = 81;
|
||||
#endif
|
||||
|
||||
#if !defined(WIFI_WATCHDOG)
|
||||
#define WIFI_WATCHDOG 5000
|
||||
#endif
|
||||
|
||||
// The stream URL
|
||||
char streamURL[64] = {"Undefined"}; // Stream URL to pass to the app.
|
||||
|
||||
@ -135,7 +139,77 @@ void setLamp(int newVal) {
|
||||
Serial.print("%, pwm = ");
|
||||
Serial.println(brightness);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSetup(){
|
||||
// Feedback that we are now attempting to connect
|
||||
flashLED(400);
|
||||
|
||||
#if defined(WIFI_AP_ENABLE)
|
||||
#if defined(AP_ADDRESS)
|
||||
// User has specified the AP details, pre-configure AP
|
||||
IPAddress local_IP(AP_ADDRESS);
|
||||
IPAddress gateway(AP_ADDRESS);
|
||||
IPAddress subnet(255,255,255,0);
|
||||
WiFi.softAPConfig(local_IP, gateway, subnet);
|
||||
#endif
|
||||
#if defined(AP_CHAN)
|
||||
WiFi.softAP(ssid, password, AP_CHAN);
|
||||
Serial.println("Setting up Fixed Channel AccessPoint");
|
||||
Serial.print("SSID : ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("Password : ");
|
||||
Serial.println(password);
|
||||
Serial.print("Channel : ");
|
||||
Serial.println(AP_CHAN);
|
||||
# else
|
||||
WiFi.softAP(ssid, password);
|
||||
Serial.println("Setting up AccessPoint");
|
||||
Serial.print("SSID : ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("Password : ");
|
||||
Serial.println(password);
|
||||
#endif
|
||||
#else
|
||||
Serial.printf("Connecting to Wifi Network: %s ", ssid);
|
||||
#if defined(ST_IP)
|
||||
#if !defined (ST_GATEWAY) || !defined (ST_NETMASK)
|
||||
#error "You must supply both Gateway and NetMask when specifying a static IP address"
|
||||
#endif
|
||||
IPAddress staticIP(ST_IP);
|
||||
IPAddress gateway(ST_GATEWAY);
|
||||
IPAddress subnet(ST_NETMASK);
|
||||
#if !defined(ST_DNS1)
|
||||
WiFi.config(staticIP, gateway, subnet);
|
||||
#else
|
||||
IPAddress dns1(ST_DNS1);
|
||||
#if !defined(ST_DNS2)
|
||||
WiFi.config(staticIP, gateway, subnet, dns1);
|
||||
#else
|
||||
IPAddress dns2(ST_DNS2);
|
||||
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Initiate network connection request
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// Wait to connect, or timeout
|
||||
unsigned long start = millis();
|
||||
while ((millis() - start <= WIFI_WATCHDOG) && (WiFi.status() != WL_CONNECTED)) {
|
||||
delay(WIFI_WATCHDOG / 10);
|
||||
Serial.print('.');
|
||||
}
|
||||
|
||||
// If we have connected, show details
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println(" Succeeded");
|
||||
} else {
|
||||
Serial.println(" Failed");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
@ -263,7 +337,6 @@ void setup() {
|
||||
//s->set_dcw(s, 1); // 0 = disable , 1 = enable
|
||||
//s->set_colorbar(s, 0); // 0 = disable , 1 = enable
|
||||
|
||||
|
||||
// We now have our config defined; setup the hardware.
|
||||
|
||||
// Initialise and set the lamp
|
||||
@ -275,88 +348,27 @@ void setup() {
|
||||
Serial.println("No lamp, or lamp disabled in config");
|
||||
}
|
||||
|
||||
// Feedback that we are now attempting to connect
|
||||
Serial.println();
|
||||
Serial.println("Wifi Initialisation");
|
||||
flashLED(400);
|
||||
delay(100);
|
||||
|
||||
// We need a working Wifi before we can start the http handlers
|
||||
Serial.println("Starting WiFi");
|
||||
#if defined(WIFI_AP_ENABLE)
|
||||
#if defined(AP_ADDRESS)
|
||||
IPAddress local_IP(AP_ADDRESS);
|
||||
IPAddress gateway(AP_ADDRESS);
|
||||
IPAddress subnet(255,255,255,0);
|
||||
WiFi.softAPConfig(local_IP, gateway, subnet);
|
||||
#endif
|
||||
#if defined(AP_CHAN)
|
||||
WiFi.softAP(ssid, password, AP_CHAN);
|
||||
Serial.println("Setting up Fixed Channel AccessPoint");
|
||||
Serial.print("SSID : ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("Password : ");
|
||||
Serial.println(password);
|
||||
Serial.print("Channel : ");
|
||||
Serial.println(AP_CHAN);
|
||||
# else
|
||||
WiFi.softAP(ssid, password);
|
||||
Serial.println("Setting up AccessPoint");
|
||||
Serial.print("SSID : ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("Password : ");
|
||||
Serial.println(password);
|
||||
#endif
|
||||
WifiSetup();
|
||||
#else
|
||||
Serial.print("Connecting to Wifi Network: ");
|
||||
Serial.println(ssid);
|
||||
#if defined(ST_IP)
|
||||
#if !defined (ST_GATEWAY) || !defined (ST_NETMASK)
|
||||
#error "You must supply both Gateway and NetMask when specifying a static IP address"
|
||||
#endif
|
||||
IPAddress staticIP(ST_IP);
|
||||
IPAddress gateway(ST_GATEWAY);
|
||||
IPAddress subnet(ST_NETMASK);
|
||||
#if !defined(ST_DNS1)
|
||||
WiFi.config(staticIP, gateway, subnet);
|
||||
#else
|
||||
IPAddress dns1(ST_DNS1);
|
||||
#if !defined(ST_DNS2)
|
||||
WiFi.config(staticIP, gateway, subnet, dns1);
|
||||
#else
|
||||
IPAddress dns2(ST_DNS2);
|
||||
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(250); // Wait for Wifi to connect. If this fails wifi the code basically hangs here.
|
||||
// - It would be good to do something else here as a future enhancement.
|
||||
// (eg: go to a captive AP config portal to configure the wifi)
|
||||
WifiSetup();
|
||||
}
|
||||
|
||||
// feedback that we are connected
|
||||
Serial.println("WiFi connected");
|
||||
flashLED(200);
|
||||
delay(100);
|
||||
flashLED(200);
|
||||
delay(100);
|
||||
flashLED(200);
|
||||
#endif
|
||||
|
||||
// Start the Stream server, and the handler processes for the Web UI.
|
||||
// Start the two http handlers for the HTTP UI and Stream.
|
||||
startCameraServer(httpPort, streamPort);
|
||||
|
||||
// find our IP address
|
||||
IPAddress ip;
|
||||
char httpURL[64] = {"Unknown"};
|
||||
|
||||
#if defined(WIFI_AP_ENABLE)
|
||||
ip = WiFi.softAPIP();
|
||||
#else
|
||||
ip = WiFi.localIP();
|
||||
#endif
|
||||
|
||||
// Construct the App URL
|
||||
if (httpPort != 80) {
|
||||
sprintf(httpURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], httpPort);
|
||||
@ -365,15 +377,38 @@ void setup() {
|
||||
}
|
||||
// Construct the Stream URL
|
||||
sprintf(streamURL, "http://%d.%d.%d.%d:%d/", ip[0], ip[1], ip[2], ip[3], streamPort);
|
||||
|
||||
// Inform the user
|
||||
Serial.printf("\nCamera Ready!\nUse '%s' to connect\n", httpURL);
|
||||
Serial.printf("Raw stream URL is '%s'\n", streamURL);
|
||||
Serial.printf("Stream viewer available at '%sview'", streamURL);
|
||||
Serial.printf("Stream viewer available at '%sview'\n", streamURL);
|
||||
// Burst flash the LED to show we are connected
|
||||
for (int i = 0; i < 5; i++) {
|
||||
flashLED(80);
|
||||
delay(120);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Just loop forever.
|
||||
// Just loop forever, reconnecting Wifi As necesscary.
|
||||
// The stream and URI handler processes initiated by the startCameraServer() call at the
|
||||
// end of setup() will handle the camera and UI processing from now on.
|
||||
delay(10000);
|
||||
#if defined(WIFI_AP_ENABLE)
|
||||
delay(WIFI_WATCHDOG);
|
||||
#else
|
||||
static bool warned = false;
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
// We are connected, wait a bit and re-check
|
||||
if (warned) {
|
||||
Serial.println("WiFi reconnected");
|
||||
warned = false;
|
||||
}
|
||||
delay(WIFI_WATCHDOG);
|
||||
} else {
|
||||
if (!warned) {
|
||||
Serial.println("WiFi disconnected, retrying");
|
||||
warned = true;
|
||||
}
|
||||
WifiSetup();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
@ -40,6 +40,11 @@ const char* password = "my-password";
|
||||
// #define ST_DNS1 192,168,0,2
|
||||
// #define ST_DNS2 8,8,8,8
|
||||
|
||||
// Wifi Watchdog defines how long we spend waiting for a connection before retrying,
|
||||
// and how often we check to see if we are still connected, milliseconds
|
||||
// You may wish to increase this if your WiFi is slow at conencting,
|
||||
// #define WIFI_WATCHDOG = 5000
|
||||
|
||||
|
||||
/*
|
||||
* Port numbers for WebUI and Stream, defaults to 80 and 81.
|
||||
|
Reference in New Issue
Block a user