Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  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 = "mqtt://m20.cloudmqtt.com";
  34. const char* user = "vqdhgxdd";
  35. const char* pass = "0zyRZ9ySe5Cn";
  36.  
  37. WiFiClient espClient;
  38. PubSubClient client(espClient);
  39. long lastMsg = 0;
  40. char msg[50];
  41. int value = 0;
  42.  
  43. void setup_wifi() {
  44.  
  45. delay(10);
  46. // We start by connecting to a WiFi network
  47. Serial.println();
  48. Serial.print("Connecting to ");
  49. Serial.println(ssid);
  50.  
  51. WiFi.begin(ssid, password);
  52.  
  53. while (WiFi.status() != WL_CONNECTED) {
  54. delay(500);
  55. Serial.print(".");
  56. }
  57.  
  58. randomSeed(micros());
  59.  
  60. Serial.println("");
  61. Serial.println("WiFi connected");
  62. Serial.println("IP address: ");
  63. Serial.println(WiFi.localIP());
  64. }
  65.  
  66. void callback(char* topic, byte* payload, unsigned int length) {
  67. Serial.print("Message arrived [");
  68. Serial.print(topic);
  69. Serial.print("] ");
  70. for (int i = 0; i < length; i++) {
  71. Serial.print((char)payload[i]);
  72. }
  73. Serial.println();
  74.  
  75. // Switch on the LED if an 1 was received as first character
  76. if ((char)payload[0] == '1') {
  77. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  78. // but actually the LED is on; this is because
  79. // it is active low on the ESP-01)
  80. } else {
  81. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  82. }
  83.  
  84. }
  85.  
  86. void reconnect() {
  87. // Loop until we're reconnected
  88. while (!client.connected()) {
  89. Serial.print("Attempting MQTT connection...");
  90. // Create a random client ID
  91. String clientId = "ESP8266Client-";
  92. clientId += String(random(0xffff), HEX);
  93. // Attempt to connect
  94. if (client.connect(clientId.c_str()), user, pass) {
  95. Serial.println("connected");
  96. // Once connected, publish an announcement...
  97. client.publish("outTopic", "hello world");
  98. // ... and resubscribe
  99. client.subscribe("inTopic");
  100. } else {
  101. Serial.print("failed, rc=");
  102. Serial.print(client.state());
  103. Serial.println(" try again in 5 seconds");
  104. // Wait 5 seconds before retrying
  105. delay(5000);
  106. }
  107. }
  108. }
  109.  
  110. void setup() {
  111. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  112. Serial.begin(115200);
  113. setup_wifi();
  114. client.setServer(mqtt_server, 1883);
  115. client.setCallback(callback);
  116. }
  117.  
  118. void loop() {
  119.  
  120. if (!client.connected()) {
  121. reconnect();
  122. }
  123. client.loop();
  124.  
  125. long now = millis();
  126. if (now - lastMsg > 2000) {
  127. lastMsg = now;
  128. ++value;
  129. snprintf (msg, 50, "hello world #%ld", value);
  130. Serial.print("Publish message: ");
  131. Serial.println(msg);
  132. client.publish("outTopic", msg);
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement