mirror of
https://github.com/schinken/esp8266-geigercounter.git
synced 2024-11-24 17:40:21 +01:00
Refactor MQTT connect
This commit is contained in:
parent
b9c3cf0d6c
commit
9ee13ff425
1 changed files with 9 additions and 13 deletions
|
@ -18,9 +18,12 @@ float lastuSv = 0, currentuSv = 0;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
WiFi.hostname("GeigerCounter");
|
WiFi.hostname("ESP-GeigerCounter");
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
|
mqttClient.setClient(wifiClient);
|
||||||
|
mqttClient.setServer(mqttHost, 1883);
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
geigerCounterSerial.begin(BAUD_GEIGERCOUNTER);
|
||||||
|
|
||||||
|
@ -69,19 +72,12 @@ void updateRadiationValues() {
|
||||||
|
|
||||||
void connectMqtt() {
|
void connectMqtt() {
|
||||||
|
|
||||||
bool newConnection = false;
|
|
||||||
|
|
||||||
while (!mqttClient.connected()) {
|
while (!mqttClient.connected()) {
|
||||||
mqttClient.setClient(wifiClient);
|
if (mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected")) {
|
||||||
mqttClient.setServer(mqttHost, 1883);
|
|
||||||
mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected");
|
|
||||||
|
|
||||||
delay(1000);
|
|
||||||
newConnection = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newConnection) {
|
|
||||||
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
|
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
|
||||||
|
} else {
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue