Re-Apply patch from @NKnusperer for default hostname

This commit is contained in:
schinken 2022-03-16 16:17:06 +01:00
parent cff085ba59
commit 6b361a813b
2 changed files with 13 additions and 7 deletions

View file

@ -14,12 +14,12 @@ SoftwareSerial geigerCounterSerial(PIN_UART_RX, PIN_UART_TX);
uint8_t idx = 0; uint8_t idx = 0;
char buffer[64]; char buffer[64];
char hostname[24];
int lastCPM = 0, currentCPM = 0; int lastCPM = 0, currentCPM = 0;
float lastuSv = 0, currentuSv = 0; float lastuSv = 0, currentuSv = 0;
unsigned long lastPublishMs = 0; unsigned long lastPublishMs = 0;
void setup() { void setup() {
// power up wait // power up wait
delay(3000); delay(3000);
@ -28,7 +28,13 @@ void setup() {
geigerCounterSerial.begin(9600); geigerCounterSerial.begin(9600);
delay(10); delay(10);
WiFi.hostname(WIFI_HOSTNAME); #ifdef WIFI_HOSTNAME
strncpy(hostname, WIFI_HOSTNAME, sizeof(hostname));
#else
snprintf(hostname, sizeof(hostname), "ESP-GEIGER-%X", ESP.getChipId());
#endif
WiFi.hostname(hostname);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
@ -41,7 +47,7 @@ void setup() {
mqttClient.setServer(MQTT_HOST, 1883); mqttClient.setServer(MQTT_HOST, 1883);
#ifdef OTA_PASSWORD #ifdef OTA_PASSWORD
ArduinoOTA.setHostname(WIFI_HOSTNAME); ArduinoOTA.setHostname(hostname);
ArduinoOTA.setPassword(OTA_PASSWORD); ArduinoOTA.setPassword(OTA_PASSWORD);
ArduinoOTA.begin(); ArduinoOTA.begin();
#endif #endif
@ -68,7 +74,7 @@ void publishValues() {
void connectMqtt() { void connectMqtt() {
while (!mqttClient.connected()) { while (!mqttClient.connected()) {
if (mqttClient.connect(WIFI_HOSTNAME, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) { if (mqttClient.connect(hostname, MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) {
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true); mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
Serial.println("MQTT connected"); Serial.println("MQTT connected");
} else { } else {

View file

@ -11,9 +11,9 @@
#define BAUD_GEIGERCOUNTER 9600 #define BAUD_GEIGERCOUNTER 9600
const char* WIFI_HOSTNAME = "ESP-GeigerCounter"; //#define WIFI_HOSTNAME "ESP-GeigerCounter";
const char* WIFI_SSID = "xxxxxxxxxx"; #define WIFI_SSID "xxxx"
const char* WIFI_PASSWORD = "xxxxxxxxxxx"; #define WIFI_PASSWORD "xxxxx"
//#define OTA_PASSWORD "foobar" //#define OTA_PASSWORD "foobar"