This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MQTTPublisher.cpp
171 lines (142 loc) · 4.14 KB
/
MQTTPublisher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "MQTTPublisher.h"
#include "Settings.h"
WiFiClient espClient;
PubSubClient client(espClient);
MQTTPublisher::MQTTPublisher(bool inDebugMode)
{
randomSeed(micros());
debugMode = inDebugMode;
}
MQTTPublisher::~MQTTPublisher()
{
client.publish("watermeter", "offline");
client.disconnect();
}
bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();
if (debugMode)
{
Serial.print("MQTT) Attempting connection to server: ");
Serial.print(MQTT_HOST_NAME);
Serial.println("...");
}
// Create a random client ID
String clientId = "watermeter-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
Serial.println("MQTT) Connecting with credientials");
clientConnected = client.connect(clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
Serial.println("MQTT) Connecting without credentials.");
clientConnected = client.connect(clientId.c_str());
}
if (clientConnected)
{
if (debugMode) {
Serial.println("MQTT) connected");
}
// Once connected, publish an announcement...
client.publish("watermeter", "online");
return true;
}
else {
if (debugMode)
{
Serial.print("MQTT) failed, rc=");
Serial.print(client.state());
}
}
return false;
}
void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
Serial.println("MQTT) disabled. No hostname or port set.");
return; //not configured
}
if(debugMode){
Serial.println("MQTT) enabled. Connecting.");
}
client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect(); //connect right away
isStarted = true;
}
void MQTTPublisher::stop()
{
isStarted = false;
}
void MQTTPublisher::handle()
{
if (!isStarted)
return;
if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
if (!reconnect()) return;
}
//got a valid mqtt connection. Loop through the inverts and send out the data if needed
client.loop();
bool sendRegular = millis() - lastSentRegularUpdate > MQTT_REGULAR_UPDATE_INTERVAL;
bool sendQuick = millis() - lastSentQuickUpdate > MQTT_QUICK_UPDATE_INTERVAL;
bool sendOk = true; //if a mqtt message fails, wait for retransmit at a later time
if (sendRegular || sendQuick)
{
auto mqttTopic = MQTT_TOPIC;
if (sendQuick)
{
uint32_t currentTime = millis();
if (currentTime - lastSend > SEND_FREQUENCY ) {
lastSend = currentTime;
// send flow
if (flow != oldflow) {
oldflow = flow;
if(debugMode){
Serial.print("MQTT) l/min:");
Serial.println(flow);
}
if (flow < ((uint32_t)MAX_FLOW)) {
if (sendOk) sendOk = publishOnMQTT(mqttTopic, "/flow", String(flow, 4));
}
}
// No Pulse count received in 2min
if (currentTime - lastPulse > 120000) {
flow = 0;
}
// Pulse count has changed
if (pulseCount != oldPulseCount) {
oldPulseCount = pulseCount;
if(debugMode){
Serial.print("MQTT) pulsecount:");
Serial.println(pulseCount);
}
if (sendOk) sendOk = publishOnMQTT(mqttTopic, "/puls", String(pulseCount));
double volume = ((double)pulseCount / (double)PULSE_FACTOR);
if (volume != oldvolume) {
oldvolume = volume;
if(debugMode){
Serial.print("MQTT) volume:");
Serial.println(volume, 3);
}
if (sendOk) sendOk = publishOnMQTT(mqttTopic, "/vol", String(volume, 4));
}
}
}
}
client.loop();
}
if (sendQuick)
lastSentQuickUpdate = millis();
if (sendRegular)
lastSentRegularUpdate = millis();
}
bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}