MQTT - Updated comments

This commit is contained in:
Jan Gromeš 2018-07-11 17:40:25 +02:00
parent d9c553f7d0
commit 8770d82534
4 changed files with 36 additions and 12 deletions

View file

@ -17,13 +17,15 @@
ESP8266 wifi = Kite.ModuleA; ESP8266 wifi = Kite.ModuleA;
// create MQTT client instance using the wifi module // 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() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// initialize ESP8266 with baudrate 9600 // initialize ESP8266
Serial.print(F("[ESP8266] Initializing ... ")); Serial.print(F("[ESP8266] Initializing ... "));
// baudrate: 9600 baud
byte state = wifi.begin(9600); byte state = wifi.begin(9600);
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -33,8 +35,10 @@ void setup() {
while(true); while(true);
} }
// join AP named "SSID" using the password "password" // join access point
Serial.print(F("[ESP8266] Joining AP ... ")); Serial.print(F("[ESP8266] Joining AP ... "));
// name: SSID
// password: password
state = wifi.join("SSID", "password"); state = wifi.join("SSID", "password");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -44,8 +48,12 @@ void setup() {
while(true); 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 ... ")); 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"); state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -57,8 +65,10 @@ void setup() {
} }
void loop() { void loop() {
// publish MQTT message to the topic "hello" with content "world" // publish MQTT message
Serial.print(F("[ESP8266] Publishing MQTT message ... ")); Serial.print(F("[ESP8266] Publishing MQTT message ... "));
// topic name: hello
// application message: world
byte state = mqtt.publish("hello", "world"); byte state = mqtt.publish("hello", "world");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));

View file

@ -17,13 +17,15 @@
ESP8266 wifi = Kite.ModuleA; ESP8266 wifi = Kite.ModuleA;
// create MQTT client instance using the wifi module // 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() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// initialize ESP8266 with baudrate 9600 // initialize ESP8266
Serial.print(F("[ESP8266] Initializing ... ")); Serial.print(F("[ESP8266] Initializing ... "));
// baudrate: 9600 baud
byte state = wifi.begin(9600); byte state = wifi.begin(9600);
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -33,8 +35,10 @@ void setup() {
while(true); while(true);
} }
// join AP named "SSID" using the password "password" // join access point
Serial.print(F("[ESP8266] Joining AP ... ")); Serial.print(F("[ESP8266] Joining AP ... "));
// name: SSID
// password: password
state = wifi.join("SSID", "password"); state = wifi.join("SSID", "password");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -44,8 +48,12 @@ void setup() {
while(true); while(true);
} }
// connect to MQTT broker using client ID "arduino", username "try" and password "try" // connect to MQTT server
Serial.print(F("[ESP8266] Connecting to MQTT broker ... ")); 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"); state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -55,10 +63,11 @@ void setup() {
while(true); while(true);
} }
// subscribe to the topic "hello" // subscribe to MQTT topic
// after calling this method, server will send PUBLISH packets // after calling this method, server will send PUBLISH packets
// to this client each time a new message was published at the topic // to this client each time a new message was published at the topic
Serial.print(F("[ESP8266] Subscribing to MQTT topic ... ")); Serial.print(F("[ESP8266] Subscribing to MQTT topic ... "));
// topic name: hello
state = wifi.subscribe("hello"); state = wifi.subscribe("hello");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
@ -67,9 +76,10 @@ void setup() {
Serial.println(state, HEX); Serial.println(state, HEX);
} }
// unsubscribe from topic "hello" // unsubscribe from MQTT topic
// after calling this method, server will stop sending PUBLISH packets // after calling this method, server will stop sending PUBLISH packets
Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... ")); Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... "));
// topic filter: hello
state = wifi.unsubscribe("hello"); state = wifi.unsubscribe("hello");
if(state == ERR_NONE) { if(state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));

View file

@ -379,6 +379,7 @@ uint8_t MQTTClient::check(void (*func)(const char*, const char*)) {
} }
size_t MQTTClient::encodeLength(uint32_t len, uint8_t* encoded) { 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; size_t i = 0;
do { do {
encoded[i] = len % 128; 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) { 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 mult = 1;
uint32_t len = 0; uint32_t len = 0;
uint8_t i = 0; uint8_t i = 0;

View file

@ -29,8 +29,10 @@
class MQTTClient { class MQTTClient {
public: public:
// constructor
MQTTClient(TransportLayer* tl, uint16_t port = 1883); 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 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 disconnect();
uint8_t publish(const char* topic, const char* message); uint8_t publish(const char* topic, const char* message);