move wifi stuff to separate module, implement WifiManager for AP configuration
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user