Run MQTT client
| 68 | |
| 69 | // Run MQTT client |
| 70 | void startMqttClient() |
| 71 | { |
| 72 | procTimer.stop(); |
| 73 | |
| 74 | // 1. [Setup] |
| 75 | if(!mqtt.setWill(F("last/will"), F("The connection from this device is lost:("), |
| 76 | MqttClient::getFlags(MQTT_QOS_AT_LEAST_ONCE, MQTT_RETAIN_TRUE))) { |
| 77 | debugf("Unable to set the last will and testament. Most probably there is not enough memory on the device."); |
| 78 | } |
| 79 | |
| 80 | mqtt.setEventHandler(MQTT_TYPE_PUBACK, onMessageDelivered); |
| 81 | |
| 82 | mqtt.setConnectedHandler([](MqttClient& client, mqtt_message_t* message) { |
| 83 | Serial << _F("Connected to ") << client.getRemoteIp() << endl; |
| 84 | |
| 85 | // Start publishing message now |
| 86 | publishMessage(); |
| 87 | // and schedule a timer to send messages every 5 seconds |
| 88 | procTimer.initializeMs<5 * 1000>(publishMessage).start(); |
| 89 | return 0; |
| 90 | }); |
| 91 | |
| 92 | mqtt.setCompleteDelegate(checkMQTTDisconnect); |
| 93 | mqtt.setMessageHandler(onMessageReceived); |
| 94 | |
| 95 | #ifdef ENABLE_SSL |
| 96 | mqtt.setSslInitHandler([](Ssl::Session& session) { |
| 97 | session.options.verifyLater = true; |
| 98 | session.keyCert.assign(default_private_key, sizeof(default_private_key), default_certificate, |
| 99 | sizeof(default_certificate), nullptr); |
| 100 | }); |
| 101 | #endif |
| 102 | |
| 103 | // 2. [Connect] |
| 104 | Url url(MQTT_URL); |
| 105 | Serial << _F("Connecting to ") << url << endl; |
| 106 | mqtt.connect(url, F("esp8266")); |
| 107 | mqtt.subscribe(F("main/status/#")); |
| 108 | } |
| 109 | |
| 110 | void onConnected(IpAddress ip, IpAddress netmask, IpAddress gateway) |
| 111 | { |
no test coverage detected