Browse code

fucj this

root authored on28/07/2022 12:41:35
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,66 @@
1
+void sendMQTTMessage(const char *topic,const char *payload)
2
+{
3
+    bool result = mqttClient.publish(topic, payload, false);
4
+}
5
+
6
+bool mqttReconnect()
7
+{
8
+    int MQTT_RECONNECT_RETRIES = 0;
9
+
10
+    while (!mqttClient.connected() && MQTT_RECONNECT_RETRIES < MQTT_MAX_RECONNECT_TRIES)
11
+    {
12
+        MQTT_RECONNECT_RETRIES++;
13
+
14
+        if (mqttClient.connect(HOSTNAME, MQTT_USER, MQTT_PASS))
15
+        {
16
+            char *message = new char[16 + strlen(HOSTNAME) + 1];
17
+            strcpy(message, "p1 meter alive: ");
18
+            strcat(message, HOSTNAME);
19
+            mqttClient.publish("sensors/power/p1meter/status", message);
20
+        }
21
+        else
22
+        {
23
+            delay(5000);
24
+        }
25
+    }
26
+
27
+    if (MQTT_RECONNECT_RETRIES >= MQTT_MAX_RECONNECT_TRIES)
28
+    {
29
+        return false;
30
+    }
31
+
32
+    return true;
33
+}
34
+
35
+void sendMetric(String name, String metric)
36
+{
37
+    //if (metric > 0)
38
+    //{
39
+        //char output[10];
40
+        //ltoa(metric, output, sizeof(output));
41
+
42
+        String topic = String(MQTT_ROOT_TOPIC) + "/" + name;
43
+        sendMQTTMessage(topic.c_str(), metric.c_str());
44
+    //}
45
+}
46
+
47
+void sendDataToBroker()
48
+{
49
+    String Metric="";
50
+      for ( int i = 0; i < 7; i++)
51
+      {
52
+        if ( i != 4)
53
+        {
54
+        char output[10];
55
+        ltoa(telegramObjects[i].value, output, sizeof(output));
56
+        String Str_Data(output);
57
+        ( i == 0 ) ? Metric = Str_Data : Metric = Metric + ";" + Str_Data;
58
+        }
59
+      }
60
+      
61
+      sendMetric("KWH_Metric", Metric );
62
+      char output[10];
63
+      ltoa(telegramObjects[4].value, output, sizeof(output));
64
+      String Str_Gas(output);
65
+      sendMetric("Gas_Metric", Str_Gas );
66
+}