forked from bartwo/esp32_p1meter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.ino
63 lines (55 loc) · 1.47 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
void sendMQTTMessage(const char *topic, char *payload)
{
bool result = mqttClient.publish(topic, payload, false);
}
bool mqttReconnect()
{
int MQTT_RECONNECT_RETRIES = 0;
while (!mqttClient.connected() && MQTT_RECONNECT_RETRIES < MQTT_MAX_RECONNECT_TRIES)
{
MQTT_RECONNECT_RETRIES++;
if (mqttClient.connect(HOSTNAME, MQTT_USER, MQTT_PASS))
{
char *message = new char[16 + strlen(HOSTNAME) + 1];
strcpy(message, "p1 meter alive: ");
strcat(message, HOSTNAME);
mqttClient.publish("hass/status", message);
}
else
{
delay(5000);
}
}
if (MQTT_RECONNECT_RETRIES >= MQTT_MAX_RECONNECT_TRIES)
{
return false;
}
return true;
}
void sendMetric(String name, long metric)
{
//if (metric > 0)
//{
char output[10];
ltoa(metric, output, sizeof(output));
String topic = String(MQTT_ROOT_TOPIC) + "/" + name;
#ifdef DEBUG
Serial.println(topic);
#endif
sendMQTTMessage(topic.c_str(), output);
//}
}
void sendDataToBroker()
{
for (int i = 0; i < NUMBER_OF_READOUTS; i++)
{
#ifdef DEBUG
Serial.println((String) "Sending: " + telegramObjects[i].name + " value: " + telegramObjects[i].value);
#endif
if (telegramObjects[i].sendData)
{
sendMetric(telegramObjects[i].name, telegramObjects[i].value);
telegramObjects[i].sendData = false;
}
}
}