-
Notifications
You must be signed in to change notification settings - Fork 2
/
MQTT.cpp
339 lines (291 loc) · 9.93 KB
/
MQTT.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
///Define class for MQTT
#include <stdint.h>
#include <string.h>
#include "Arduino.h"
#include "MQTT.h"
char topic[100];
char message[4000];
uint8_t i=0;
MQTT::MQTT(){
};
MQTT::MQTT(Stream &out){
this->OUT = &out;
};
bool MQTT::initialize(){
if(!sim800.initialize(1, this->OUT)) return false;
return sim800.startTCP("m12.cloudmqtt.com",13079);
}
bool MQTT::isConnected(){
return _isConnected;
}
bool MQTT::connect(const char* MQTTClientID, const char* MQTTUsername, const char* MQTTPassword)
{
while(UART.available()) UART.read();
unsigned long datalength;
int X;
unsigned char encodedByte;
OUT->println("Connecting to MQTT Broker...");
UART.write(0x10);
MQTTProtocolNameLength = strlen(MQTTProtocolName);
MQTTClientIDLength = strlen(MQTTClientID);
MQTTUsernameLength = strlen(MQTTUsername);
MQTTPasswordLength = strlen(MQTTPassword);
datalength = MQTTProtocolNameLength + 2 + 4 + MQTTClientIDLength + 2 + MQTTUsernameLength + 2 + MQTTPasswordLength + 2;
OUT->print("Data Length :"); OUT->println(datalength);
X = datalength;
do
{
encodedByte = X % 128;
X = X / 128;
// if there are more data to encode, set the top bit of this byte
if ( X > 0 ) {
encodedByte |= 128;
}
UART.write(encodedByte);
}
while ( X > 0 );
UART.write(MQTTProtocolNameLength >> 8);
UART.write(MQTTProtocolNameLength & 0xFF);
UART.write(MQTTProtocolName);
UART.write(MQTTLVL); // LVL
UART.write(MQTTFlags); // Flags
UART.write(MQTTKeepAlive >> 8);
UART.write(MQTTKeepAlive & 0xFF);
UART.write(MQTTClientIDLength >> 8);
UART.write(MQTTClientIDLength & 0xFF);
UART.print(MQTTClientID);
UART.write(MQTTUsernameLength >> 8);
UART.write(MQTTUsernameLength & 0xFF);
UART.print(MQTTUsername);
UART.write(MQTTPasswordLength >> 8);
UART.write(MQTTPasswordLength & 0xFF);
UART.print(MQTTPassword);
UART.flush();
OUT->println("Connection packet sent...");
return false;
}
bool MQTT::publish(char* MQTTTopic, char* MQTTMessage, uint8_t qos){
OUT->println("Publishing data...");
char topic[100], packetid[100], str1[2000], str2[2000];
int datalength=0, topiclength=0,packetidlength=0, X=0;
unsigned char encodedByte;
topiclength = sprintf((char*)topic, MQTTTopic);
if(qos>0)
{
packetidlength = sprintf((char*)packetid,"PACKETID");
datalength = sprintf((char*)str1, "%s%s", topic,packetid);
}
else{
datalength = sprintf((char*)str1, "%s", topic);
}
//str1[datalength] = '\0';
datalength = sprintf((char*)str2, "%s%s", str1,MQTTMessage);
//str2[datalength] = '\0';
OUT->print("Datalength = ");
OUT->println(datalength);
OUT->print("str = ");
OUT->println(str2);
UART.write(0x30+(qos*2)); //QOS = 1
delay(100);
X= (qos>0)?(datalength + 2 + 2):(datalength + 2);
do
{
encodedByte = X % 128;
X = X / 128;
// if there are more data to encode, set the top bit of this byte
if ( X > 0 ) {
encodedByte |= 128;
}
UART.write(encodedByte);
}
while ( X > 0 );
UART.write(topiclength >> 8);
UART.write(topiclength & 0xFF);
UART.write(topic);
if(qos>0){
UART.write(packetidlength >> 8);
UART.write(packetidlength & 0xFF);
UART.print(packetid);
}
UART.print(MQTTMessage);
}
bool MQTT::subscribe(char* MQTTTopic)
{
int X=0;
unsigned char encodedByte;
unsigned short topiclength;
unsigned long datalength;
const char MQTTPacketID = 0x0001;
topiclength = strlen(MQTTTopic);
datalength = 2 + 2 + topiclength + 1;
UART.write(0x82);
X = datalength;
do
{
encodedByte = X % 128;
X = X / 128;
// if there are more data to encode, set the top bit of this byte
if ( X > 0 ) {
encodedByte |= 128;
}
UART.write(encodedByte);
}
while ( X > 0 );
UART.write(MQTTPacketID >> 8);
UART.write(MQTTPacketID & 0xFF);
UART.write(topiclength >> 8);
UART.write(topiclength & 0xFF);
UART.print(MQTTTopic);
UART.write(MQTTQOS);
}
bool MQTT::ping(){
packetType = PINGREQ;
UART.write(0xC0);
UART.write(0x00);
}
void MQTT::loop(){
ping();
//serialEvent();
}
void MQTT::serialEvent()
{
OUT->println("");
OUT->println("Begin");
int X;
unsigned char code;
unsigned char encodedByte;
unsigned long multiplier = 1;
int msglen = 0; int topiclen = 0;
while(UART.available())
{
code = UART.read();
OUT->print((char)code);
OUT->print(",");
OUT->print(code,HEX);
OUT->print(",");
OUT->println(code,DEC);
if(code==0x10){ //CONNECT packet
while(UART.available())
{
code=UART.read();
OUT->print((char)code);
}
}
if(code==0x20) //CONNACK
{
code=UART.read(); //Byte2 of CONNACK
code=UART.read(); //Byte3 of CONNACK
code=UART.read(); //Byte4 of CONNACK - Return Code
if(code==0x00) //Successfully Connected to MQTT broker
{
_isConnected = true;
OUT->println("Successfully Connected to MQTT broker");
}
else
{
if(code==0x01)
OUT->println("Connection Refused, unacceptable protocol version");
if(code==0x02)
OUT->println("Connection Refused, identifier rejected");
if(code==0x03)
OUT->println("Connection Refused, Server unavailable");
if(code==0x04)
OUT->println("Connection Refused, bad user name or password");
if(code==0x05)
OUT->println("Connection Refused, not authorized");
_isConnected = false;
OUT->print("Unable to connect to MQTT broker");
OUT->print(", CONNACK received :");
OUT->print(code,HEX);
}
}
if(code==0x90) //SUBACK
{
code=UART.read(); //Byte2 of SUBACK
code=UART.read(); //Byte3 of SUBACK
code=UART.read(); //Byte4 of SUBACK
code=UART.read(); //Byte5 of SUBACK
if(code==0x00 || code==0x01 || code==0x02)
OUT->println("Successfully subscribed..");
else // When return is 0x80
OUT->println("Unable to subscribe..");
}
else if(code==0xD0 || code==0xC0){
if(code==0xD0){
code=UART.read();
if(code==0x00)
_isConnected=true;
else
_isConnected=false;
}
}
else if(code==0x50){
while(UART.available()){
OUT->print((char)UART.read());
}
}
else if(code==0x30 ) //Message Received
{
OUT->println("Message Begin...");
do{
encodedByte = UART.read(); //Get the Length of the message
msglen = msglen + ((encodedByte & 127) * multiplier);
multiplier = multiplier * 128;
if(multiplier > 2097152) // 2097152 = 128 * 128 * 128
{
OUT->println("Exception while calculating the Messge Length");
break;
}
}
while((encodedByte & 128)!=0);
code = UART.read(); // Topic Len MSB
//OUT->print((char)code);OUT->print(",");OUT->println(code,HEX);
code = UART.read(); // Topic Len LSB
//OUT->print((char)code);OUT->print(",");OUT->println(code,HEX);
topiclen = code;
OUT->print("Msg Length:"); OUT->println(msglen,DEC);
OUT->print("Topic Length:"); OUT->println(topiclen,DEC);
OUT->print("Topic : ");
uint8_t i = 0;
for(i=0;i<topiclen;i++){
OUT->print((char)UART.read());
}
OUT->print("\nMessage :");
int count = 0;
while(UART.available()){
code = UART.read();
OUT->print((char)code);
count++;
}
OUT->print("\nMessage Length = "); OUT->print(count);
OUT->println("\nMessage End...");
}
else if(code==0x32)
{
OUT->println("Message packet begin");
while(UART.available()){
code=UART.read();
OUT->print(code);
OUT->print(",");
OUT->print(code,HEX);
OUT->print(",");
OUT->println((char)code);
}
OUT->println("Message packet end");
}
else if(code==0x34)
{
OUT->println("Message packet begin");
while(UART.available()){
code=UART.read();
OUT->print(code);
OUT->print(",");
OUT->print(code,HEX);
OUT->print(",");
OUT->println((char)code);
}
OUT->println("Message packet end");
}
}
OUT->println("End\n");
}