-
Notifications
You must be signed in to change notification settings - Fork 5
/
MqttSerial.cpp
executable file
·81 lines (57 loc) · 1.58 KB
/
MqttSerial.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
#include "SoftwareSerial.h"
#include "MqttSerial.h"
#include "JsonParser.h"
using namespace ArduinoJson::Parser;
MqttSerial::MqttSerial(String clientName, SoftwareSerial &serial) {
this->clientName = clientName;
this->serial = &serial;
}
void MqttSerial::printToSerialPort(int command, String value) {
serial->write(command);
serial->print(value);
serial->write(245);
}
void MqttSerial::begin(String hostname, String port, String username, String password) {
printToSerialPort(1, clientName);
printToSerialPort(2, hostname);
printToSerialPort(3, port);
printToSerialPort(4, username);
printToSerialPort(5, password);
}
void MqttSerial::connect()
{
printToSerialPort(6, "");
}
void MqttSerial::disconnect()
{
printToSerialPort(7, "");
}
void MqttSerial::publish(String topic, String payload) {
printToSerialPort(8, topic);
printToSerialPort(9, payload);
}
void MqttSerial::subscribe(String topic) {
printToSerialPort(10, topic);
}
void MqttSerial::loop(void (*callback) (String topic, String payload)) {
if (serial->available() > 1) {
char buff[1000];
int i =0;
int data = serial->read();
JsonParser<16> parser;
while (true) {
if (data == -1) continue;
if (data == 254) break;
buff[i++] = data;
data = serial->read();
delay(10);
}
buff[i] = '\0';
JsonObject root = parser.parse(buff);
char* topic = root["topic"];
char* payload = root["payload"];
String topicS(topic);
String payloadS(payload);
callback(topicS, payloadS);
}
}