From 5ce3b0c1b357dd3bc263ba5f210d7b52d89706c3 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 14 Jun 2019 05:35:17 +0200 Subject: [PATCH] Centralized the creation of a hostname to a global variable hostname, set in pitrix.cpp. Added an mDNS record for the webserver. --- include/prototypes.h | 1 + src/http_server.cpp | 4 ++++ src/mqtt.cpp | 14 ++------------ src/ota.cpp | 16 +++------------- src/pitrix.cpp | 10 ++++++++++ 5 files changed, 20 insertions(+), 25 deletions(-) diff --git a/include/prototypes.h b/include/prototypes.h index 6a135dd..decd621 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -40,3 +40,4 @@ typedef struct { } Coords; extern uint8_t baseHue; +extern char hostname[30]; diff --git a/src/http_server.cpp b/src/http_server.cpp index b1078dd..ac1be4b 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -4,6 +4,8 @@ #include "http_server.h" #include "effects.h" +#include "my_wifi.h" +#include "prototypes.h" #if defined( ESP8266 ) ESP8266WebServer http_server(HTTP_SERVER_PORT); @@ -71,6 +73,8 @@ void http_server_setup() { http_server.send_P(400, text_plain, PSTR("Unknown effect.")); }); http_server.begin(); + + MDNS.addService("_http", "_tcp", 80); } void http_server_loop() { diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 0c2f5f7..9856693 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -87,18 +87,8 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { } boolean mqtt_connect() { - char client_id[30]; - int chipid; - #if defined( ESP8266 ) - chipid = ESP.getChipId(); - #elif defined( ESP32 ) - chipid = ESP.getEfuseMac() & 0xFFFFFF; - #else - #error Neither ESP32 nor ESP8266 set. - #endif - snprintf(client_id, 30, HOSTNAME, chipid); - LOG("MQTT * Connecting to MQTT server with client id "); LOGln(client_id); - if (mqtt_client.connect(client_id, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) { + LOG("MQTT * Connecting to MQTT server with client id "); LOGln(hostname); + if (mqtt_client.connect(hostname, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) { LOGln("MQTT * Connected."); mqtt_client.publish(MQTT_TOPIC "status", "ONLINE"); mqtt_client.subscribe(MQTT_TOPIC "+"); diff --git a/src/ota.cpp b/src/ota.cpp index 15673bc..267d45a 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -1,9 +1,9 @@ #include #include "my_wifi.h" - #include #include "config.h" +#include "prototypes.h" void ota_setup() { ArduinoOTA.onStart([]() { @@ -30,19 +30,9 @@ void ota_setup() { else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); - char client_id[30]; - int chipid; - #if defined( ESP8266 ) - chipid = ESP.getChipId(); - #elif defined( ESP32 ) - chipid = ESP.getEfuseMac() & 0xFFFFFF; - #else - #error Neither ESP32 nor ESP8266 set. - #endif - snprintf(client_id, 30, HOSTNAME, chipid); - LOG("OTA * Starting OTA with client_id "); LOGln(client_id); - ArduinoOTA.setHostname(client_id); + LOG("OTA * Starting OTA with client_id "); LOGln(hostname); + ArduinoOTA.setHostname(hostname); ArduinoOTA.begin(); } diff --git a/src/pitrix.cpp b/src/pitrix.cpp index caeb076..0f629c5 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -17,10 +17,20 @@ uint8_t starting_up = OTA_STARTUP_DELAY; int loop_timeouts = 0; long loop_started_at = 0; uint8_t baseHue = 0; // defined as extern in prototypes.h +char hostname[30]; // defined as extern in prototypes.h void setup() { Serial.begin(74880); LOGln("Core * Starting"); + + int chipid; + #if defined( ESP8266 ) + chipid = ESP.getChipId(); + #elif defined( ESP32 ) + chipid = ESP.getEfuseMac() & 0xFFFFFF; + #endif + snprintf(hostname, 30, HOSTNAME, chipid); + setup_effects(); wifi_setup(); ntp_setup();