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
This commit is contained in:
parent
fd0d75ee16
commit
fdf26d1669
94
src/main.cpp
94
src/main.cpp
|
@ -3,6 +3,7 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <WiFiMulti.h>
|
#include <WiFiMulti.h>
|
||||||
|
#include <SPIFFS.h>
|
||||||
|
|
||||||
#include "WLAN_Logins.h"
|
#include "WLAN_Logins.h"
|
||||||
#include "HTTPServer.h"
|
#include "HTTPServer.h"
|
||||||
|
@ -25,7 +26,7 @@ bool led_on = false;
|
||||||
size_t led_idx;
|
size_t led_idx;
|
||||||
size_t frame;
|
size_t frame;
|
||||||
|
|
||||||
WiFiMulti WiFiMulti;
|
WiFiMulti wiFiMulti;
|
||||||
|
|
||||||
Fader ledFader(NUM_STRIPS, NUM_LEDS);
|
Fader ledFader(NUM_STRIPS, NUM_LEDS);
|
||||||
UDPProto udpProto(&ledFader);
|
UDPProto udpProto(&ledFader);
|
||||||
|
@ -86,7 +87,9 @@ static void ledTask( void * parameter )
|
||||||
|
|
||||||
uint32_t start_time = micros();
|
uint32_t start_time = micros();
|
||||||
|
|
||||||
|
if(WiFi.status() == WL_CONNECTED) {
|
||||||
udpProto.loop();
|
udpProto.loop();
|
||||||
|
}
|
||||||
|
|
||||||
led_idx++;
|
led_idx++;
|
||||||
if(led_idx >= STRANDS[0].numPixels) {
|
if(led_idx >= STRANDS[0].numPixels) {
|
||||||
|
@ -134,6 +137,40 @@ static void ledTask( void * parameter )
|
||||||
vTaskDelete( NULL );
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(2, OUTPUT); // On-Board LED
|
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);
|
digitalWrite(2, HIGH);
|
||||||
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
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.set_color(Fader::Color{64,0,0,0});
|
||||||
ledFader.fade_color(Fader::Color{0,64,0,0});
|
ledFader.fade_color(Fader::Color{0,64,0,0});
|
||||||
|
|
||||||
|
@ -191,6 +208,21 @@ void setup()
|
||||||
2, /* priority of the task */
|
2, /* priority of the task */
|
||||||
NULL); /* Task handle to keep track of created 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();
|
HTTPServer::instance().start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,8 +230,10 @@ void loop() {
|
||||||
led_on = !led_on;
|
led_on = !led_on;
|
||||||
digitalWrite(2, led_on);
|
digitalWrite(2, led_on);
|
||||||
delay(500);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue