2 Basic ESP8266 MQTT example
4 This sketch demonstrates the capabilities of the pubsub library in combination
5 with the ESP8266 board/library.
7 It connects to an MQTT server then:
8 - publishes "hello world" to the topic "outTopic" every two seconds
9 - subscribes to the topic "inTopic", printing out any messages
10 it receives. NB - it assumes the received payloads are strings not binary
11 - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
14 It will reconnect to the server if the connection is lost using a blocking
15 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
16 achieve the same result without blocking the main loop.
18 To install the ESP8266 board, (using Arduino 1.6.4+):
19 - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
20 http://arduino.esp8266.com/stable/package_esp8266com_index.json
21 - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
22 - Select your ESP8266 in "Tools -> Board"
26 #include <ESP8266WiFi.h>
27 #include <PubSubClient.h>
29 // Update these with values suitable for your network.
31 //const char* ssid = "........";
32 //const char* password = "........";
33 const char* mqtt_server = "chrome";
34 const char* input = "inTopic" ;
35 const char* output = "outTopic" ;
38 PubSubClient client(espClient);
44 pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
47 client.setServer(mqtt_server, 1883);
48 client.setCallback(callback);
54 // We start by connecting to a WiFi network
56 Serial.print("Connecting to ");
59 WiFi.begin(ssid, password);
61 while (WiFi.status() != WL_CONNECTED) {
67 Serial.println("WiFi connected");
68 Serial.println("IP address: ");
69 Serial.println(WiFi.localIP());
72 void callback(char* topic, byte* payload, unsigned int length) {
73 Serial.print("Message arrived [");
76 for (int i = 0; i < length; i++) {
77 Serial.print((char)payload[i]);
81 // Switch on the LED if an 1 was received as first character
82 if ((char)payload[0] == '1') {
83 digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
84 // but actually the LED is on; this is because
85 // it is acive low on the ESP-01)
87 digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
93 // Loop until we're reconnected
94 while (!client.connected()) {
95 Serial.print("Attempting MQTT connection...");
97 if (client.connect("ESP8266Client")) {
98 Serial.println("connected");
99 // Once connected, publish an announcement...
100 client.publish(output, "hello world");
101 // ... and resubscribe
102 client.subscribe(input);
104 Serial.print("failed, rc=");
105 Serial.print(client.state());
106 Serial.println(" try again in 5 seconds");
107 // Wait 5 seconds before retrying
114 if (!client.connected()) {
120 if (now - lastMsg > 2000) {
123 snprintf (msg, 75, "hello world #%ld", value);
124 Serial.print("Publish message: ");
126 client.publish(output, msg);