Skip to content

Commit 5ca9a2f

Browse files
committed
Add a state ConnectPhy where we are waiting for the connection to the phy layer to be established
1 parent 500f416 commit 5ca9a2f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extern "C" unsigned long getTime()
7474
******************************************************************************/
7575

7676
ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
77-
: _state{State::ConnectMqttBroker}
77+
: _state{State::ConnectPhy}
7878
, _lastSyncRequestTickTime{0}
7979
, _mqtt_data_buf{0}
8080
, _mqtt_data_len{0}
@@ -116,7 +116,6 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, String brokerAddre
116116

117117
int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
118118
{
119-
120119
_brokerAddress = brokerAddress;
121120
_brokerPort = brokerPort;
122121

@@ -176,8 +175,6 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
176175

177176
void ArduinoIoTCloudTCP::update()
178177
{
179-
if(checkPhyConnection() != NetworkConnectionState::CONNECTED) return;
180-
181178
/* Retrieve the latest data from the MQTT Client. */
182179
if (_mqttClient.connected())
183180
_mqttClient.poll();
@@ -186,6 +183,7 @@ void ArduinoIoTCloudTCP::update()
186183
State next_state = _state;
187184
switch (_state)
188185
{
186+
case State::ConnectPhy: next_state = handle_ConnectPhy(); break;
189187
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker(); break;
190188
case State::SubscribeMqttTopics: next_state = handle_SubscribeMqttTopics(); break;
191189
case State::RequestLastValues: next_state = handle_RequestLastValues(); break;
@@ -220,6 +218,14 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
220218
* PRIVATE MEMBER FUNCTIONS
221219
******************************************************************************/
222220

221+
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
222+
{
223+
if (_connection->check() == NetworkConnectionState::CONNECTED)
224+
return State::ConnectMqttBroker;
225+
else
226+
return State::ConnectPhy;
227+
}
228+
223229
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
224230
{
225231
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
9898

9999
enum class State
100100
{
101+
ConnectPhy,
101102
ConnectMqttBroker,
102103
SubscribeMqttTopics,
103104
RequestLastValues,
@@ -146,6 +147,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
146147
inline String getTopic_datain () { return ( getThingId().length() == 0) ? String("/a/d/" + getDeviceId() + "/e/i") : String("/a/t/" + getThingId() + "/e/i"); }
147148
inline String getTopic_ota_in () { return String("/a/d/" + getDeviceId() + "/ota/i"); }
148149

150+
State handle_ConnectPhy();
149151
State handle_ConnectMqttBroker();
150152
State handle_SubscribeMqttTopics();
151153
State handle_RequestLastValues();

0 commit comments

Comments
 (0)