From 8770d82534b555502e6b1b985e41647e7f5353b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grome=C5=A1?= Date: Wed, 11 Jul 2018 17:40:25 +0200 Subject: [PATCH] MQTT - Updated comments --- .../ESP8266_MQTT_Publish.ino | 20 ++++++++++++---- .../ESP8266_MQTT_Subscribe.ino | 24 +++++++++++++------ src/protocols/MQTT.cpp | 2 ++ src/protocols/MQTT.h | 2 ++ 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino index cf161b39..22d88c3a 100644 --- a/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino +++ b/examples/ESP8266_MQTT_Publish/ESP8266_MQTT_Publish.ino @@ -17,13 +17,15 @@ ESP8266 wifi = Kite.ModuleA; // create MQTT client instance using the wifi module -MQTTClient mqtt(&wifi); +// the default port used for MQTT is 1883 +MQTTClient mqtt(&wifi, 1883); void setup() { Serial.begin(9600); - // initialize ESP8266 with baudrate 9600 + // initialize ESP8266 Serial.print(F("[ESP8266] Initializing ... ")); + // baudrate: 9600 baud byte state = wifi.begin(9600); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -33,8 +35,10 @@ void setup() { while(true); } - // join AP named "SSID" using the password "password" + // join access point Serial.print(F("[ESP8266] Joining AP ... ")); + // name: SSID + // password: password state = wifi.join("SSID", "password"); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -44,8 +48,12 @@ void setup() { while(true); } - // connect to MQTT server using client ID "arduino", username "try" and password "try" + // connect to MQTT server Serial.print(F("[ESP8266] Connecting to MQTT server ... ")); + // server URL: broker.shiftr.io + // client ID: arduino + // username: try + // password: try state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try"); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -57,8 +65,10 @@ void setup() { } void loop() { - // publish MQTT message to the topic "hello" with content "world" + // publish MQTT message Serial.print(F("[ESP8266] Publishing MQTT message ... ")); + // topic name: hello + // application message: world byte state = mqtt.publish("hello", "world"); if(state == ERR_NONE) { Serial.println(F("success!")); diff --git a/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino index 15941896..6bc6a360 100644 --- a/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino +++ b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino @@ -17,13 +17,15 @@ ESP8266 wifi = Kite.ModuleA; // create MQTT client instance using the wifi module -MQTTClient mqtt(&wifi); +// the default port used for MQTT is 1883 +MQTTClient mqtt(&wifi, 1883); void setup() { Serial.begin(9600); - // initialize ESP8266 with baudrate 9600 + // initialize ESP8266 Serial.print(F("[ESP8266] Initializing ... ")); + // baudrate: 9600 baud byte state = wifi.begin(9600); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -33,8 +35,10 @@ void setup() { while(true); } - // join AP named "SSID" using the password "password" + // join access point Serial.print(F("[ESP8266] Joining AP ... ")); + // name: SSID + // password: password state = wifi.join("SSID", "password"); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -44,8 +48,12 @@ void setup() { while(true); } - // connect to MQTT broker using client ID "arduino", username "try" and password "try" - Serial.print(F("[ESP8266] Connecting to MQTT broker ... ")); + // connect to MQTT server + Serial.print(F("[ESP8266] Connecting to MQTT server ... ")); + // server URL: broker.shiftr.io + // client ID: arduino + // username: try + // password: try state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try"); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -55,10 +63,11 @@ void setup() { while(true); } - // subscribe to the topic "hello" + // subscribe to MQTT topic // after calling this method, server will send PUBLISH packets // to this client each time a new message was published at the topic Serial.print(F("[ESP8266] Subscribing to MQTT topic ... ")); + // topic name: hello state = wifi.subscribe("hello"); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -67,9 +76,10 @@ void setup() { Serial.println(state, HEX); } - // unsubscribe from topic "hello" + // unsubscribe from MQTT topic // after calling this method, server will stop sending PUBLISH packets Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... ")); + // topic filter: hello state = wifi.unsubscribe("hello"); if(state == ERR_NONE) { Serial.println(F("success!")); diff --git a/src/protocols/MQTT.cpp b/src/protocols/MQTT.cpp index 9ea6e889..e808a874 100644 --- a/src/protocols/MQTT.cpp +++ b/src/protocols/MQTT.cpp @@ -379,6 +379,7 @@ uint8_t MQTTClient::check(void (*func)(const char*, const char*)) { } size_t MQTTClient::encodeLength(uint32_t len, uint8_t* encoded) { + // algorithm to encode packet length as per MQTT specification 3.1.1 size_t i = 0; do { encoded[i] = len % 128; @@ -392,6 +393,7 @@ size_t MQTTClient::encodeLength(uint32_t len, uint8_t* encoded) { } uint32_t MQTTClient::decodeLength(uint8_t* encoded) { + // algorithm to decode packet length as per MQTT specification 3.1.1 uint32_t mult = 1; uint32_t len = 0; uint8_t i = 0; diff --git a/src/protocols/MQTT.h b/src/protocols/MQTT.h index 1f2e00ae..53604d3f 100644 --- a/src/protocols/MQTT.h +++ b/src/protocols/MQTT.h @@ -29,8 +29,10 @@ class MQTTClient { public: + // constructor MQTTClient(TransportLayer* tl, uint16_t port = 1883); + // basic methods uint8_t connect(const char* host, const char* clientId, const char* userName = "", const char* password = "", uint16_t keepAlive = 60, bool cleanSession = true, const char* willTopic = "", const char* willMessage = ""); uint8_t disconnect(); uint8_t publish(const char* topic, const char* message);