Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 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 = "Nudelmastaren";
  32. const char* password = "nnln2691";
  33. const char* mqtt_server = "m23.cloudmqtt.com";
  34. const int mqttPort = 11662;
  35. const char* mqttUser = "hsxvflij";
  36. const char* mqttPassword = "Y-BaNkKyg-aM";
  37. int led = D5;
  38. int ledPinL = D1;
  39. int ledPinR = D2;
  40. int ledPinU = D3;
  41. int ledPinD = D4;
  42.  
  43. WiFiClient espClient;
  44. PubSubClient client(espClient);
  45. long lastMsg = 0;
  46. char msg[50];
  47. int value = 0;
  48. String msgRecived;
  49.  
  50.  
  51. void setup() {
  52. pinMode(led, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  53. pinMode(ledPinL, OUTPUT);
  54. pinMode(ledPinR, OUTPUT);
  55. pinMode(ledPinU, OUTPUT);
  56. pinMode(ledPinD, OUTPUT);
  57. Serial.begin(115200);
  58. setup_wifi();
  59. client.setServer(mqtt_server, mqttPort);
  60. client.setCallback(callback);
  61. }
  62.  
  63. void setup_wifi() {
  64.  
  65. delay(10);
  66. // We start by connecting to a WiFi network
  67. Serial.println();
  68. Serial.print("Connecting to ");
  69. Serial.println(ssid);
  70.  
  71. WiFi.begin(ssid, password);
  72.  
  73. while (WiFi.status() != WL_CONNECTED) {
  74. delay(500);
  75. Serial.print(".");
  76. }
  77.  
  78. Serial.println("");
  79. Serial.println("WiFi connected");
  80. Serial.println("IP address: ");
  81. Serial.println(WiFi.localIP());
  82. }
  83.  
  84. void callback(char* topic, byte* payload, unsigned int length) {
  85. Serial.print("Message arrived [");
  86. Serial.print(topic);
  87. Serial.print("] ");
  88. for (int i = 0; i < length; i++) {
  89. Serial.print((char)payload[i]);
  90. }
  91. Serial.println();
  92.  
  93. // Switch on the LED if an 1 was received as first character
  94. if ((char)payload[0] == 'L') {
  95. digitalWrite(led, HIGH); // Turn the LED on (Note that LOW is the voltage level
  96. // but actually the LED is on; this is because
  97. // it is acive low on the ESP-01)
  98. delay(100); // wait for a second
  99. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  100. delay(100); // wait for a second
  101. digitalWrite(ledPinL, HIGH); // turn the LED on (HIGH is the voltage level)
  102. delay(100); // wait for a second
  103. digitalWrite(ledPinL, LOW); // turn the LED off by making the voltage LOW
  104. delay(200);
  105. } else {
  106. digitalWrite(ledPinL, LOW); // Turn the LED off by making the voltage HIGH
  107. }
  108. if ((char)payload[0] == 'R') {
  109. digitalWrite(led, HIGH); // Turn the LED on (Note that LOW is the voltage level
  110. // but actually the LED is on; this is because
  111. // it is acive low on the ESP-01)
  112. delay(100); // wait for a second
  113. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  114. delay(100); // wait for a second
  115. digitalWrite(ledPinR, HIGH); // turn the LED on (HIGH is the voltage level)
  116. delay(100); // wait for a second
  117. digitalWrite(ledPinR, LOW); // turn the LED off by making the voltage LOW
  118. delay(200);
  119. } else {
  120. digitalWrite(ledPinR, LOW); // Turn the LED off by making the voltage HIGH
  121. }
  122. if ((char)payload[0] == 'U') {
  123. digitalWrite(led, HIGH); // Turn the LED on (Note that LOW is the voltage level
  124. // but actually the LED is on; this is because
  125. // it is acive low on the ESP-01)
  126. delay(100); // wait for a second
  127. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  128. delay(100); // wait for a second
  129. digitalWrite(ledPinU, HIGH); // turn the LED on (HIGH is the voltage level)
  130. delay(100); // wait for a second
  131. digitalWrite(ledPinU, LOW); // turn the LED off by making the voltage LOW
  132. delay(200);
  133. } else {
  134. digitalWrite(ledPinU, LOW); // Turn the LED off by making the voltage HIGH
  135. }
  136. if ((char)payload[0] == 'D') {
  137. digitalWrite(ledPinD, HIGH); // Turn the LED on (Note that LOW is the voltage level
  138. digitalWrite(led, HIGH); // Turn the LED on (Note that LOW is the voltage level
  139. // but actually the LED is on; this is because
  140. // it is acive low on the ESP-01)
  141. delay(100); // wait for a second
  142. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  143. delay(100); // wait for a second
  144. digitalWrite(ledPinD, HIGH); // turn the LED on (HIGH is the voltage level)
  145. delay(100); // wait for a second
  146. digitalWrite(ledPinD, LOW); // turn the LED off by making the voltage LOW
  147. delay(200);
  148. } else {
  149. digitalWrite(ledPinD, LOW); // Turn the LED off by making the voltage HIGH
  150. }
  151. }
  152.  
  153. void reconnect() {
  154. // Loop until we're reconnected
  155. while (!client.connected()) {
  156. Serial.print("Attempting MQTT connection...");
  157. // Attempt to connect
  158. if (client.connect("ESP8266Client", mqttUser, mqttPassword)) {
  159. Serial.println("connected");
  160. // Once connected, publish an announcement...
  161. client.publish("iotandroid", "hello world");
  162. // ... and resubscribe
  163. client.subscribe("iotandroid");
  164. } else {
  165. Serial.print("failed, rc=");
  166. Serial.print(client.state());
  167. Serial.println(" try again in 5 seconds");
  168. // Wait 5 seconds before retrying
  169. delay(5000);
  170. }
  171. }
  172. }
  173. void loop() {
  174.  
  175. if (!client.connected()) {
  176. reconnect();
  177. }
  178. client.loop();
  179.  
  180. long now = millis();
  181. if (now - lastMsg > 2000) {
  182. lastMsg = now;
  183. ++value;
  184. snprintf (msg, 75, "hello world #%ld", value);
  185. Serial.print("Publish message: ");
  186. Serial.println(msg);
  187. client.publish("iotandroid", msg);
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement