Skip to content

Commit

Permalink
MQTT own objects to handle connections
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunorman committed Aug 2, 2018
1 parent 6319d5c commit 58d999f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
16 changes: 0 additions & 16 deletions RadHelpers.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
#include <Arduino.h>
#include <PubSubClient.h>
#include <RadConfig.h>

String float_to_string(float value, int decimals) {
char c_value[10];
dtostrf(value, 1, decimals, c_value);
return c_value;
}


void mqtt_reconnect(PubSubClient &client, String dev_id, RadConfig config) {
Serial.println("[MQTT] Attempting MQTT connection...");
while (!client.connected()) {
if (client.connect(dev_id.c_str())) {
Serial.println("[MQTT] Connected");
}
else {
Serial.println("[MQTT] Failed, rc=" + String(client.state()));
delay(5000);
}
}
}
5 changes: 0 additions & 5 deletions RadHelpers.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#pragma once

#include <Arduino.h>
#include <PubSubClient.h>
#include <RadConfig.h>
#include <RadLED.h>

String float_to_string(float value, int decimals);

void mqtt_reconnect(PubSubClient &client, String dev_id, RadConfig config);
56 changes: 56 additions & 0 deletions RadMQTT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>

#include <RadConfig.h>
#include <RadMQTT.h>
#include <PubSubClient.h>
#include <RadLED.h>

WiFiClient wclient;

RadMQTT::RadMQTT(RadConfig config) {
_config = config;
client = new PubSubClient(config.mqtt_server, config.mqtt_port, wclient);
connect();
}

void RadMQTT::connect() {
if (WiFi.status() != WL_CONNECTED) {
// Do nothing here as the wifi isn't even connected.
return;
}

_config.log("[MQTT] Attempting MQTT connection...");
while (!client->connected()) {
if (client->connect(_config.device_id.c_str())) {
_config.log("[MQTT] Connected");
// Connected! 3 success flashes
delay(1000);
LED_flash(D4, 3, 500, 500);
delay(1000);
}
else {
_config.log("[MQTT] Failed, rc=" + String(client->state()));
LED_flash(D4, 10, 100, 100);
delay(5000);
}
}
}

/**
* I have found with the MQTT library you can't hammer it with a .loop()
* on every arduino loop otherwise it causes it to disconnect randomly and
* then try to reconnect again causing a ~5 second lock.
*
* Unsure if this is the mosquitto server, or the device.
*/
void RadMQTT::loop() {
if (!client->connected()) {
connect();
_last_poll = millis();
}
else if (millis() - _last_poll > 2000) {
client->loop();
_last_poll = millis();
}
}
17 changes: 17 additions & 0 deletions RadMQTT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <Arduino.h>
#include <RadConfig.h>
#include <PubSubClient.h>

class RadMQTT {
public:
RadMQTT (RadConfig config);
void connect();
void loop();

PubSubClient *client;
private:
RadConfig _config;
unsigned long _last_poll;
};

0 comments on commit 58d999f

Please sign in to comment.