RadioLib/examples/ESP8266_MQTT_Subscribe/ESP8266_MQTT_Subscribe.ino

111 wiersze
3.4 KiB
Arduino
Czysty Zwykły widok Historia

2018-07-10 13:46:09 +00:00
/*
2018-07-11 11:44:43 +00:00
* KiteLib ESP8266 MQTT Subscribe Example
2018-07-10 13:46:09 +00:00
*
2018-07-11 11:44:43 +00:00
* This example subscribes to MQTT topic using ESP8266 WiFi module.
2018-07-10 13:46:09 +00:00
*
2018-07-11 11:44:43 +00:00
* The messages are pulled from https://shiftr.io/try. You can use this namespace
2018-07-10 13:46:09 +00:00
* for testing purposes, but remember that it is publicly accessible!
*
2018-07-11 11:44:43 +00:00
* IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
2018-07-10 13:46:09 +00:00
* AT firmware (can be found in the /extras folder of the library)!
*/
// include the library
#include <KiteLib.h>
// ESP8266 module is in slot A on the shield
ESP8266 wifi = Kite.ModuleA;
2018-07-11 11:44:43 +00:00
// create MQTT client instance using the wifi module
MQTTClient mqtt(&wifi);
2018-07-10 13:46:09 +00:00
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 ... "));
2018-07-11 11:44:43 +00:00
state = mqtt.connect("broker.shiftr.io", "arduino", "try", "try");
2018-07-10 13:46:09 +00:00
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
while(true);
}
2018-07-11 11:44:43 +00:00
// subscribe to the topic "hello"
// 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 ... "));
state = wifi.subscribe("hello");
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
}
// unsubscribe from topic "hello"
// after calling this method, server will stop sending PUBLISH packets
Serial.print(F("[ESP8266] Unsubscribing from MQTT topic ... "));
state = wifi.unsubscribe("hello");
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
}
}
// create a function that will be called when a new PUBLISH packet
// arrives from the server
//
// IMPORTANT: This function MUST have two C-strings as arguments!
void onPublish(const char* topic, const char* message) {
Serial.println("[ESP8266] Received packet from MQTT server: ");
Serial.print("[ESP8266] Topic:\t");
Serial.println(topic);
Serial.print("[ESP8266] Message:\t");
Serial.println(message);
2018-07-10 13:46:09 +00:00
}
void loop() {
2018-07-11 11:44:43 +00:00
// check for new MQTT packets from server each time the loop() runs
// this will also send a PING packet, restarting the keep alive timer
byte state = wifi.check(onPublish);
Serial.print("[ESP8266] MQTT check ");
2018-07-10 13:46:09 +00:00
if(state == ERR_NONE) {
Serial.println(F("success!"));
2018-07-11 11:44:43 +00:00
} else {
2018-07-10 13:46:09 +00:00
Serial.print(F("failed, code 0x"));
Serial.println(state, HEX);
}
2018-07-11 11:44:43 +00:00
// the rest of your loop() code goes here
// make sure that the maximum time the loop() runs is less than 1.5x keep alive,
// otherwise the server will close the network connection
2018-07-10 13:46:09 +00:00
}