Refactor MQTT connect

This commit is contained in:
schinken 2017-03-04 09:06:07 +01:00
parent b9c3cf0d6c
commit 9ee13ff425

View file

@ -18,8 +18,11 @@ 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.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
mqttClient.connect("geigercounter", MQTT_TOPIC_LAST_WILL, 1, true, "disconnected"); } else {
delay(1000);
delay(1000); }
newConnection = true;
}
if (newConnection) {
mqttClient.publish(MQTT_TOPIC_LAST_WILL, "connected", true);
} }
} }