@@ -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;
@@ -271,6 +275,16 @@ void ArduinoIoTCloudTCP::printDebugInfo()
271
275
DEBUG_INFO (" MQTT Broker: %s:%d" , _brokerAddress.c_str (), _brokerPort);
272
276
}
273
277
278
+ void ArduinoIoTCloudTCP::disconnect () {
279
+ if (_state == State::ConfigPhy || _state == State::Init) {
280
+ return ;
281
+ }
282
+
283
+ _mqttClient.stop ();
284
+ _auto_reconnect = false ;
285
+ _state = State::Disconnect;
286
+ }
287
+
274
288
/* *****************************************************************************
275
289
* PRIVATE MEMBER FUNCTIONS
276
290
******************************************************************************/
@@ -436,9 +450,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
436
450
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
437
451
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
438
452
439
- /* Setup timer for broker connection and restart */
440
- _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
441
- return State::ConnectPhy;
453
+ if (_auto_reconnect) {
454
+ /* Setup timer for broker connection and restart */
455
+ _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
456
+ return State::ConnectPhy;
457
+ }
458
+
459
+ return State::Disconnected;
442
460
}
443
461
444
462
void ArduinoIoTCloudTCP::onMessage (int length)
@@ -685,6 +703,7 @@ int ArduinoIoTCloudTCP::updateCertificate(String authorityKeyIdentifier, String
685
703
}
686
704
return 0 ;
687
705
}
706
+
688
707
#endif
689
708
690
709
/* *****************************************************************************
0 commit comments