Skip to content

Commit fc1e056

Browse files
committed
fix MQTT bug
1 parent 323f82c commit fc1e056

File tree

1 file changed

+77
-64
lines changed

1 file changed

+77
-64
lines changed

src/Adapters/BlinkerMQTT.h

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#if defined(ESP8266)
55
#include <ESP8266mDNS.h>
66
#include <ESP8266WiFi.h>
7+
#include <ESP8266HTTPClient.h>
78
#elif defined(ESP32)
89
#include <ESPmDNS.h>
910
#include <WiFi.h>
11+
#include <HTTPClient.h>
1012
#endif
1113
#include "Blinker/BlinkerProtocol.h"
1214
#include "modules/WebSockets/WebSocketsServer.h"
@@ -175,84 +177,90 @@ class BlinkerMQTT {
175177

176178
void BlinkerMQTT::connectServer() {
177179
const int httpsPort = 443;
178-
const char* host = "iotdev.clz.me";
179-
const char* fingerprint = "b0 79 d7 e6 b2 be a7 1f 28 65 40 db a7 6a 7d 78 83 19 85 68";
180-
#ifdef BLINKER_DEBUG_ALL
181-
BLINKER_LOG2("connecting to ", host);
182-
#endif
183-
while (!client.connect(host, httpsPort)) {
184-
#ifdef BLINKER_DEBUG_ALL
185-
BLINKER_LOG1("connection failed");
186-
#endif
187-
// return;
188-
::delay(100);
189-
}
190-
#ifdef BLINKER_DEBUG_ALL
191-
BLINKER_LOG1("connection success");
192-
#endif
193-
180+
const char* host = "https://iotdev.clz.me";
194181
#if defined(ESP8266)
195-
if (client.verify(fingerprint, host)) {
196-
#ifdef BLINKER_DEBUG_ALL
197-
BLINKER_LOG1("certificate matches");
198-
#endif
199-
}
200-
else {
201-
#ifdef BLINKER_DEBUG_ALL
202-
BLINKER_LOG1("certificate doesn't match");
203-
#endif
204-
}
182+
const char* fingerprint = "84 5f a4 8a 70 5e 79 7e f5 b3 b4 20 45 c8 35 55 72 f6 85 5a";
183+
#elif defined(ESP32)
184+
// const char* ca = \
185+
// "-----BEGIN CERTIFICATE-----\n" \
186+
// "MIIEgDCCA2igAwIBAgIQDKTfhr9lmWbWUT0hjX36oDANBgkqhkiG9w0BAQsFADBy\n" \
187+
// "MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg\n" \
188+
// "SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy\n" \
189+
// "dXN0QXNpYSBUTFMgUlNBIENBMB4XDTE4MDEwNDAwMDAwMFoXDTE5MDEwNDEyMDAw\n" \
190+
// "MFowGDEWMBQGA1UEAxMNaW90ZGV2LmNsei5tZTCCASIwDQYJKoZIhvcNAQEBBQAD\n" \
191+
// "ggEPADCCAQoCggEBALbOFn7cJ2I/FKMJqIaEr38n4kCuJCCeNf1bWdWvOizmU2A8\n" \
192+
// "QeTAr5e6Q3GKeJRdPnc8xXhqkTm4LOhgdZB8KzuVZARtu23D4vj4sVzxgC/zwJlZ\n" \
193+
// "MRMxN+cqI37kXE8gGKW46l2H9vcukylJX+cx/tjWDfS2YuyXdFuS/RjhCxLgXzbS\n" \
194+
// "cve1W0oBZnBPRSMV0kgxTWj7hEGZNWKIzK95BSCiMN59b+XEu3NWGRb/VzSAiJEy\n" \
195+
// "Hy9DcDPBC9TEg+p5itHtdMhy2gq1OwsPgl9HUT0xmDATSNEV2RB3vwviNfu9/Eif\n" \
196+
// "ObhsV078zf30TqdiESqISEB68gJ0Otru67ePoTkCAwEAAaOCAWowggFmMB8GA1Ud\n" \
197+
// "IwQYMBaAFH/TmfOgRw4xAFZWIo63zJ7dygGKMB0GA1UdDgQWBBR/KLqnke61779P\n" \
198+
// "xc9htonQwLOxPDAYBgNVHREEETAPgg1pb3RkZXYuY2x6Lm1lMA4GA1UdDwEB/wQE\n" \
199+
// "AwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwTAYDVR0gBEUwQzA3\n" \
200+
// "BglghkgBhv1sAQIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQu\n" \
201+
// "Y29tL0NQUzAIBgZngQwBAgEwgYEGCCsGAQUFBwEBBHUwczAlBggrBgEFBQcwAYYZ\n" \
202+
// "aHR0cDovL29jc3AyLmRpZ2ljZXJ0LmNvbTBKBggrBgEFBQcwAoY+aHR0cDovL2Nh\n" \
203+
// "Y2VydHMuZGlnaXRhbGNlcnR2YWxpZGF0aW9uLmNvbS9UcnVzdEFzaWFUTFNSU0FD\n" \
204+
// "QS5jcnQwCQYDVR0TBAIwADANBgkqhkiG9w0BAQsFAAOCAQEAhtM4eyrWB14ajJpQ\n" \
205+
// "ibZ5FbzVuvv2Le0FOSoss7UFCDJUYiz2LiV8yOhL4KTY+oVVkqHaYtcFS1CYZNzj\n" \
206+
// "6xWcqYZJ+pgsto3WBEgNEEe0uLSiTW6M10hm0LFW9Det3k8fqwSlljqMha3gkpZ6\n" \
207+
// "8WB0f2clXOuC+f1SxAOymnGUsSqbU0eFSgevcOIBKR7Hr3YXBXH3jjED76Q52OMS\n" \
208+
// "ucfOM9/HB3jN8o/ioQbkI7xyd/DUQtzK6hSArEoYRl3p5H2P4fr9XqmpoZV3i3gQ\n" \
209+
// "oOdVycVtpLunyUoVAB2DcOElfDxxXCvDH3XsgoIU216VY03MCaUZf7kZ2GiNL+UX\n" \
210+
// "9UBd0Q==\n" \
211+
// "-----END CERTIFICATE-----\n";
212+
#endif
213+
214+
HTTPClient http;
215+
216+
String url_iot = String(host) + "/api/v1/user/device/diy/auth?authKey=" + String(authkey);
217+
#ifdef BLINKER_DEBUG_ALL
218+
BLINKER_LOG2("HTTPS begin: ", url_iot);
205219
#endif
206220

207-
String url_iot = "/api/v1/user/device/diy/auth?authkey=" + String(authkey);
208-
#ifdef BLINKER_DEBUG_ALL
209-
BLINKER_LOG2("requesting URL: ", url_iot);
210-
#endif
211-
client.print(String("GET ") + "/" + " HTTP/1.1\r\n" +
212-
"Host: " + host + "\r\n" +
213-
"User-Agent: Blinker_MQTT_DEVICE\r\n" +
214-
"Connection: close\r\n\r\n");
215-
#ifdef BLINKER_DEBUG_ALL
216-
BLINKER_LOG1("request sent");
221+
#if defined(ESP8266)
222+
http.begin(url_iot, fingerprint); //HTTP
223+
#elif defined(ESP32)
224+
// http.begin(url_iot, ca); TODO
225+
http.begin(url_iot);
217226
#endif
218-
String dataGet;
219-
String lastGet;
220-
String lengthOfJson;
221-
222-
while (client.connected()) {
223-
dataGet = client.readStringUntil('\n');
227+
int httpCode = http.GET();
224228

225-
if (dataGet.startsWith("Content-Length: ")){
226-
lengthOfJson = STRING_find_string(dataGet, " ", "\0", 0);
227-
}
229+
String payload;
228230

229-
if (dataGet == "\r") {
230-
#ifdef BLINKER_DEBUG_ALL
231-
BLINKER_LOG1("headers received");
231+
if (httpCode > 0) {
232+
// HTTP header has been send and Server response header has been handled
233+
#ifdef BLINKER_DEBUG_ALL
234+
BLINKER_LOG2("[HTTP] GET... code: ", httpCode);
232235
#endif
233-
break;
236+
237+
// file found at server
238+
if (httpCode == HTTP_CODE_OK) {
239+
payload = http.getString();
240+
BLINKER_LOG1(payload);
234241
}
235242
}
236-
237-
for(int i=0;i<lengthOfJson.toInt();i++){
238-
lastGet += (char)client.read();
243+
else {
244+
#ifdef BLINKER_DEBUG_ALL
245+
BLINKER_LOG2("[HTTP] GET... failed, error: ", http.errorToString(httpCode).c_str());
246+
#endif
239247
}
240248

241-
dataGet = lastGet;
249+
http.end();
242250

243251
#ifdef BLINKER_DEBUG_ALL
244252
BLINKER_LOG1("reply was:");
245253
BLINKER_LOG1("==============================");
246-
BLINKER_LOG1(dataGet);
254+
BLINKER_LOG1(payload);
247255
BLINKER_LOG1("==============================");
248256
#endif
249257

250-
String _userID = STRING_find_string(dataGet, "deviceName", "\"", 3);
251-
String _userName = STRING_find_string(dataGet, "iotId", "\"", 3);
252-
String _key = STRING_find_string(dataGet, "iotToken", "\"", 3);
253-
String _productInfo = STRING_find_string(dataGet, "ProductKey", "\"", 3);
254-
String _broker = STRING_find_string(dataGet, "broker", "\"", 3);
255-
String _uuid = STRING_find_string(dataGet, "uuid", "\"", 3);
258+
String _userID = STRING_find_string(payload, "deviceName", "\"", 4);
259+
String _userName = STRING_find_string(payload, "iotId", "\"", 4);
260+
String _key = STRING_find_string(payload, "iotToken", "\"", 4);
261+
String _productInfo = STRING_find_string(payload, "ProductKey", "\"", 4);
262+
String _broker = STRING_find_string(payload, "broker", "\"", 4);
263+
String _uuid = STRING_find_string(payload, "uuid", "\"", 4);
256264

257265
if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
258266
memcpy(DEVICE_NAME, _userID.c_str(), 12);
@@ -280,14 +288,14 @@ void BlinkerMQTT::connectServer() {
280288
BLINKER_LOG1("====================");
281289
BLINKER_LOG2("DEVICE_NAME: ", DEVICE_NAME);
282290
BLINKER_LOG2("MQTT_PRODUCTINFO: ", MQTT_PRODUCTINFO);
283-
BLINKER_LOG2("MQTT_ID: ", MQTT_ID);
284-
BLINKER_LOG2("MQTT_NAME: ", MQTT_NAME);
285-
BLINKER_LOG2("MQTT_KEY: ", MQTT_KEY);
291+
BLINKER_LOG2("MQTT_ID: ", MQTT_ID);
292+
BLINKER_LOG2("MQTT_NAME: ", MQTT_NAME);
293+
BLINKER_LOG2("MQTT_KEY: ", MQTT_KEY);
286294
BLINKER_LOG2("MQTT_BROKER: ", _broker);
287295
BLINKER_LOG2("HOST: ", MQTT_HOST);
288296
BLINKER_LOG2("PORT: ", MQTT_PORT);
289297
BLINKER_LOG2("UUID: ", UUID);
290-
BLINKER_LOG1("====================");
298+
BLINKER_LOG1("====================");
291299
#endif
292300

293301
if (_broker == BLINKER_MQTT_BORKER_ALIYUN) {
@@ -344,9 +352,14 @@ bool BlinkerMQTT::connect() {
344352
if (mqtt->connected()) {
345353
return true;
346354
}
355+
if ((millis() - latestTime) < 5000) {
356+
return false;
357+
}
358+
347359
#ifdef BLINKER_DEBUG_ALL
348360
BLINKER_LOG1("Connecting to MQTT... ");
349361
#endif
362+
350363
if ((ret = mqtt->connect()) != 0) {
351364
BLINKER_LOG1(mqtt->connectErrorString(ret));
352365
BLINKER_LOG1("Retrying MQTT connection in 5 seconds...");

0 commit comments

Comments
 (0)