enhanced Aliot MQTT connection full through

This commit is contained in:
strawmanbobi
2022-02-27 20:01:08 +08:00
parent 0e4d73de5f
commit b020ec47e3
6 changed files with 66 additions and 22 deletions

View File

@@ -12,6 +12,7 @@
"memory": "cpp", "memory": "cpp",
"random": "cpp", "random": "cpp",
"optional": "cpp", "optional": "cpp",
"memory_resource": "cpp" "memory_resource": "cpp",
"system_error": "cpp"
} }
} }

View File

@@ -16,6 +16,7 @@
#define CHECK_INTERVAL 10000 #define CHECK_INTERVAL 10000
#define MESSAGE_BUFFER_SIZE 10 #define MESSAGE_BUFFER_SIZE 10
#define MQTT_CONNECT_RETRY_MAX 3
static const char *deviceName = NULL; static const char *deviceName = NULL;
static const char *productKey = NULL; static const char *productKey = NULL;
@@ -105,6 +106,7 @@ static void callback(char *topic, byte *payload, unsigned int length) {
static bool mqttConnecting = false; static bool mqttConnecting = false;
int AliyunIoTSDK::mqttCheckConnect() { int AliyunIoTSDK::mqttCheckConnect() {
int mqttStatus = 0; int mqttStatus = 0;
int connectRetry = 0;
Serial.println("INFO:\tAlink MQTT connection checking..."); Serial.println("INFO:\tAlink MQTT connection checking...");
@@ -112,6 +114,7 @@ int AliyunIoTSDK::mqttCheckConnect() {
Serial.print("INFO:\tAlink MQTT client state = "); Serial.print("INFO:\tAlink MQTT client state = ");
Serial.println(client->state()); Serial.println(client->state());
if (MQTT_CONNECTED != client->state()) { if (MQTT_CONNECTED != client->state()) {
connectRetry = 0;
while (false == client->connected()) { while (false == client->connected()) {
client->disconnect(); client->disconnect();
Serial.print("INFO:\tConnecting to MQTT Server, clientId = "); Serial.print("INFO:\tConnecting to MQTT Server, clientId = ");
@@ -126,8 +129,15 @@ int AliyunIoTSDK::mqttCheckConnect() {
} else { } else {
Serial.print("ERROR:\tMQTT Connect err: "); Serial.print("ERROR:\tMQTT Connect err: ");
Serial.println(client->state()); Serial.println(client->state());
delay(60000); delay(10000);
connectRetry++;
Serial.print("INFO:\tretry: ");
Serial.println(connectRetry);
mqttStatus = -1; mqttStatus = -1;
if (connectRetry > MQTT_CONNECT_RETRY_MAX) {
Serial.println("ERROR:\t max connect retry times reached");
break;
}
} }
mqttConnecting = false; mqttConnecting = false;
} }

View File

@@ -207,7 +207,7 @@ boolean PubSubClient::connect(const char *id, const char *user, const char *pass
return true; return true;
} else { } else {
_state = buffer[3]; _state = buffer[3];
Serial.print("MQTT connection responded :"); Serial.print("INFO:\tMQTT connection responded :");
Serial.println(_state); Serial.println(_state);
} }
} }

View File

@@ -74,8 +74,7 @@ void setup() {
pinMode(0, OUTPUT); pinMode(0, OUTPUT);
digitalWrite(0, LOW); digitalWrite(0, LOW);
attachInterrupt(digitalPinToInterrupt(RESET_PIN), factoryReset, ONLOW); attachInterrupt(digitalPinToInterrupt(RESET_PIN), factoryReset, ONLOW);
delay(3000);
delay(10);
Serial.clearWriteError(); Serial.clearWriteError();
INFOLN(); INFOLN();
@@ -88,13 +87,20 @@ void setup() {
INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby =="); INFOLN("== IRIS Kit [1.2.7] Powered by IRBaby ==");
// try loading saved iriskit settings // try loading saved iriskit settings
iriskit_settings.credential_token.clear();
iriskit_settings.server_address.clear();
if (loadSettings()) { if (loadSettings()) {
iriskit_settings = getIrisKitSettings(); iriskit_settings = getIrisKitSettings();
INFOF("saved credentials loaded, token = %s\n", INFOLN("saved credentials loaded");
iriskit_settings.credential_token.c_str()); 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; iriskit_settings_loaded = true;
} else {
INFOLN("no credentials saved yet, request new from IRIS server");
} }
// custom parameter for iris credentials // custom parameter for iris credentials
@@ -105,6 +111,7 @@ void setup() {
memset(iris_credential_token, 0, CREDENTIAL_MAX); memset(iris_credential_token, 0, CREDENTIAL_MAX);
if (!iriskit_settings_loaded) { if (!iriskit_settings_loaded) {
INFOLN("iriskit settings not loaded, set it from WifiManager");
server_address = server_address =
new WiFiManagerParameter("server_address", "Server Address", "", URL_SHORT_MAX); new WiFiManagerParameter("server_address", "Server Address", "", URL_SHORT_MAX);
credential_token = credential_token =
@@ -117,6 +124,7 @@ void setup() {
wifi_manager.addParameter(server_address); wifi_manager.addParameter(server_address);
wifi_manager.addParameter(credential_token); wifi_manager.addParameter(credential_token);
} else { } else {
INFOLN("iriskit settings loaded");
strcpy(iris_server_address, iriskit_settings.server_address.c_str()); strcpy(iris_server_address, iriskit_settings.server_address.c_str());
strcpy(iris_credential_token, iriskit_settings.credential_token.c_str()); strcpy(iris_credential_token, iriskit_settings.credential_token.c_str());
} }
@@ -134,6 +142,8 @@ void setup() {
delete credential_token; delete credential_token;
} }
// TODO: fix the logic without settings loaded
INFOF("Wifi Connected, IRIS server = %s, credential token = %s\n", INFOF("Wifi Connected, IRIS server = %s, credential token = %s\n",
iris_server_address, iris_credential_token); iris_server_address, iris_credential_token);
@@ -149,9 +159,9 @@ void setup() {
credential_init_retry++; credential_init_retry++;
if (credential_init_retry >= CREDENTIAL_INIT_RETRY_MAX) { if (credential_init_retry >= CREDENTIAL_INIT_RETRY_MAX) {
ERRORLN("retried fetch credential for 3 times, reset WiFi"); ERRORLN("retried fetch credential for 3 times, reset WiFi");
factoryReset(); wifiReset();
} }
delay(1000); delay(2000);
} while (1); } while (1);
INFOF("credential get : %s\n", iris_credential_token); INFOF("credential get : %s\n", iris_credential_token);
@@ -184,7 +194,7 @@ void factoryReset() {
} }
last_interrupt_time = interrupt_time; last_interrupt_time = interrupt_time;
if (end_time - start_time > 3000) { if (end_time - start_time > 3000) {
factoryReset(); wifiReset();
} }
} }
@@ -192,8 +202,13 @@ void factoryReset() {
// private function defitions // private function defitions
static void wifiReset() { static void wifiReset() {
DEBUGLN("\nReset settings"); DEBUGLN("Reset settings");
wifi_manager.resetSettings();
LittleFS.format(); 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(); ESP.reset();
} }

View File

@@ -68,6 +68,7 @@ void checkAlinkMQTT() {
iot_retry = 0; iot_retry = 0;
sendIrisKitHeartBeat(); sendIrisKitHeartBeat();
} else { } else {
INFOF("Alink MQTT check failed, retry = %d\n", iot_retry);
iot_retry++; iot_retry++;
} }
if (iot_retry >= IOT_RETRY_MAX) { if (iot_retry >= IOT_RETRY_MAX) {

View File

@@ -52,9 +52,20 @@ int fetchIrisCredential(String credential_token,
String& device_name, String& device_name,
String& device_secret) { String& device_secret) {
int ret = -1; int ret = -1;
bool protocol_prefix = false;
String fetch_credential_url;
String device_id("IRbaby_"); 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; HTTPClient http_client;
int tsi = 0; int tsi = 0;
int response_code = 0; int response_code = 0;
@@ -89,14 +100,20 @@ int fetchIrisCredential(String credential_token,
INFOF("HTTP response payload = %s\n", payload.c_str()); INFOF("HTTP response payload = %s\n", payload.c_str());
http_response_doc.clear(); http_response_doc.clear();
if (OK == deserializeJson(http_response_doc, payload.c_str())) { if (OK == deserializeJson(http_response_doc, payload.c_str())) {
String ds = http_response_doc["entity"]; 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; device_secret = ds;
INFOF("HTTP response deserialized, PK = %s, DN = %s, DS = %s\n", INFOF("HTTP response deserialized, PK = %s, DN = %s, DS = %s\n",
product_key.c_str(), device_name.c_str(), device_secret.c_str()); product_key.c_str(), device_name.c_str(), device_secret.c_str());
ret = 0; ret = 0;
} else {
INFOF("response invalid, code = %d\n", resultCode);
}
} }
} }
http_client.end(); http_client.end();
return ret; return ret;