From 88b476db51211eb38807ab9b33676afcabd5ab65 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Mon, 7 Aug 2017 23:05:07 +0200 Subject: [PATCH] Handle empty mqtt_host, add /start_config_ap --- Arduino/McLighting/McLighting.ino | 37 ++++++++++++++++++++----------- Arduino/McLighting/definitions.h | 3 +-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 0b877de..53b5cc2 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -219,7 +219,7 @@ void setup() { //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration - if (!wifiManager.autoConnect()) { + if (!wifiManager.autoConnect(HOSTNAME)) { DBG_OUTPUT_PORT.println("failed to connect and hit timeout"); //reset and try again, or maybe put it to deep sleep ESP.reset(); @@ -241,7 +241,7 @@ void setup() { writeEEPROM(64, 6, mqtt_port); // 64-69 writeEEPROM(70, 32, mqtt_user); // 70-101 writeEEPROM(102, 32, mqtt_pass); // 102-133 - writeEEPROM(134, 1, "1"); // 134 --> alwasy "1" + writeEEPROM(134, 1, "1"); // 134 --> always "1" EEPROM.commit(); } #endif @@ -299,13 +299,15 @@ void setup() { // Configure MQTT // *************************************************************************** #ifdef ENABLE_MQTT - String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 3); - String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 4); - - DBG_OUTPUT_PORT.printf("Connect %s %d\n", mqtt_host, String(mqtt_port).toInt()); + if (mqtt_host != "" && String(mqtt_port).toInt() > 0) { + String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, strlen(HOSTNAME) + 4); + String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, strlen(HOSTNAME) + 5); - mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt()); - mqtt_client.setCallback(mqtt_callback); + DBG_OUTPUT_PORT.printf("Connect %s %d\n", mqtt_host, String(mqtt_port).toInt()); + + mqtt_client.setServer(mqtt_host, String(mqtt_port).toInt()); + mqtt_client.setCallback(mqtt_callback); + } #endif @@ -389,19 +391,26 @@ void setup() { server.on("/upload", handleMinimalUpload); server.on("/restart", []() { - DBG_OUTPUT_PORT.printf("/restart:\n"); + DBG_OUTPUT_PORT.printf("/restart\n"); server.send(200, "text/plain", "restarting..." ); ESP.restart(); }); server.on("/reset_wlan", []() { - DBG_OUTPUT_PORT.printf("/reset_wlan:\n"); + DBG_OUTPUT_PORT.printf("/reset_wlan\n"); server.send(200, "text/plain", "Resetting WLAN and restarting..." ); WiFiManager wifiManager; wifiManager.resetSettings(); ESP.restart(); }); + server.on("/start_config_ap", []() { + DBG_OUTPUT_PORT.printf("/start_config_ap\n"); + server.send(200, "text/plain", "Starting config AP ..." ); + WiFiManager wifiManager; + wifiManager.startConfigPortal(HOSTNAME); + }); + // *************************************************************************** // Setup: SPIFFS Webserver handler @@ -528,10 +537,12 @@ void loop() { #endif #ifdef ENABLE_MQTT - if (!mqtt_client.connected()) { - mqtt_reconnect(); + if (mqtt_host != "" && String(mqtt_port).toInt() > 0) { + if (!mqtt_client.connected()) { + mqtt_reconnect(); + } + mqtt_client.loop(); } - mqtt_client.loop(); #endif // Simple statemachine that handles the different modes diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 97c6cdd..93311ff 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -2,8 +2,7 @@ #define PIN 5 // PIN where neopixel / WS2811 strip is attached #define NUMLEDS 24 // Number of leds in the strip - -const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname +const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname #define ENABLE_OTA // If defined, enable Arduino OTA code.