From fdf26d1669a15c44f829531c2d3f03cbc9f721f1 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 20 Nov 2019 22:43:40 +0100 Subject: [PATCH] main: more sophisticated WiFi setup - Try to find an AP 10 times, then set up our own - When connected to an AP and the connection is lost, restart the WiFi setup - Initialize the LEDs before the WiFi, so we are able to (maybe?) display messages on the LEDs --- src/main.cpp | 96 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 76c690f..7e74986 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "WLAN_Logins.h" #include "HTTPServer.h" @@ -25,7 +26,7 @@ bool led_on = false; size_t led_idx; size_t frame; -WiFiMulti WiFiMulti; +WiFiMulti wiFiMulti; Fader ledFader(NUM_STRIPS, NUM_LEDS); UDPProto udpProto(&ledFader); @@ -86,7 +87,9 @@ static void ledTask( void * parameter ) uint32_t start_time = micros(); - udpProto.loop(); + if(WiFi.status() == WL_CONNECTED) { + udpProto.loop(); + } led_idx++; if(led_idx >= STRANDS[0].numPixels) { @@ -134,6 +137,40 @@ static void ledTask( void * parameter ) vTaskDelete( NULL ); } +void wifi_setup(void) +{ + Serial.println("Trying to connect..."); + + for(size_t tries = 0; tries < 10; tries++) + { + if(wiFiMulti.run() == WL_CONNECTED) { + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + break; + } + + led_on = !led_on; + digitalWrite(2, led_on); + delay(100); + + Serial.print("."); + } + + if(WiFi.status() != WL_CONNECTED) { + Serial.println("Connection failed, setting up access point..."); + + IPAddress apIP(192, 168, 42, 1); + + WiFi.mode(WIFI_AP); + WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); + WiFi.softAP("๐Ÿ•ฏ๏ธ๐Ÿ’กโ˜€๏ธ", "Licht234"); + WiFi.enableAP(true); + } + +} + void setup() { pinMode(2, OUTPUT); // On-Board LED @@ -148,38 +185,18 @@ void setup() } } - // We start by connecting to a WiFi network + if(!SPIFFS.begin()) { + Serial.println("SPIFFS setup failed!"); + while(true) { + delay(100); + } + } digitalWrite(2, HIGH); Serial.println(); Serial.println(); - for (auto &net : NETWORKS) - { - Serial.print("Adding network "); - Serial.println(net.ssid); - WiFiMulti.addAP(net.ssid, net.password); - } - - while (WiFiMulti.run() != WL_CONNECTED) - { - led_on = !led_on; - digitalWrite(2, led_on); - delay(250); - - Serial.print("."); - } - - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - - HTTPServer::instance().setFader(&ledFader); - - udpProto.start(2703); - ledFader.set_color(Fader::Color{64,0,0,0}); ledFader.fade_color(Fader::Color{0,64,0,0}); @@ -191,6 +208,21 @@ void setup() 2, /* priority of the task */ NULL); /* Task handle to keep track of created task */ + // Connect the WiFi network (or start an AP if that doesn't work) + for (auto &net : NETWORKS) + { + Serial.print("Adding network "); + Serial.println(net.ssid); + wiFiMulti.addAP(net.ssid, net.password); + } + + wifi_setup(); + + // start the UDP server + udpProto.start(2703); + + // start the web server + HTTPServer::instance().setFader(&ledFader); HTTPServer::instance().start(); } @@ -198,8 +230,10 @@ void loop() { led_on = !led_on; digitalWrite(2, led_on); delay(500); - //Serial.print("RSSI: "); - //Serial.println(WiFi.RSSI()); - //server.loop(); + // reconnect WiFi when connection is lost + if(WiFi.status() == WL_CONNECTION_LOST) { + Serial.println("WLAN disconnected. Restarting setup."); + wifi_setup(); + } } \ No newline at end of file