-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSamovarMqtt.h
97 lines (81 loc) · 2.53 KB
/
SamovarMqtt.h
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
#ifndef __SAMOVAR_MQTT_H_
#define __SAMOVAR_MQTT_H_
#include <AsyncMqttClient.h>
#include "Samovar.h"
#define PAYLOADSIZE 800
AsyncMqttClient mqttClient;
char mqttstr[100] = "SMV/\0";
char mqttstr1[100];
static const char mqttUser[] = "samovar";
static const char mqttPassword[] = "samovar-tool.ru";
void SendMsg(const String& m, MESSAGE_TYPE msg_type);
void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttPublish(uint16_t packetId);
void connectToMqtt();
void initMqtt() {
char buf[10];
itoa(chipId, buf, 10);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onPublish(onMqttPublish);
mqttClient.setClientId(buf);
//mqttClient.setClientId("1234");
mqttClient.setMaxTopicLength(PAYLOADSIZE);
mqttClient.setCredentials(mqttUser, mqttPassword);
mqttClient.setServer("ora1.samovar-tool.ru", 1883);
strcat(mqttstr, SamSetup.blynkauth);
strcat(mqttstr, "/");
if (strlen(SamSetup.blynkauth) < 30) send_mqtt = false; else send_mqtt = true;
// mqttClient.onSubscribe(onMqttSubscribe);
// mqttClient.onUnsubscribe(onMqttUnsubscribe);
// mqttClient.onMessage(onMqttMessage);
connectToMqtt();
}
void connectToMqtt() {
// Serial.println("Connecting to MQTT...");
// uint8_t i;
// i = 0;
mqttClient.connect();
// while (!mqttClient.connected()){
// delay(50);
// i++;
// if (i > 25) break;
// }
}
void onMqttConnect(bool sessionPresent) {
#ifdef __SAMOVAR_DEBUG
String s = "mqttClient.connected";
SendMsg(s, NOTIFY_MSG);
WriteConsoleLog(s);
#endif
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
#ifdef __SAMOVAR_DEBUG
String s = "Disconnected from MQTT: " + String(static_cast<std::underlying_type<AsyncMqttClientDisconnectReason>::type>(reason));
SendMsg(s, ALARM_MSG);
WriteConsoleLog(s);
#endif
}
void onMqttPublish(uint16_t packetId) {
// Serial.println("Publish acknowledged.");
// Serial.print(" packetId: ");
// Serial.println(packetId);
}
void MqttSendMsg(const String &Str, const char *chart ) {
if (!send_mqtt) return;
strcpy(mqttstr1, mqttstr);
strcat(mqttstr1, chart);
static char payload[PAYLOADSIZE];
//Версия сообщения
strcat(mqttstr1, "/3");
Str.toCharArray(payload, PAYLOADSIZE);
uint16_t packetIdPub1 = mqttClient.publish(mqttstr1, 2, true, payload);
if (packetIdPub1 == 0) {
if (!mqttClient.connected()){
mqttClient.connect();
}
packetIdPub1 = mqttClient.publish(mqttstr1, 2, true, payload);
}
}
#endif