From a7d91d2c8f71f2cddb6e4f461284937bf699d340 Mon Sep 17 00:00:00 2001 From: i3water <121024123@qq.com> Date: Thu, 8 Nov 2018 16:39:19 +0800 Subject: [PATCH] update codes, upgrade ESP8266 REGISTER & REFRESH codes --- src/Adapters/BlinkerMQTT.h | 136 +++++++++++++++++++++++++++++++-- src/Adapters/BlinkerPRO.h | 137 ++++++++++++++++++++++++++++++++-- src/Blinker/BlinkerProtocol.h | 56 ++++++++++++-- 3 files changed, 310 insertions(+), 19 deletions(-) diff --git a/src/Adapters/BlinkerMQTT.h b/src/Adapters/BlinkerMQTT.h index ab52fede..3fba8638 100644 --- a/src/Adapters/BlinkerMQTT.h +++ b/src/Adapters/BlinkerMQTT.h @@ -478,10 +478,133 @@ class BlinkerMQTT { bool BlinkerMQTT::connectServer() { const int httpsPort = 443; - const char* host = "https://iotdev.clz.me"; #if defined(ESP8266) + const char* host = "iotdev.clz.me"; const char* fingerprint = "84 5f a4 8a 70 5e 79 7e f5 b3 b4 20 45 c8 35 55 72 f6 85 5a"; + + // WiFiClientSecure client_s; + +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG2(("connecting to "), host); +#endif + + + uint8_t connet_times = 0; + client_s.stop(); + ::delay(100); + + while (1) { + bool cl_connected = false; + if (!client_s.connect(host, httpsPort)) { + // #ifdef BLINKER_DEBUG_ALL + BLINKER_ERR_LOG1(("server connection failed")); + // #endif + // return BLINKER_CMD_FALSE; + + connet_times++; + } + else { +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG1(("connection succeed")); +#endif + // return true; + cl_connected = true; + + break; + } + + if (connet_times >= 4 && !cl_connected) return BLINKER_CMD_FALSE; + } + +#ifndef BLINKER_LAN_DEBUG + if (client_s.verify(fingerprint, host)) { + #ifdef BLINKER_DEBUG_ALL + // _status = DH_VERIFIED; + BLINKER_LOG1(("Fingerprint verified")); + // return true; + #endif + } + else { + #ifdef BLINKER_DEBUG_ALL + // _status = DH_VERIFY_FAILED; + // _status = DH_VERIFIED; + BLINKER_LOG1(("Fingerprint verification failed!")); + // return false; + #endif + } +#endif + + // String url; + String client_msg; + + String url_iot = "/api/v1/user/device/diy/auth?authKey=" + String(authkey); + +#if defined(BLINKER_ALIGENIE_LIGHT) + url_iot += "&aliType=light"; +#elif defined(BLINKER_ALIGENIE_OUTLET) + url_iot += "&aliType=outlet"; +#elif defined(BLINKER_ALIGENIE_SWITCH) +#elif defined(BLINKER_ALIGENIE_SENSOR) + url_iot += "&aliType=sensor"; +#endif + +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG3("HTTPS begin: ", host, url_iot); +#endif + + client_msg = STRING_format("GET " + url_iot + " HTTP/1.1\r\n" + + "Host: " + host + ":" + STRING_format(httpsPort) + "\r\n" + + "Connection: close\r\n\r\n"); + + client_s.print(client_msg); +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG2(("client_msg: "), client_msg); +#endif + + unsigned long timeout = millis(); + while (client_s.available() == 0) { + if (millis() - timeout > 5000) { + BLINKER_LOG1((">>> Client Timeout !")); + client_s.stop(); + return BLINKER_CMD_FALSE; + } + } + + String _dataGet; + String lastGet; + String lengthOfJson; + while (client_s.available()) { + // String line = client_s.readStringUntil('\r'); + _dataGet = client_s.readStringUntil('\n'); + + if (_dataGet.startsWith("Content-Length: ")){ + int addr_start = _dataGet.indexOf(' '); + int addr_end = _dataGet.indexOf('\0', addr_start + 1); + lengthOfJson = _dataGet.substring(addr_start + 1, addr_end); + } + + if (_dataGet == "\r") { +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG1(("headers received")); +#endif + break; + } + } + + for(int i=0;i= 4 && !cl_connected) return BLINKER_CMD_FALSE; + } + +#ifndef BLINKER_LAN_DEBUG + if (client_s.verify(fingerprint, host)) { + #ifdef BLINKER_DEBUG_ALL + // _status = DH_VERIFIED; + BLINKER_LOG1(("Fingerprint verified")); + // return true; + #endif + } + else { + #ifdef BLINKER_DEBUG_ALL + // _status = DH_VERIFY_FAILED; + // _status = DH_VERIFIED; + BLINKER_LOG1(("Fingerprint verification failed!")); + // return false; + #endif + } +#endif + + // String url; + String client_msg; + + String url_iot = "/api/v1/user/device/register?deviceType=" + \ + String(_deviceType) + "&deviceName=" + macDeviceName(); + + if (_deviceType == BLINKER_SMART_LAMP) { + url_iot += "&aliType=light"; + } + else if (_deviceType == BLINKER_SMART_PLUGIN) { + url_iot += "&aliType=outlet"; + } + else if (_deviceType == BLINKER_AIR_DETECTOR) { + url_iot += "&aliType=sensor"; + } + + #ifdef BLINKER_DEBUG_ALL + BLINKER_LOG3("HTTPS begin: ", host, url_iot); +#endif + + client_msg = STRING_format("GET " + url_iot + " HTTP/1.1\r\n" + + "Host: " + host + ":" + STRING_format(httpsPort) + "\r\n" + + "Connection: close\r\n\r\n"); + + client_s.print(client_msg); +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG2(("client_msg: "), client_msg); +#endif + + unsigned long timeout = millis(); + while (client_s.available() == 0) { + if (millis() - timeout > 5000) { + BLINKER_LOG1((">>> Client Timeout !")); + client_s.stop(); + return BLINKER_CMD_FALSE; + } + } + + String _dataGet; + String lastGet; + String lengthOfJson; + while (client_s.available()) { + // String line = client_s.readStringUntil('\r'); + _dataGet = client_s.readStringUntil('\n'); + + if (_dataGet.startsWith("Content-Length: ")){ + int addr_start = _dataGet.indexOf(' '); + int addr_end = _dataGet.indexOf('\0', addr_start + 1); + lengthOfJson = _dataGet.substring(addr_start + 1, addr_end); + } + + if (_dataGet == "\r") { +#ifdef BLINKER_DEBUG_ALL + BLINKER_LOG1(("headers received")); +#endif + break; + } + } + + for(int i=0;i::run() // } if (_getRegister) { - if (!_isRegistered && ((millis() - _register_fresh) > 60000 || _register_fresh == 0)) { + if (!_isRegistered && ((millis() - _register_fresh) > 5000 || _register_fresh == 0)) { #ifdef BLINKER_DEBUG_ALL BLINKER_LOG1(BLINKER_F("conn deviceRegister")); #endif @@ -1601,7 +1602,15 @@ void BlinkerProtocol::run() // if (_disconnectCount >= 12) { #ifdef BLINKER_DEBUG_ALL BLINKER_LOG1(BLINKER_F("device reRegister")); + BLINKER_LOG_FreeHeap(); #endif + if (BLINKER_FreeHeap() < 15000) { + conn.disconnect(); + return; + } + + BLINKER_LOG_FreeHeap(); + if (conn.reRegister()) { _disconnectCount = 0; _disconnectTime = millis(); @@ -1614,6 +1623,27 @@ void BlinkerProtocol::run() BApi::ntpInit(); } + + if ((millis() - _refreshTime) >= BLINKER_ONE_DAY_TIME * 2 * 1000) { + // if ((millis() - _refreshTime) >= 120000) { + conn.disconnect(); + + #ifdef BLINKER_DEBUG_ALL + BLINKER_LOG1(BLINKER_F("device reRegister")); + BLINKER_LOG_FreeHeap(); + #endif + + if (BLINKER_FreeHeap() < 15000) { + conn.disconnect(); + return; + } + + BLINKER_LOG_FreeHeap(); + + if (conn.reRegister()) { + _refreshTime = millis(); + } + } #endif #if defined(BLINKER_WIFI) || defined(BLINKER_MQTT) @@ -1626,8 +1656,9 @@ void BlinkerProtocol::run() WiFi.reconnect(); } - if ((millis() > _reconTime) && (millis() - _reconTime) >= 10000) { - _reconTime += 10000; + // if ((millis() > _reconTime) && (millis() - _reconTime) >= 10000) { + if ((millis() - _reconTime) >= 10000) { + _reconTime = millis(); BLINKER_LOG1(BLINKER_F("WiFi disconnected! reconnecting!")); @@ -1638,6 +1669,15 @@ void BlinkerProtocol::run() } #endif +// #if defined(BLINKER_MQTT) +// // if ((millis() - _refreshTime) >= BLINKER_ONE_DAY_TIME * 2 * 1000) { +// if ((millis() - _refreshTime) >= 120000) { +// conn.begin(_authKey); + +// _refreshTime += BLINKER_ONE_DAY_TIME * 2 * 1000; +// } +// #endif + bool conState = conn.connected(); switch (state) @@ -1662,8 +1702,9 @@ void BlinkerProtocol::run() _disFreshTime = millis(); } else { - if ((millis() > _disFreshTime) && (millis() - _disFreshTime) >= 5000) { - _disFreshTime += 5000; + // if ((millis() > _disFreshTime) && (millis() - _disFreshTime) >= 5000) { + if ((millis() - _disFreshTime) >= 5000) { + _disFreshTime = millis(); _disconnectCount++; if (_disconnectCount > 12) _disconnectCount = 12; @@ -1701,8 +1742,9 @@ void BlinkerProtocol::run() _disFreshTime = millis(); } else { - if ((millis() > _disFreshTime) && (millis() - _disFreshTime) >= 5000) { - _disFreshTime += 5000; + // if ((millis() > _disFreshTime) && (millis() - _disFreshTime) >= 5000) { + if ((millis() - _disFreshTime) >= 5000) { + _disFreshTime = millis(); _disconnectCount++; if (_disconnectCount > 12) _disconnectCount = 12;