//tries to create a connection with the mqtt broker. also used to try and reconnect. bool ATTDevice::MqttConnect() { char mqttId[23]; // Or something long enough to hold the longest file name you will ever use. int length = _deviceId.length(); length = length > 22 ? 22 : length; _deviceId.toCharArray(mqttId, length); mqttId[length] = 0; if(_mqttUserName && _mqttpwd){ if (!_mqttclient->connect(mqttId, (char*)_mqttUserName, (char*)_mqttpwd)) { #ifdef DEBUG Serial.print(MQTTSERVTEXT); Serial.println(FAILED_RETRY); #endif return false; } #ifdef DEBUG Serial.print(MQTTSERVTEXT); Serial.println(SUCCESTXT); #endif MqttSubscribe(); return true; } else{ #ifdef DEBUG Serial.print(MQTTSERVTEXT); Serial.println("failed: invalid credentials"); #endif return false; } }
//tries to create a connection with the mqtt broker. also used to try and reconnect. void ATTDevice::MqttConnect() { char mqttId[23]; // Or something long enough to hold the longest file name you will ever use. int length = _deviceId.length(); length = length > 22 ? 22 : length; _deviceId.toCharArray(mqttId, length); mqttId[length] = 0; String brokerId = _clientId + ":" + _clientId; while (!_mqttclient->connect(mqttId, (char*)brokerId.c_str(), (char*)_clientKey.c_str())) { #ifdef DEBUG Serial.print(MQTTSERVTEXT); Serial.println(FAILED_RETRY); #endif delay(RETRYDELAY); } #ifdef DEBUG Serial.print(MQTTSERVTEXT); Serial.println(SUCCESTXT); #endif MqttSubscribe(); }