-
Notifications
You must be signed in to change notification settings - Fork 4
/
MQTTS_SIM7020E.cpp
276 lines (258 loc) · 6.88 KB
/
MQTTS_SIM7020E.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
#include "MQTTS_SIM7020E.h"
#include <Arduino.h>
MQTTS_SIM7020E::MQTTS_SIM7020E():
_serial(1) {}
void MQTTS_SIM7020E::setupModule() {
pinMode(26, OUTPUT);
rebootModule();
_serial.begin(9600, SERIAL_8N1, 16, 17);
}
void MQTTS_SIM7020E::rebootModule() {
digitalWrite(26, LOW);
delay(800);
digitalWrite(26, HIGH);
}
void MQTTS_SIM7020E::checkModuleReady() {
int count = 0;
_serial.println(F("AT"));
int previousCheck = millis();
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (_data_input.indexOf(F("OK")) != -1) {
Serial.println(F("...SIM7020E is Ready"));
break;
}
} else {
int currentCheck = millis();
if (currentCheck - previousCheck > 5000) {
if (count > 5) {
Serial.print(F("\nCannot connect to SIM7020E, rebooting..."));
}
previousCheck = currentCheck;
Serial.print(F("."));
rebootModule();
_serial.println(F("AT"));
delay(100);
count++;
}
delay(500);
_serial.println(F("AT"));
delay(100);
Serial.print(F("."));
}
}
_flushSerial();
}
void MQTTS_SIM7020E::waitForNetwork() {
_serial.println(F("AT+CGATT?"));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf(F("+CGATT: 1")) != -1) {
delay(1000);
Serial.println(F("...Network is ready"));
break;
}
} else {
delay(1000);
_serial.println(F("AT+CGATT?"));
delay(100);
Serial.print(F("."));
}
}
_flushSerial();
}
String MQTTS_SIM7020E::dnsLookup(String hostname) {
_serial.println("AT+CDNSGIP=\"" + hostname + "\"");
String data = "";
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf(F("+CDNSGIP: 1")) != -1) {
int index = _data_input.indexOf(F(","));
index = _data_input.indexOf(F(","), index + 1);
data = _data_input.substring(index + 2, _data_input.length() - 2);
break;
} else if (_data_input.indexOf(F("+CDNSGIP: 0")) != -1) {
return "";
}
}
}
_flushSerial();
return data;
}
void MQTTS_SIM7020E::setCertificate(int type, String cert) {
_clearCertificate(type);
const int batchSize = 1024;
int certSize = cert.length() + 3;
int batchCount = cert.length() / batchSize;
if (cert.length() % 1024 != 0) {
batchCount++;
}
for (int i = 0; i < batchCount; i++) {
int startIndex = i * batchSize;
int endIndex = startIndex + batchSize;
bool lastBatch = i == batchCount - 1;
String payload = cert.substring(startIndex, endIndex);
_serial.print(F("AT+CSETCA="));
_serial.print(type);
_serial.print(F(","));
_serial.print(certSize);
_serial.print(F(","));
_serial.print(lastBatch ? 0 : 1);
_serial.print(F(",0,\""));
_serial.print(payload);
_serial.println(F("\""));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf(F("OK")) != -1) {
break;
}
if (_data_input.indexOf(F("ERROR")) != -1) {
Serial.print(F("ERROR setting certificate type: "));
Serial.println(type);
return;
}
}
}
_flushSerial();
}
_flushSerial();
}
void MQTTS_SIM7020E::_clearCertificate(int type) {
_serial.print(F("AT+CSETCA="));
_serial.print(type);
_serial.println(F(",1,0,0,\" \""));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf(F("OK")) != -1) {
break;
}
}
}
_flushSerial();
}
void MQTTS_SIM7020E::checkCertificates() {
_serial.println(F("AT+CSETCA?"));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
Serial.println(_data_input);
if (_data_input.indexOf("OK") != -1) {
break;
}
}
}
_flushSerial();
}
void MQTTS_SIM7020E::connect(String ipAddress, String port) {
_serial.print(F("AT+CMQTTSNEW=\""));
_serial.print(ipAddress);
_serial.print(F("\",\""));
_serial.print(port);
_serial.println(F("\",60000,1024"));
int count = 0;
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf("OK") != -1) {
Serial.println(F("...MQTT connected"));
break;
}
if (_data_input.indexOf("ERROR") != -1) {
Serial.println(F("ERROR creating new MQTTS connection"));
break;
}
} else if (count < 60) {
delay(1000);
Serial.print(F("."));
count++;
} else {
Serial.println(F("Cannot create MQTT connection"));
break;
}
}
_flushSerial();
}
void MQTTS_SIM7020E::registerClient(String clientName) {
_serial.print(F("AT+CMQCON=0,4,\""));
_serial.print(clientName);
_serial.println(F("\",600,1,0"));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf("OK") != -1) {
Serial.println(F("...Client registered"));
break;
}
}
}
delay(1000);
_flushSerial();
}
void MQTTS_SIM7020E::publish(String topic, String payload) {
bool success = false;
for (int i = 0; i < 3; i++) {
_serial.print(F("AT+CMQPUB=0,\""));
_serial.print(topic);
_serial.print(F("\",0,0,0,"));
_serial.print(payload.length() * 2);
_serial.print(F(",\""));
for (int j = 0; j < payload.length(); j++) {
_serial.print(payload[j], HEX);
}
_serial.println("\"");
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf("OK") != -1) {
success = true;
break;
} else if (_data_input.indexOf("ERROR") != -1) {
break;
}
}
}
if (success) {
break;
}
}
if (!success) {
Serial.println(F("ERROR publishing message"));
}
_flushSerial();
}
void MQTTS_SIM7020E::disconnect() {
_serial.println(F("AT+CMQDISCON=0"));
while (1) {
if (_serial.available()) {
_data_input = _serial.readStringUntil('\n');
if (debug) Serial.println("<<" + _data_input);
if (_data_input.indexOf("OK") != -1) {
Serial.println(F("...MQTT disconnected"));
break;
}
}
}
_flushSerial();
}
void MQTTS_SIM7020E::_flushSerial() {
while (1) {
if (_serial.available()) {
_serial.readStringUntil('\n');
} else {
break;
}
}
_serial.flush();
}