1 | /*
|
---|
2 | Basic ESP8266 MQTT example
|
---|
3 |
|
---|
4 | This sketch demonstrates the capabilities of the pubsub library in combination
|
---|
5 | with the ESP8266 board/library.
|
---|
6 |
|
---|
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,
|
---|
12 | else switch it off
|
---|
13 |
|
---|
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.
|
---|
17 |
|
---|
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"
|
---|
23 |
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <ESP8266WiFi.h>
|
---|
27 | #include <PubSubClient.h>
|
---|
28 |
|
---|
29 | // Update these with values suitable for your network.
|
---|
30 |
|
---|
31 | const char* ssid = "........";
|
---|
32 | const char* password = "........";
|
---|
33 | const char* mqtt_server = "broker.mqtt-dashboard.com";
|
---|
34 |
|
---|
35 | WiFiClient espClient;
|
---|
36 | PubSubClient client(espClient);
|
---|
37 | long lastMsg = 0;
|
---|
38 | char msg[50];
|
---|
39 | int value = 0;
|
---|
40 |
|
---|
41 | void setup() {
|
---|
42 | pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
---|
43 | Serial.begin(115200);
|
---|
44 | setup_wifi();
|
---|
45 | client.setServer(mqtt_server, 1883);
|
---|
46 | client.setCallback(callback);
|
---|
47 | }
|
---|
48 |
|
---|
49 | void setup_wifi() {
|
---|
50 |
|
---|
51 | delay(10);
|
---|
52 | // We start by connecting to a WiFi network
|
---|
53 | Serial.println();
|
---|
54 | Serial.print("Connecting to ");
|
---|
55 | Serial.println(ssid);
|
---|
56 |
|
---|
57 | WiFi.begin(ssid, password);
|
---|
58 |
|
---|
59 | while (WiFi.status() != WL_CONNECTED) {
|
---|
60 | delay(500);
|
---|
61 | Serial.print(".");
|
---|
62 | }
|
---|
63 |
|
---|
64 | Serial.println("");
|
---|
65 | Serial.println("WiFi connected");
|
---|
66 | Serial.println("IP address: ");
|
---|
67 | Serial.println(WiFi.localIP());
|
---|
68 | }
|
---|
69 |
|
---|
70 | void callback(char* topic, byte* payload, unsigned int length) {
|
---|
71 | Serial.print("Message arrived [");
|
---|
72 | Serial.print(topic);
|
---|
73 | Serial.print("] ");
|
---|
74 | for (int i = 0; i < length; i++) {
|
---|
75 | Serial.print((char)payload[i]);
|
---|
76 | }
|
---|
77 | Serial.println();
|
---|
78 |
|
---|
79 | // Switch on the LED if an 1 was received as first character
|
---|
80 | if ((char)payload[0] == '1') {
|
---|
81 | digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
|
---|
82 | // but actually the LED is on; this is because
|
---|
83 | // it is acive low on the ESP-01)
|
---|
84 | } else {
|
---|
85 | digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | void reconnect() {
|
---|
91 | // Loop until we're reconnected
|
---|
92 | while (!client.connected()) {
|
---|
93 | Serial.print("Attempting MQTT connection...");
|
---|
94 | // Attempt to connect
|
---|
95 | if (client.connect("ESP8266Client")) {
|
---|
96 | Serial.println("connected");
|
---|
97 | // Once connected, publish an announcement...
|
---|
98 | client.publish("outTopic", "hello world");
|
---|
99 | // ... and resubscribe
|
---|
100 | client.subscribe("inTopic");
|
---|
101 | } else {
|
---|
102 | Serial.print("failed, rc=");
|
---|
103 | Serial.print(client.state());
|
---|
104 | Serial.println(" try again in 5 seconds");
|
---|
105 | // Wait 5 seconds before retrying
|
---|
106 | delay(5000);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | void loop() {
|
---|
111 |
|
---|
112 | if (!client.connected()) {
|
---|
113 | reconnect();
|
---|
114 | }
|
---|
115 | client.loop();
|
---|
116 |
|
---|
117 | long now = millis();
|
---|
118 | if (now - lastMsg > 2000) {
|
---|
119 | lastMsg = now;
|
---|
120 | ++value;
|
---|
121 | snprintf (msg, 75, "hello world #%ld", value);
|
---|
122 | Serial.print("Publish message: ");
|
---|
123 | Serial.println(msg);
|
---|
124 | client.publish("outTopic", msg);
|
---|
125 | }
|
---|
126 | }
|
---|