-
Notifications
You must be signed in to change notification settings - Fork 1
/
MQTToverSerial.cpp
229 lines (195 loc) · 5.33 KB
/
MQTToverSerial.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "MQTToverSerial.h"
MQTToverSerial::MQTToverSerial(WiFiClient& _wifiClient, HardwareSerial &_serial, const char* _serverIp, int _serverPort, const char* _MQTTlogin, const char* _MQTTpass, const char* _MQTTid, HardwareSerial* _debugSerial) : wifiClient(_wifiClient), serial(_serial), serverIp(_serverIp), serverPort(_serverPort), MQTTlogin(_MQTTlogin), MQTTpass(_MQTTpass), MQTTid(_MQTTid), debugSerial(_debugSerial)
{
pubSubClient = new PubSubClient(wifiClient);
pubSubClient->setServer(serverIp, serverPort);
pubSubClient->setCallback( [&](char* topic, byte* payload, unsigned int length)
{
this->serial.print("MSSG");
this->serial.print(specialCharacter);
this->serial.print(topic);
this->serial.print(specialCharacter);
for (unsigned int j = 0; j < length; ++j)
{
this->serial.print((char) payload[j]);
}
this->serial.println("");
if (this->debugSerial != NULL)
{
this->debugSerial->print("MSSG");
this->debugSerial->print(specialCharacter);
this->debugSerial->print(topic);
this->debugSerial->print(specialCharacter);
for (unsigned int j = 0; j < length; ++j)
{
this->debugSerial->print((char) payload[j]);
}
this->debugSerial->println("");
}
});
pubSubClient->connect(MQTTid, MQTTlogin, MQTTpass);
}
MQTToverSerial::~MQTToverSerial()
{
if (pubSubClient != NULL)
delete pubSubClient;
}
bool MQTToverSerial::Publish(const char* topic, const char* message)
{
pubSubClient->publish(topic, message);
return true;
}
bool MQTToverSerial::SubscribeTopic(const char* topic)
{
if (!pubSubClient->connected())
{
Reconnect();
}
return pubSubClient->subscribe(topic);
}
bool MQTToverSerial::UnsubscribeTopic(const char* topic)
{
if (!pubSubClient->connected())
{
Reconnect();
}
return pubSubClient->unsubscribe(topic);
}
void MQTToverSerial::Reconnect()
{
// Loop until we're reconnected
while (!pubSubClient->connected())
{
// Attempt to connect
if (!pubSubClient->connect(MQTTid, MQTTlogin, MQTTpass))
{
serial.print("ERR_");
serial.print(specialCharacter);
serial.println("No connection with MQTT broker!");
if (debugSerial != NULL)
{
debugSerial->print("ERR_");
debugSerial->print(specialCharacter);
debugSerial->println("No connection with MQTT broker!");
}
// Wait 5 seconds before retrying
delay(5000);
}
}
}
#define BUFFER_SIZE 2000 //UART RX buffer size
void MQTToverSerial::ReadSerial()
{
char serialBuffer[BUFFER_SIZE + 1];
memset(serialBuffer, 0, BUFFER_SIZE + 1);
while(serial.readBytesUntil('\n', serialBuffer, BUFFER_SIZE))
{
if (debugSerial != NULL)
{
debugSerial->println(serialBuffer);
}
char command[5];
strncpy(command, serialBuffer, 4);
command[4] = '\0';
if (strcmp(command, "SEND") == 0)
{
size_t i = 5;
char topic[BUFFER_SIZE + 1];
size_t i_t = 0;
memset(topic, 0, BUFFER_SIZE + 1);
char message[BUFFER_SIZE + 1];
size_t i_m = 0;
memset(message, 0, BUFFER_SIZE + 1);
while (i < BUFFER_SIZE + 1 && serialBuffer[i] != specialCharacter)
{
topic[i_t] = serialBuffer[i];
++i;
++i_t;
}
++i;
if (strlen(topic) == 0)
{
Error(serialBuffer);
memset(serialBuffer, 0, BUFFER_SIZE + 1);
continue;
}
while (i < BUFFER_SIZE + 1 && serialBuffer[i] != '\n')
{
message[i_m] = serialBuffer[i];
++i;
++i_m;
}
if(!pubSubClient->connected())
Reconnect();
Publish(topic, message);
}
else if (strcmp(command, "SUBS") == 0)
{
size_t i = 5;
char topic[BUFFER_SIZE + 1];
size_t i_t = 0;
memset(topic, 0, BUFFER_SIZE + 1);
while (i < BUFFER_SIZE + 1 && serialBuffer[i] != '\n')
{
topic[i_t] = serialBuffer[i];
++i;
++i_t;
}
if (strlen(topic) == 0)
{
Error(serialBuffer);
memset(serialBuffer, 0, BUFFER_SIZE + 1);
continue;
}
if(!pubSubClient->connected())
Reconnect();
SubscribeTopic(topic);
}
else if (strcmp(command, "UNSB") == 0)
{
size_t i = 5;
char topic[BUFFER_SIZE + 1];
size_t i_t = 0;
memset(topic, 0, BUFFER_SIZE + 1);
while (i < BUFFER_SIZE + 1 && serialBuffer[i] != '\n')
{
topic[i_t] = serialBuffer[i];
++i;
++i_t;
}
if (strlen(topic) == 0)
{
Error(serialBuffer);
memset(serialBuffer, 0, BUFFER_SIZE + 1);
continue;
}
if(!pubSubClient->connected())
Reconnect();
UnsubscribeTopic(topic);
}
else
{
Error(serialBuffer);
}
memset(serialBuffer, 0, BUFFER_SIZE + 1);
}
}
bool MQTToverSerial::Loop()
{
if (!pubSubClient->connected()) {
Reconnect();
}
return pubSubClient->loop();
}
void MQTToverSerial::Error(const char* buff)
{
serial.print("ERR_");
serial.print(specialCharacter);
serial.println(buff);
if (debugSerial != NULL)
{
debugSerial->print("ERR_");
debugSerial->print(specialCharacter);
debugSerial->println(buff);
}
}