@@ -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
@@ -215,6 +218,7 @@ void ArduinoIoTCloudTCP::update()
215
218
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker (); break ;
216
219
case State::Connected: next_state = handle_Connected (); break ;
217
220
case State::Disconnect: next_state = handle_Disconnect (); break ;
221
+ case State::Disconnected: break ;
218
222
}
219
223
220
224
_state = next_state;
@@ -274,6 +278,16 @@ void ArduinoIoTCloudTCP::printDebugInfo()
274
278
DEBUG_INFO (" MQTT Broker: %s:%d" , _brokerAddress.c_str (), _brokerPort);
275
279
}
276
280
281
+ void ArduinoIoTCloudTCP::disconnect () {
282
+ if (_state == State::ConfigPhy || _state == State::Init) {
283
+ return ;
284
+ }
285
+
286
+ _mqttClient.stop ();
287
+ _auto_reconnect = false ;
288
+ _state = State::Disconnect;
289
+ }
290
+
277
291
/* *****************************************************************************
278
292
* PRIVATE MEMBER FUNCTIONS
279
293
******************************************************************************/
@@ -447,9 +461,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect()
447
461
DEBUG_INFO (" Disconnected from Arduino IoT Cloud" );
448
462
execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
449
463
450
- /* Setup timer for broker connection and restart */
451
- _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
452
- return State::ConnectPhy;
464
+ if (_auto_reconnect) {
465
+ /* Setup timer for broker connection and restart */
466
+ _connection_attempt.begin (AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
467
+ return State::ConnectPhy;
468
+ }
469
+
470
+ return State::Disconnected;
453
471
}
454
472
455
473
void ArduinoIoTCloudTCP::onMessage (int length)
@@ -696,6 +714,7 @@ int ArduinoIoTCloudTCP::updateCertificate(String authorityKeyIdentifier, String
696
714
}
697
715
return 0 ;
698
716
}
717
+
699
718
#endif
700
719
701
720
/* *****************************************************************************
0 commit comments