Advertisement
Guest User

Untitled

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