MCPcopy Create free account
hub / github.com/SmingHub/Sming / startMqttClient

Function startMqttClient

samples/MqttClient_Hello/app/application.cpp:70–108  ·  view source on GitHub ↗

Run MQTT client

Source from the content-addressed store, hash-verified

68
69// Run MQTT client
70void 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
110void onConnected(IpAddress ip, IpAddress netmask, IpAddress gateway)
111{

Callers 2

publishMessageFunction · 0.70
onConnectedFunction · 0.70

Calls 14

setWillMethod · 0.80
setEventHandlerMethod · 0.80
setConnectedHandlerMethod · 0.80
getRemoteIpMethod · 0.80
setCompleteDelegateMethod · 0.80
subscribeMethod · 0.80
publishMessageFunction · 0.70
FFunction · 0.50
stopMethod · 0.45
startMethod · 0.45
setMessageHandlerMethod · 0.45
setSslInitHandlerMethod · 0.45

Tested by

no test coverage detected