Fix esp example
This commit is contained in:
parent
461cbdb6e8
commit
aa9afc7b44
@ -1,22 +1,25 @@
|
|||||||
/*
|
/*
|
||||||
Basic ESP8266 MQTT example
|
Basic ESP8266 MQTT example
|
||||||
|
|
||||||
This sketch demonstrates the capabilities of the pubsub library in combination with the ESP8266 board/library.
|
This sketch demonstrates the capabilities of the pubsub library in combination
|
||||||
|
with the ESP8266 board/library.
|
||||||
|
|
||||||
It connects to an MQTT server then:
|
It connects to an MQTT server then:
|
||||||
- publishes "hello world" to the topic "outTopic"
|
- publishes "hello world" to the topic "outTopic" every two seconds
|
||||||
- subscribes to the topic "inTopic", printing out any messages
|
- subscribes to the topic "inTopic", printing out any messages
|
||||||
it receives. NB - it assumes the received payloads are strings not binary
|
it receives. NB - it assumes the received payloads are strings not binary
|
||||||
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, else switch it off
|
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
|
||||||
|
else switch it off
|
||||||
|
|
||||||
It will reconnect to the server if the connection is lost using a blocking
|
It will reconnect to the server if the connection is lost using a blocking
|
||||||
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
|
||||||
achieve the same result without blocking the main loop.
|
achieve the same result without blocking the main loop.
|
||||||
|
|
||||||
To install the ESP8266 board
|
To install the ESP8266 board, (using Arduino 1.6.4+):
|
||||||
Install Arduino 1.6.4+ (tested with Arduino 1.6.5)
|
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
|
||||||
json file from https://github.com/arduino/Arduino/wiki/Unofficial-list-of-3rd-party-boards-support-urls added in "File -> Preferences -> Additional Boards Manager URLs"
|
http://arduino.esp8266.com/stable/package_esp8266com_index.json
|
||||||
Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
|
- Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
|
||||||
Select your ESP8266 in "Tools -> Board"
|
- Select your ESP8266 in "Tools -> Board"
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -103,22 +106,21 @@ void reconnect() {
|
|||||||
delay(5000);
|
delay(5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
|
||||||
void loop() {
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
|
||||||
if (!client.connected()) {
|
long now = millis();
|
||||||
reconnect();
|
if (now - lastMsg > 2000) {
|
||||||
}
|
lastMsg = now;
|
||||||
client.loop();
|
++value;
|
||||||
|
snprintf (msg, 75, "hello world #%ld", value);
|
||||||
long now = millis();
|
Serial.print("Publish message: ");
|
||||||
if (now - lastMsg > 2000) {
|
Serial.println(msg);
|
||||||
lastMsg = now;
|
client.publish("outTopic", msg);
|
||||||
++value;
|
|
||||||
snprintf (msg, 75, "hello world #%ld", value);
|
|
||||||
Serial.print("Publish message: ");
|
|
||||||
Serial.println(msg);
|
|
||||||
client.publish("outTopic", msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user