@@ -79,7 +79,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
79
79
* PUBLIC MEMBER FUNCTIONS
80
80
******************************************************************************/
81
81
82
- int ArduinoIoTCloudTCP::begin (ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
82
+ int ArduinoIoTCloudTCP::begin (ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect )
83
83
{
84
84
_connection = &connection;
85
85
_brokerAddress = brokerAddress;
@@ -135,14 +135,17 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
135
135
136
136
/* Setup retry timers */
137
137
_connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
138
- return begin (enable_watchdog, _brokerAddress, _brokerPort);
138
+ return begin (enable_watchdog, _brokerAddress, _brokerPort, auto_reconnect );
139
139
}
140
140
141
- int ArduinoIoTCloudTCP::begin (bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
141
+ int ArduinoIoTCloudTCP::begin (bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect )
142
142
{
143
143
_enable_watchdog = enable_watchdog;
144
144
_brokerAddress = brokerAddress;
145
145
_brokerPort = brokerPort;
146
+ _auto_reconnect = auto_reconnect;
147
+
148
+ _state = State::ConfigPhy;
146
149
147
150
_mqttClient.setClient (_brokerClient);
148
151
@@ -214,6 +217,7 @@ void ArduinoIoTCloudTCP::update()
214
217
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker (); break ;
215
218
case State::Connected: next_state = handle_Connected (); break ;
216
219
case State::Disconnect: next_state = handle_Disconnect (); break ;
220
+ case State::Disconnected: break ;
217
221
}
218
222
219
223
_state = next_state;
@@ -273,6 +277,16 @@ void ArduinoIoTCloudTCP::printDebugInfo()
273
277
DEBUG_INFO (" MQTT Broker: %s:%d" , _brokerAddress.c_str (), _brokerPort);
274
278
}
275
279
280
+ void ArduinoIoTCloudTCP::disconnect () {
281
+ if (_state == State::ConfigPhy || _state == State::Init) {
282
+ return ;
283
+ }
284
+
285
+ _mqttClient.stop ();
286
+ _auto_reconnect = false ;
287
+ _state = State::Disconnect;
288
+ }
289
+
276
290
/* *****************************************************************************
277
291
* PRIVATE MEMBER FUNCTIONS
278
292
******************************************************************************/
@@ -431,9 +445,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
431
445
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
432
446
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
433
447
434
- /* Setup timer for broker connection and restart */
435
- _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
436
- return State::ConnectPhy;
448
+ if (_auto_reconnect) {
449
+ /* Setup timer for broker connection and restart */
450
+ _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
451
+ return State::ConnectPhy;
452
+ }
453
+
454
+ return State::Disconnected;
437
455
}
438
456
439
457
void ArduinoIoTCloudTCP::onMessage (int length)
@@ -680,6 +698,7 @@ int ArduinoIoTCloudTCP::updateCertificate(String authorityKeyIdentifier, String
680
698
}
681
699
return 0 ;
682
700
}
701
+
683
702
#endif
684
703
685
704
/* *****************************************************************************
0 commit comments