move wifi stuff to separate module, implement WifiManager for AP configuration
This commit is contained in:
@@ -2,21 +2,18 @@
|
||||
* Libraries and global scope code
|
||||
*/
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <time.h>
|
||||
|
||||
BearSSL::WiFiClientSecure net;
|
||||
time_t now;
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
String devname; // Device identifier for MQTT
|
||||
char nodename[80] = "UNDEF";
|
||||
|
||||
struct Settings {
|
||||
String name = "";
|
||||
String ip = "";
|
||||
} settings;
|
||||
|
||||
time_t now;
|
||||
|
||||
/*
|
||||
* Debugging might be nice sometimes
|
||||
*/
|
||||
|
||||
@@ -1,11 +1,60 @@
|
||||
// Future use
|
||||
/*
|
||||
* Wifi and OTA related stuff
|
||||
*/
|
||||
|
||||
wifi_setup() {
|
||||
|
||||
#include <time.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <WiFiManager.h>
|
||||
#include <ESP8266httpUpdate.h>
|
||||
|
||||
WiFiManager WifiManager;
|
||||
WiFiClientSecure net;
|
||||
|
||||
//BearSSL::WiFiClientSecure net;
|
||||
|
||||
//
|
||||
// If the WifiManager configuration portal is called, we can do stuff
|
||||
//
|
||||
//void WifiManagerCallback(WiFiManager *myWiFiManager) {
|
||||
// PRINTLN_SERIAL("Entered AP config mode");
|
||||
//}
|
||||
|
||||
void wifi_associate() {
|
||||
WifiManager.setConfigPortalTimeout(300); // If no configuration is done in 5 mins, exit/restart quietly
|
||||
if (WifiManager.autoConnect(nodename)) { // @@@FIXME@@@ we should really set a PSK for the AP (but in my brief testing it crashed the ESP, so what's up with that)
|
||||
PRINTLN_SERIAL("Wifi connected");
|
||||
|
||||
//wificlient.setFingerprint(WTR_SHA1);
|
||||
net.setInsecure(); // Do not check fingerprint
|
||||
net.setTimeout(15000); // 15 Seconds
|
||||
|
||||
PRINT_SERIAL("Setting time using SNTP");
|
||||
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
now = time(nullptr);
|
||||
while (now < 1510592825) {
|
||||
delay(500);
|
||||
APPEND_SERIAL(".");
|
||||
now = time(nullptr);
|
||||
}
|
||||
APPENDLN_SERIAL(" done!");
|
||||
struct tm timeinfo;
|
||||
gmtime_r(&now, &timeinfo);
|
||||
PRINT_SERIAL("Current time: ");
|
||||
APPENDLN_SERIAL(asctime(&timeinfo));
|
||||
}
|
||||
}
|
||||
|
||||
wifi_loop() {
|
||||
|
||||
void wifi_setup() {
|
||||
// WifiManager.setAPCallback(WifiManagerCallback); // Enable WifiManager callback to clear the cache
|
||||
wifi_associate();
|
||||
}
|
||||
|
||||
void wifi_loop() {
|
||||
if(WiFi.status() != WL_CONNECTED) {
|
||||
PRINTLN_SERIAL("Wifi disconnected, trying to reconnect");
|
||||
wifi_associate();
|
||||
}
|
||||
}
|
||||
/*
|
||||
// Check for firmware upgrades
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#ifdef ENABLE_MQTT
|
||||
|
||||
|
||||
// sample schema's to use: https://www.home-assistant.io/integrations/light.mqtt/
|
||||
|
||||
/*
|
||||
* Define a function newcmd in your module to be able to accept mqtt commands
|
||||
* Syntax and parameters are like this:
|
||||
@@ -72,34 +75,7 @@ void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
||||
}
|
||||
}
|
||||
|
||||
void WifiMQTTconnect() {
|
||||
|
||||
if(WiFi.status() != WL_CONNECTED) {
|
||||
PRINTLN_SERIAL("Wifi not connected, connecting...");
|
||||
WiFi.mode(WIFI_STA); // Weird MQTT connection bug, https://github.com/knolleary/pubsubclient/issues/138#issuecomment-326113915
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
PRINTLN_SERIAL("Waiting for Wifi...");
|
||||
delay(500);
|
||||
}
|
||||
settings.ip = WiFi.localIP().toString();
|
||||
PRINTLN_SERIAL("Wifi connected");
|
||||
}
|
||||
|
||||
PRINT_SERIAL("Setting time using SNTP");
|
||||
configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
now = time(nullptr);
|
||||
while (now < 1510592825) {
|
||||
delay(500);
|
||||
APPEND_SERIAL(".");
|
||||
now = time(nullptr);
|
||||
}
|
||||
APPENDLN_SERIAL(" done!");
|
||||
struct tm timeinfo;
|
||||
gmtime_r(&now, &timeinfo);
|
||||
PRINT_SERIAL("Current time: ");
|
||||
APPENDLN_SERIAL(asctime(&timeinfo));
|
||||
net.setInsecure(); // verification options set to none
|
||||
void mqtt_connect() {
|
||||
mqtt.setServer(MQTT_SERVER, MQTT_SERVERPORT);
|
||||
mqtt.setCallback(mqtt_callback);
|
||||
|
||||
@@ -141,7 +117,7 @@ void mqtt_loop() {
|
||||
|
||||
// Check for MQTT instructions
|
||||
if(!mqtt.connected()) {
|
||||
WifiMQTTconnect();
|
||||
mqtt_connect();
|
||||
} else {
|
||||
mqtt.loop();
|
||||
|
||||
|
||||
@@ -24,7 +24,8 @@ void setup() {
|
||||
settings.name.remove(devlen+1, 6);
|
||||
|
||||
PRINTLN_SERIAL("device name generated");
|
||||
|
||||
|
||||
wifi_setup();
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
mqtt_setup();
|
||||
@@ -40,6 +41,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
wifi_loop();
|
||||
#ifdef ENABLE_MQTT
|
||||
mqtt_loop();
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user