-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MQTT own objects to handle connections
- Loading branch information
1 parent
6319d5c
commit 58d999f
Showing
4 changed files
with
73 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |