-
Notifications
You must be signed in to change notification settings - Fork 18
/
mqtt.ino
200 lines (176 loc) · 6.36 KB
/
mqtt.ino
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//mqtt data send
WiFiClient espClient;
PubSubClient client(espClient);
//=======================================================================
// SendDataMQTT: send MQTT data to broker with 'retain' flag set to TRUE
//=======================================================================
void SendDataMQTT (struct sensorData *environment)
{
char bufferTempF[5];
char bufferTempC[5];
char bufferRain[10];
char bufferRain24[10];
int hourPtr = timeinfo.tm_hour;
client.setServer(mqttServer, mqttPort);
//client.setCallback(callback);
while (!client.connected()) {
MonPrintf("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword))
{
Serial.println("connected");
}
else
{
Serial.print("failed with state ");
Serial.print(client.state());
delay(1000);
}
}
MQTTPublish("boot/", (int)bootCount, true);
MQTTPublish("rssi/", rssi, true);
MQTTPublish("temperatureF/", (int)environment->temperatureF, true);
MQTTPublish("temperatureC/", (int)environment->temperatureC, true);
MQTTPublish("windSpeed/", environment->windSpeed, true);
MQTTPublish("windDirection/", (int)environment->windDirection, true);
MQTTPublish("windCardinalDirection/", environment->windCardinalDirection, true);
MQTTPublish("photoresistor/", (int)environment->photoresistor, true);
#ifndef METRIC
MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011), true);
MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011), true);
MQTTPublish("rainfall24/", (float) (last24() * 0.011), true);
#else
MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011 * 25.4), true);
MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4), true);
MQTTPublish("rainfall24/", (float) (last24() * 0.011 * 25.4), true);
#endif
MQTTPublish("batteryVoltage/", environment->batteryVoltage, true);
MQTTPublish("lux/", environment->lux, true);
MQTTPublish("UVIndex/", environment->UVIndex, true);
MQTTPublish("relHum/", environment->humidity, true);
MQTTPublish("pressure/", environment->barometricPressure, true);
MQTTPublish("caseTemperature/", environment->BMEtemperature, true);
MQTTPublish("batteryADC/", (int)environment->batteryADC, true);
MQTTPublish("ESPcoreF/", (int)environment->coreF, true);
MQTTPublish("ESPcoreC/", (int)environment->coreC, true);
MQTTPublish("timeEnabled/", (int)elapsedTime, true);
MQTTPublish("lowBattery/", lowBattery, true);
MonPrintf("Issuing mqtt disconnect\n");
client.disconnect();
MonPrintf("Disconnected\n");
}
//=======================================================================
// MQTTPublishString: routine to publish string
//=======================================================================
void MQTTPublish(const char topic[], char *value, bool retain)
{
char topicBuffer[256];
char payload[256];
strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
sprintf(payload, "%s", value);
MQTTSend(topicBuffer, payload, retain);
}
//=======================================================================
// MQTTPublishInt: routine to publish int values as strings
//=======================================================================
void MQTTPublish(const char topic[], int value, bool retain)
{
char topicBuffer[256];
char payload[256];
strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
sprintf(payload, "%i", value);
MQTTSend(topicBuffer, payload, retain);
}
//=======================================================================
// MQTTPublish Long: routine to publish int values as strings
//=======================================================================
void MQTTPublish(const char topic[], long value, bool retain)
{
char topicBuffer[256];
char payload[256];
strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
sprintf(payload, "%li", value);
MQTTSend(topicBuffer, payload, retain);
}
//=======================================================================
// MQTTPublishFloat: routine to publish float values as strings
//=======================================================================
void MQTTPublish(const char topic[], float value, bool retain)
{
char topicBuffer[256];
char payload[256];
strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
sprintf(payload, "%6.3f", value);
MQTTSend(topicBuffer, payload, retain);
}
//=======================================================================
// MQTTPublishBool: routine to publish bool values as strings
//=======================================================================
void MQTTPublish(const char topic[], bool value, bool retain)
{
char topicBuffer[256];
char payload[256];
strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
if (value)
{
sprintf(payload, "true");
}
else
{
sprintf(payload, "false");
}
MQTTSend(topicBuffer, payload, retain);
}
//=======================================================================
// reconnect: MQTT reconnect
//=======================================================================
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP32Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//=======================================================================
// MQTTSend: MQTT send topic with value to broker
//=======================================================================
void MQTTSend(char *topicBuffer, char *payload, bool retain)
{
int status = 0;
int retryCount = 0;
#ifdef ExtendedMQTT
MonPrintf("%s: %s\n", topicBuffer, payload);
#endif
while (!status && retryCount < 5)
{
status = client.publish(topicBuffer, payload, retain);
#ifdef ExtendedMQTT
MonPrintf("MQTT status: %i\n", status);
#endif
delay(50);
retryCount++;
}
}