Centralized the creation of a hostname to a global variable hostname, set in pitrix.cpp. Added an mDNS record for the webserver.

This commit is contained in:
Fabian Schlenz 2019-06-14 05:35:17 +02:00
parent 4fdd1d3739
commit 5ce3b0c1b3
5 changed files with 20 additions and 25 deletions

View File

@ -40,3 +40,4 @@ typedef struct {
} Coords;
extern uint8_t baseHue;
extern char hostname[30];

View File

@ -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() {

View File

@ -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 "+");

View File

@ -1,9 +1,9 @@
#include <ArduinoOTA.h>
#include "my_wifi.h"
#include <ArduinoOTA.h>
#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();
}

View File

@ -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();