Advertisement
Guest User

Untitled

a guest
Jun 4th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 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. #include <painlessMesh.h>
  29. #include "DHT.h"
  30.  
  31. #define LED 2 // GPIO number of connected LED, ON ESP-12 IS GPIO2
  32. #define DHTTYPE DHT22
  33. #define DHTPIN 2
  34. #define BLINK_PERIOD 3000 // milliseconds until cycle repeat
  35. #define BLINK_DURATION 100 // milliseconds LED is on for
  36. #define MESH_SSID "whateverYouLike"
  37. #define MESH_PASSWORD "somethingSneaky"
  38. #define MESH_PORT 5555
  39.  
  40.  
  41.  
  42. // Update these with values suitable for your network.
  43.  
  44. const char* ssid = "external.IOT";
  45. const char* password = "IoTlab32768";
  46. const char* mqtt_server = "157.158.56.57";
  47. const char* mqttUser = "iot";
  48. const char* mqttPassword = "iot";
  49.  
  50. WiFiClient espClient;
  51. PubSubClient client(espClient);
  52. long lastMsg = 0;
  53. char msg[50];
  54. int value = 0;
  55.  
  56. void setup() {
  57. pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  58. Serial.begin(115200);
  59. setup_wifi();
  60. client.setServer(mqtt_server, 1883);
  61. client.setCallback(callback);
  62. }
  63.  
  64. void setup_wifi() {
  65.  
  66. delay(10);
  67. // We start by connecting to a WiFi network
  68. Serial.println();
  69. Serial.print("Connecting to ");
  70. Serial.println(ssid);
  71.  
  72. WiFi.begin(ssid, password);
  73.  
  74. while (WiFi.status() != WL_CONNECTED) {
  75. delay(500);
  76. Serial.print(".");
  77. }
  78.  
  79. Serial.println("");
  80. Serial.println("WiFi connected");
  81. Serial.println("IP address: ");
  82. Serial.println(WiFi.localIP());
  83. }
  84.  
  85. void callback(char* topic, byte* payload, unsigned int length) {
  86. Serial.print("Message arrived [");
  87. Serial.print(topic);
  88. Serial.print("] ");
  89. for (int i = 0; i < length; i++) {
  90. Serial.print((char)payload[i]);
  91. }
  92. Serial.println();
  93.  
  94. // Switch on the LED if an 1 was received as first character
  95. if ((char)payload[0] == '1') {
  96. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  97. // but actually the LED is on; this is because
  98. // it is acive low on the ESP-01)
  99. } else {
  100. digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107.  
  108. void reconnect() {
  109. // Loop until we're reconnected
  110. while (!client.connected()) {
  111. Serial.print("Attempting MQTT connection...");
  112. // Attempt to connect
  113. if (client.connect("ESP8266Client",mqttUser, mqttPassword)) {
  114. Serial.println("connected");
  115. // Once connected, publish an announcement...
  116. client.publish("outTopic", "hello world");
  117. // ... and resubscribe
  118. client.subscribe("inTopic");
  119. } else {
  120. Serial.print("failed, rc=");
  121. Serial.print(client.state());
  122. Serial.println(" try again in 5 seconds");
  123. // Wait 5 seconds before retrying
  124. delay(5000);
  125. }
  126. }
  127. }
  128. void loop() {
  129.  
  130. if (!client.connected()) {
  131. reconnect();
  132. }
  133. client.loop();
  134.  
  135. float humidity = dht.readHumidity();
  136. float temperature = dht.readTemperature();
  137. String msg = "node id: ";
  138. long now = millis();
  139. if (now - lastMsg > 2000) {
  140. lastMsg = now;
  141. ++value;
  142. msg += "special node";
  143. msg += " temperature: ";
  144. msg += temperature;
  145. msg += "°C humidity: ";
  146. msg += humidity;
  147. msg += "% myFreeMemory: " + String(ESP.getFreeHeap());
  148. mesh.sendBroadcast(msg);
  149. snprintf (msg, 75, msg, "#%ld", value);
  150. Serial.print("Publish message: ");
  151. Serial.println(msg);
  152. client.publish("outTopic", msg);
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement