2020-02-10 14:29:39 +00:00
|
|
|
#include "wlan.h"
|
2020-02-11 15:31:52 +00:00
|
|
|
#include "settings.h"
|
2021-02-06 21:03:31 +00:00
|
|
|
#include <DNSServer.h>
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-01-08 20:48:35 +00:00
|
|
|
const char *deviceName = "paperdash-epd";
|
2020-02-11 19:21:30 +00:00
|
|
|
RTC_DATA_ATTR int wifiFailedCount = 0;
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-02-06 21:03:31 +00:00
|
|
|
// DNS server
|
|
|
|
const byte DNS_PORT = 53;
|
|
|
|
DNSServer dnsServer;
|
|
|
|
|
|
|
|
/* Soft AP network parameters */
|
|
|
|
IPAddress apIP(192,178,4,1);
|
|
|
|
|
|
|
|
|
|
|
|
bool initClientMode(const char *ssid, const char *password);
|
2020-02-11 15:31:52 +00:00
|
|
|
void initAPMode();
|
2020-02-10 14:29:39 +00:00
|
|
|
|
|
|
|
void setupWlan()
|
|
|
|
{
|
2020-02-10 22:14:54 +00:00
|
|
|
Serial.println("setup Wlan");
|
2020-02-11 19:21:30 +00:00
|
|
|
|
2020-02-11 15:31:52 +00:00
|
|
|
// load wifi settings
|
2020-05-20 10:52:07 +00:00
|
|
|
String ssid = NVS.getString("wifi.ssid");
|
|
|
|
String password = NVS.getString("wifi.password");
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-02-06 21:03:31 +00:00
|
|
|
bool clientMode = false;
|
2021-01-08 19:58:19 +00:00
|
|
|
if (!ssid.isEmpty() && !password.isEmpty() && wifiFailedCount <=3)
|
2020-02-11 15:31:52 +00:00
|
|
|
{
|
2021-02-06 21:03:31 +00:00
|
|
|
// try client mode
|
|
|
|
clientMode = initClientMode(ssid.c_str(), password.c_str());
|
2020-02-11 15:31:52 +00:00
|
|
|
}
|
2021-02-06 21:03:31 +00:00
|
|
|
|
|
|
|
if (!clientMode)
|
2020-02-11 15:31:52 +00:00
|
|
|
{
|
|
|
|
// ap mode
|
|
|
|
initAPMode();
|
|
|
|
}
|
2020-02-11 19:21:30 +00:00
|
|
|
|
2020-05-20 10:52:07 +00:00
|
|
|
WiFi.setHostname(deviceName);
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2020-05-20 10:52:07 +00:00
|
|
|
Serial.println("setup Wlan - done");
|
2020-02-20 20:59:59 +00:00
|
|
|
}
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-02-06 21:03:31 +00:00
|
|
|
|
|
|
|
void loopWlan()
|
|
|
|
{
|
|
|
|
// DNS
|
|
|
|
dnsServer.processNextRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initClientMode(const char *ssid, const char *password)
|
2020-02-11 15:31:52 +00:00
|
|
|
{
|
2020-05-20 10:52:07 +00:00
|
|
|
uint8_t tryCount = 5;
|
2020-02-10 22:14:54 +00:00
|
|
|
long startMills = millis();
|
|
|
|
|
2020-02-11 19:21:30 +00:00
|
|
|
Serial.print(" Connecting to: ");
|
2020-02-20 20:59:59 +00:00
|
|
|
Serial.print(ssid);
|
2020-02-22 14:33:27 +00:00
|
|
|
Serial.print(" ");
|
2020-02-10 22:14:54 +00:00
|
|
|
|
2020-02-11 19:21:30 +00:00
|
|
|
// connecting
|
|
|
|
WiFi.mode(WIFI_STA);
|
2020-02-20 20:59:59 +00:00
|
|
|
|
|
|
|
|
2020-02-10 22:14:54 +00:00
|
|
|
WiFi.begin(ssid, password);
|
2020-02-20 20:59:59 +00:00
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
delay(500);
|
2020-02-22 14:33:27 +00:00
|
|
|
Serial.print(".");
|
2020-05-20 10:52:07 +00:00
|
|
|
if (!tryCount--)
|
2020-02-20 20:59:59 +00:00
|
|
|
{
|
2021-01-08 19:58:19 +00:00
|
|
|
// TODO is this correct?
|
2021-02-06 21:03:31 +00:00
|
|
|
WiFi.disconnect();
|
2020-05-20 10:52:07 +00:00
|
|
|
wifiFailedCount++;
|
|
|
|
if (wifiFailedCount > 3) {
|
|
|
|
Serial.println(" wifi is not reachable...");
|
2021-02-06 21:03:31 +00:00
|
|
|
return false;
|
2020-05-20 10:52:07 +00:00
|
|
|
} else {
|
|
|
|
tryCount = 5;
|
|
|
|
Serial.println(" wifi reset...");
|
|
|
|
delay(500);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
}
|
2020-02-20 20:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println(" ...connected");
|
|
|
|
Serial.print(" IP address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
2020-02-10 22:14:54 +00:00
|
|
|
|
2020-05-20 10:52:07 +00:00
|
|
|
if (!MDNS.begin(deviceName)) {
|
|
|
|
Serial.println("Error setting up MDNS responder!");
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:49:33 +00:00
|
|
|
// Add service to MDNS-SD
|
|
|
|
MDNS.addService("http", "tcp", 80);
|
2020-11-13 17:52:21 +00:00
|
|
|
MDNS.addServiceTxt("http", "tcp", "paperdash", "epd");
|
2020-08-25 17:49:33 +00:00
|
|
|
|
2020-02-11 19:21:30 +00:00
|
|
|
Serial.print(" connected in: ");
|
2020-02-10 22:14:54 +00:00
|
|
|
Serial.println(millis() - startMills);
|
2021-02-06 21:03:31 +00:00
|
|
|
|
|
|
|
return true;
|
2020-02-10 14:29:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 15:31:52 +00:00
|
|
|
void initAPMode()
|
|
|
|
{
|
2020-02-11 19:21:30 +00:00
|
|
|
Serial.println(" init AP (Access Point)");
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-02-06 21:03:31 +00:00
|
|
|
WiFi.mode(WIFI_AP);
|
2021-02-06 10:43:27 +00:00
|
|
|
WiFi.softAP("paperdash");
|
2020-02-11 15:31:52 +00:00
|
|
|
|
2021-02-06 21:03:31 +00:00
|
|
|
// prevent esp from crashing
|
|
|
|
delay(2000);
|
|
|
|
|
|
|
|
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
|
|
|
|
|
|
|
|
// redirect all to local ap
|
|
|
|
dnsServer.start(DNS_PORT, "*", apIP);
|
|
|
|
|
2020-02-11 19:21:30 +00:00
|
|
|
IPAddress IP = WiFi.softAPIP();
|
2020-02-20 20:59:59 +00:00
|
|
|
Serial.print(" AP IP address: ");
|
|
|
|
Serial.println(IP);
|
2020-02-10 14:29:39 +00:00
|
|
|
}
|