From 89fd8fb27967efedcc81120731db3c2e9ee09651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grome=C5=A1?= Date: Tue, 10 Jul 2018 15:46:09 +0200 Subject: [PATCH] ESP8266 - Added MQTT subscribe example --- .../ESP8266_MQTT_Subscribe.ino | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino diff --git a/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino new file mode 100644 index 00000000..9c046790 --- /dev/null +++ b/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino @@ -0,0 +1,70 @@ +/* + * KiteLib ESP8266 MQTT Publish Example + * + * This example publishes MQTT messages using ESP8266 WiFi module. + * + * The messages are published to https://shiftr.io/try. You can use this namespace + * for testing purposes, but remember that it is publicly accessible! + * + * IMPORTANT: Before upolading this example, make sure that the ESP8266 module is running + * AT firmware (can be found in the /extras folder of the library)! + */ + +// include the library +#include + +// ESP8266 module is in slot A on the shield +ESP8266 wifi = Kite.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize ESP8266 with baudrate 9600 + Serial.print(F("[ESP8266] Initializing ... ")); + byte state = wifi.begin(9600); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // join AP named "SSID" using the password "password" + Serial.print(F("[ESP8266] Joining AP ... ")); + state = wifi.join("SSID", "password"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } + + // connect to MQTT broker using client ID "arduino", username "try" and password "try" + Serial.print(F("[ESP8266] Connecting to MQTT broker ... ")); + state = wifi.MqttConnect("broker.shiftr.io", "arduino", "try", "try"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + while(true); + } +} + +void loop() { + // publish MQTT message to the topic "hello" with content "world" + Serial.print(F("[ESP8266] Publishing MQTT message ... ")); + byte state = wifi.MqttPublish("hello", "world"); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code 0x")); + Serial.println(state, HEX); + } + + // wait for a second before publishing again + delay(1000); +} +