Skip to content

Commit 00b52fe

Browse files
author
Jan Kuthan
committed
fixup! Asnyc call ping
1 parent 9bf348d commit 00b52fe

1 file changed

Lines changed: 51 additions & 47 deletions

File tree

src/main.cpp

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const char* mqttDeviceId = "RelayBoard-1";
1717
// firmware
1818
const char* hearthbeatTopic = "devices/arduino/relayboard-1/heartbeat";
1919
const int ethCheckConnectionTimerMillis = 5000;
20-
const int hearthbeatTimerMillis = 5000;
21-
const int pingTimerMillis = 100;
20+
const int hearthbeatTimerMillis = 10000;
21+
const int pingTimerMillis = 50;
2222
//#############################################
2323

2424

@@ -88,15 +88,14 @@ AsyncDelay delay_ethCheckConnection;
8888
AsyncDelay delay_hearthbeat;
8989

9090
void connectEthernet();
91-
void connectMqtt();
91+
bool connectMqtt();
9292
void restartAllConnections();
9393
void pushHearthbeat();
9494
void prepareOutputPins();
9595
void prepareInputPins();
96-
bool connectionIsOk();
9796
void prepareTimers();
98-
void checkEthernetAndReconnectIfConnectionLost();
99-
void checkMqttConnectionAndReconnectIfConnectionLost();
97+
bool checkEthernetAndReconnectIfConnectionLost();
98+
bool checkMqttConnectionAndReconnectIfConnectionLost();
10099
void checkInputChangesAndPublishToMqtt();
101100
void checkAsyncPingsForSetDefaultValue();
102101
void pingPinAndSetAsyncTimer(int pin, bool valueForPing, int arrayIndex);
@@ -121,17 +120,13 @@ void setup() {
121120
}
122121

123122
void loop() {
124-
checkEthernetAndReconnectIfConnectionLost();
125-
126-
checkMqttConnectionAndReconnectIfConnectionLost();
127-
128-
mqttClient.loop();
129-
130-
pushHearthbeat();
131-
132-
checkInputChangesAndPublishToMqtt();
133-
134123
checkAsyncPingsForSetDefaultValue();
124+
125+
if (checkEthernetAndReconnectIfConnectionLost() && checkMqttConnectionAndReconnectIfConnectionLost()) {
126+
mqttClient.loop();
127+
pushHearthbeat();
128+
checkInputChangesAndPublishToMqtt();
129+
}
135130
}
136131

137132
// MQTT subscribe
@@ -229,48 +224,52 @@ void connectEthernet() {
229224
Ethernet.begin(mac);
230225
}
231226

232-
void connectMqtt() {
227+
bool connectMqtt() {
228+
229+
bool connected = false;
230+
233231
Serial.println("Connecting to MQTT start:");
234232

235233
Serial.println("- reset previous status (settingsOutputInitial)");
236234
memset(settingsOutputInitial, 0, sizeof(settingsOutputInitial));
237235

238-
Serial.println("- set server:");
239-
Serial.print("- adddress:");
240-
Serial.println(mqttServer);
241-
Serial.print("- port:");
236+
Serial.print("- set server: ");
237+
Serial.print(mqttServer);
238+
Serial.print(":");
242239
Serial.println(mqttPort);
243-
244240
mqttClient.setServer(mqttServer, mqttPort);
245241

246242
Serial.println("- set MQTT callback");
247-
248243
mqttClient.setCallback(callback);
249244

250-
while (!mqttClient.connected()) {
245+
if (!mqttClient.connected()) {
251246
Serial.print("Connecting MQTT to server: ");
252247

253248
if (!mqttClient.connect(mqttDeviceId, mqttUser, mqttPassword )) {
254-
255-
Serial.print("(disconnect all mqtt connections)");
256-
mqttClient.disconnect();
257-
258249
Serial.print("- failed with state ");
259250
Serial.println(mqttClient.state());
260-
delay(5000);
251+
delay(2000);
261252
}
262253
}
263-
Serial.println("- connected");
264254

265-
Serial.println("Set subscribe for all OUTPUT topics:");
266-
for (int i =0; i < sizeof(settingsOutput) / sizeof(settingsOutput[0]); i++) {
267-
Serial.print(" - ");
268-
Serial.println(settingsOutput[i].subscribedTopic);
255+
if (!mqttClient.connected()) {
256+
Serial.println("- cannot connect, try later...");
257+
} else {
258+
Serial.println("- connected");
259+
connected = true;
260+
261+
Serial.println("Set subscribe for all OUTPUT topics:");
262+
for (int i =0; i < sizeof(settingsOutput) / sizeof(settingsOutput[0]); i++) {
263+
Serial.print(" - ");
264+
Serial.println(settingsOutput[i].subscribedTopic);
269265

270-
mqttClient.subscribe(settingsOutput[i].subscribedTopic);
266+
mqttClient.subscribe(settingsOutput[i].subscribedTopic);
267+
}
268+
269+
Serial.println("Connection and setup MQTT complete!");
271270
}
272271

273-
Serial.println("Connection and setup MQTT complete!");
272+
return connected;
274273
}
275274

276275
void restartAllConnections() {
@@ -294,32 +293,37 @@ void pushHearthbeat() {
294293
}
295294
}
296295

297-
bool connectionIsOk() {
298-
return ethClient.connected();
299-
}
296+
bool checkEthernetAndReconnectIfConnectionLost() {
297+
bool connected = true;
300298

301-
void checkEthernetAndReconnectIfConnectionLost() {
302299
if (delay_ethCheckConnection.isExpired()) {
303300
Serial.print("Check network connection: ");
304301

305-
if (!connectionIsOk()) {
302+
connected = false;
303+
if (!ethClient.connected()) {
306304
Serial.println("lost");
307305

308306
Serial.println("Connection lost... Reconnecting...");
309-
restartAllConnections();
307+
connectEthernet();
308+
} else {
309+
Serial.println("ok");
310310
}
311311

312-
Serial.println("ok");
313-
314312
delay_ethCheckConnection.repeat();
315313
}
314+
315+
return connected;
316316
}
317317

318-
void checkMqttConnectionAndReconnectIfConnectionLost() {
318+
bool checkMqttConnectionAndReconnectIfConnectionLost() {
319+
bool connected = true;
320+
319321
if (!mqttClient.connected()) {
320322
Serial.println("Connection to MQTT server lost - reconnecting.");
321-
connectMqtt();
323+
connected = connectMqtt();
322324
}
325+
326+
return connected;
323327
}
324328

325329
void prepareTimers() {
@@ -414,7 +418,7 @@ void checkAsyncPingsForSetDefaultValue() {
414418

415419
digitalWrite(settingsOutput[i].pin, !settingsOutput[i].valueForPing);
416420

417-
infoOutputPinDelayForPing[i] = false; //todo: refactor na objekt společný s timerem
421+
infoOutputPinDelayForPing[i] = false;
418422
}
419423
}
420424
}

0 commit comments

Comments
 (0)