diff --git a/.vscode/settings.json b/.vscode/settings.json index 674c954..5b08dc6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "memory": "cpp", "random": "cpp", "optional": "cpp", - "memory_resource": "cpp" + "memory_resource": "cpp", + "system_error": "cpp" } } \ No newline at end of file diff --git a/lib/AliyunIoTSDK/src/AliyunIoTSDK.cpp b/lib/AliyunIoTSDK/src/AliyunIoTSDK.cpp index 5e3919f..48f9db9 100644 --- a/lib/AliyunIoTSDK/src/AliyunIoTSDK.cpp +++ b/lib/AliyunIoTSDK/src/AliyunIoTSDK.cpp @@ -16,6 +16,7 @@ #define CHECK_INTERVAL 10000 #define MESSAGE_BUFFER_SIZE 10 +#define MQTT_CONNECT_RETRY_MAX 3 static const char *deviceName = NULL; static const char *productKey = NULL; @@ -105,6 +106,7 @@ static void callback(char *topic, byte *payload, unsigned int length) { static bool mqttConnecting = false; int AliyunIoTSDK::mqttCheckConnect() { int mqttStatus = 0; + int connectRetry = 0; Serial.println("INFO:\tAlink MQTT connection checking..."); @@ -112,6 +114,7 @@ int AliyunIoTSDK::mqttCheckConnect() { Serial.print("INFO:\tAlink MQTT client state = "); Serial.println(client->state()); if (MQTT_CONNECTED != client->state()) { + connectRetry = 0; while (false == client->connected()) { client->disconnect(); Serial.print("INFO:\tConnecting to MQTT Server, clientId = "); @@ -124,10 +127,17 @@ int AliyunIoTSDK::mqttCheckConnect() { if (client->connect(clientId, mqttUsername, mqttPwd)) { Serial.println("INFO:\tMQTT Connected!"); } else { - Serial.print("ERROR:\tMQTT Connect err:"); + Serial.print("ERROR:\tMQTT Connect err: "); Serial.println(client->state()); - delay(60000); + delay(10000); + connectRetry++; + Serial.print("INFO:\tretry: "); + Serial.println(connectRetry); mqttStatus = -1; + if (connectRetry > MQTT_CONNECT_RETRY_MAX) { + Serial.println("ERROR:\t max connect retry times reached"); + break; + } } mqttConnecting = false; } diff --git a/lib/PubSubClient/src/PubSubClient.cpp b/lib/PubSubClient/src/PubSubClient.cpp index c78d373..88201b9 100644 --- a/lib/PubSubClient/src/PubSubClient.cpp +++ b/lib/PubSubClient/src/PubSubClient.cpp @@ -207,7 +207,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass return true; } else { _state = buffer[3]; - Serial.print("MQTT connection responded :"); + Serial.print("INFO:\tMQTT connection responded :"); Serial.println(_state); } } diff --git a/src/IRbaby.cpp b/src/IRbaby.cpp index f58a485..fb79a3a 100644 --- a/src/IRbaby.cpp +++ b/src/IRbaby.cpp @@ -74,8 +74,7 @@ void setup() { pinMode(0, OUTPUT); digitalWrite(0, LOW); attachInterrupt(digitalPinToInterrupt(RESET_PIN), factoryReset, ONLOW); - - delay(10); + delay(3000); Serial.clearWriteError(); INFOLN(); @@ -88,13 +87,20 @@ void setup() { INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby =="); // try loading saved iriskit settings + iriskit_settings.credential_token.clear(); + iriskit_settings.server_address.clear(); + if (loadSettings()) { iriskit_settings = getIrisKitSettings(); - INFOF("saved credentials loaded, token = %s\n", - iriskit_settings.credential_token.c_str()); + INFOLN("saved credentials loaded"); + INFOF("server address is empty ? %s\n", iriskit_settings.server_address.isEmpty() ? "yes" : "no"); + INFOF("credential is empty ? %s\n", iriskit_settings.credential_token.isEmpty() ? "yes" : "no"); + } + if (!iriskit_settings.credential_token.isEmpty() && + !iriskit_settings.credential_token.equalsIgnoreCase("NULL") && + !iriskit_settings.server_address.isEmpty() && + !iriskit_settings.server_address.equalsIgnoreCase("NULL")) { iriskit_settings_loaded = true; - } else { - INFOLN("no credentials saved yet, request new from IRIS server"); } // custom parameter for iris credentials @@ -105,6 +111,7 @@ void setup() { memset(iris_credential_token, 0, CREDENTIAL_MAX); if (!iriskit_settings_loaded) { + INFOLN("iriskit settings not loaded, set it from WifiManager"); server_address = new WiFiManagerParameter("server_address", "Server Address", "", URL_SHORT_MAX); credential_token = @@ -117,6 +124,7 @@ void setup() { wifi_manager.addParameter(server_address); wifi_manager.addParameter(credential_token); } else { + INFOLN("iriskit settings loaded"); strcpy(iris_server_address, iriskit_settings.server_address.c_str()); strcpy(iris_credential_token, iriskit_settings.credential_token.c_str()); } @@ -134,6 +142,8 @@ void setup() { delete credential_token; } + // TODO: fix the logic without settings loaded + INFOF("Wifi Connected, IRIS server = %s, credential token = %s\n", iris_server_address, iris_credential_token); @@ -149,9 +159,9 @@ void setup() { credential_init_retry++; if (credential_init_retry >= CREDENTIAL_INIT_RETRY_MAX) { ERRORLN("retried fetch credential for 3 times, reset WiFi"); - factoryReset(); + wifiReset(); } - delay(1000); + delay(2000); } while (1); INFOF("credential get : %s\n", iris_credential_token); @@ -184,7 +194,7 @@ void factoryReset() { } last_interrupt_time = interrupt_time; if (end_time - start_time > 3000) { - factoryReset(); + wifiReset(); } } @@ -192,8 +202,13 @@ void factoryReset() { // private function defitions static void wifiReset() { - DEBUGLN("\nReset settings"); - wifi_manager.resetSettings(); + DEBUGLN("Reset settings"); LittleFS.format(); + wifi_manager.resetSettings(); + WiFi.mode(WIFI_AP_STA); // cannot erase if not in STA mode ! + WiFi.persistent(true); + WiFi.disconnect(true); + WiFi.persistent(false); + delay(2000); ESP.reset(); } diff --git a/src/IRbabyAlink.cpp b/src/IRbabyAlink.cpp index dbfe87a..020d868 100644 --- a/src/IRbabyAlink.cpp +++ b/src/IRbabyAlink.cpp @@ -68,6 +68,7 @@ void checkAlinkMQTT() { iot_retry = 0; sendIrisKitHeartBeat(); } else { + INFOF("Alink MQTT check failed, retry = %d\n", iot_retry); iot_retry++; } if (iot_retry >= IOT_RETRY_MAX) { diff --git a/src/IRbabyHttp.cpp b/src/IRbabyHttp.cpp index 961176e..8e383d4 100644 --- a/src/IRbabyHttp.cpp +++ b/src/IRbabyHttp.cpp @@ -52,9 +52,20 @@ int fetchIrisCredential(String credential_token, String& device_name, String& device_secret) { int ret = -1; + bool protocol_prefix = false; + String fetch_credential_url; String device_id("IRbaby_"); - String fetch_credential_url(iris_server_address); + if (NULL != strstr(iris_server_address, "http://")) { + protocol_prefix = true; + } + if (protocol_prefix) { + fetch_credential_url = String(iris_server_address); + } else { + fetch_credential_url = String("http://"); + fetch_credential_url.concat(iris_server_address); + } + HTTPClient http_client; int tsi = 0; int response_code = 0; @@ -89,14 +100,20 @@ int fetchIrisCredential(String credential_token, INFOF("HTTP response payload = %s\n", payload.c_str()); http_response_doc.clear(); if (OK == deserializeJson(http_response_doc, payload.c_str())) { - String ds = http_response_doc["entity"]; - device_secret = ds; - INFOF("HTTP response deserialized, PK = %s, DN = %s, DS = %s\n", - product_key.c_str(), device_name.c_str(), device_secret.c_str()); - ret = 0; + String ds = ""; + int resultCode = http_response_doc["status"]["code"]; + if (0 == resultCode) { + INFOLN("response valid, try getting entity"); + ds = (String) http_response_doc["entity"]; + device_secret = ds; + INFOF("HTTP response deserialized, PK = %s, DN = %s, DS = %s\n", + product_key.c_str(), device_name.c_str(), device_secret.c_str()); + ret = 0; + } else { + INFOF("response invalid, code = %d\n", resultCode); + } } } - http_client.end(); return ret;