mqtt_esp8266.ino 3.5 KB

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