Guest User

Untitled

a guest
Jan 10th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /*
  2. Sketch which publishes temperature data from a DS1820 sensor to a MQTT topic.
  3. This sketch goes in deep sleep mode once the temperature has been sent to the MQTT
  4. topic and wakes up periodically (configure SLEEP_DELAY_IN_SECONDS accordingly).
  5. Hookup guide:
  6. - connect D0 pin to RST pin in order to enable the ESP8266 to wake up periodically
  7. - DS18B20:
  8. + connect VCC (3.3V) to the appropriate DS18B20 pin (VDD)
  9. + connect GND to the appopriate DS18B20 pin (GND)
  10. + connect D4 to the DS18B20 data pin (DQ)
  11. + connect a 4.7K resistor between DQ and VCC.
  12. */
  13.  
  14. #include <ESP8266WiFi.h>
  15. #include <PubSubClient.h>
  16. #include <OneWire.h>
  17. #include <DallasTemperature.h>
  18. #include <Streaming.h>
  19.  
  20. #define SLEEP_DELAY_IN_SECONDS 30
  21. #define ONE_WIRE_BUS D4 // DS18B20 pin
  22.  
  23. const char* ssid = "<YOUR_WIFI_NETWORK>";
  24. const char* password = "<YOUR_WIFI_PASSWORD>";
  25.  
  26. const char* mqtt_server = "<MQTT_BROKER_IP_ADDRESS>";
  27. const char* mqtt_username = "<MQTT_BROKER_USERNAME>";
  28. const char* mqtt_password = "<MQTT_BROKER_PASSWORD>";
  29. const char* mqtt_topic = "sensors/test/temperature";
  30.  
  31. WiFiClient espClient;
  32. PubSubClient client(espClient);
  33.  
  34. OneWire oneWire(ONE_WIRE_BUS);
  35. DallasTemperature DS18B20(&oneWire);
  36.  
  37. char temperatureString[6];
  38.  
  39. void setup() {
  40. // setup serial port
  41. Serial.begin(115200);
  42.  
  43. // setup WiFi
  44. setup_wifi();
  45. client.setServer(mqtt_server, 1883);
  46. client.setCallback(callback);
  47.  
  48. // setup OneWire bus
  49. DS18B20.begin();
  50. }
  51.  
  52. void setup_wifi() {
  53. delay(10);
  54. // We start by connecting to a WiFi network
  55. Serial.println();
  56. Serial.print("Connecting to ");
  57. Serial.println(ssid);
  58.  
  59. WiFi.begin(ssid, password);
  60.  
  61. while (WiFi.status() != WL_CONNECTED) {
  62. delay(500);
  63. Serial.print(".");
  64. }
  65.  
  66. Serial.println("");
  67. Serial.println("WiFi connected");
  68. Serial.println("IP address: ");
  69. Serial.println(WiFi.localIP());
  70. }
  71.  
  72. void callback(char* topic, byte* payload, unsigned int length) {
  73. Serial.print("Message arrived [");
  74. Serial.print(topic);
  75. Serial.print("] ");
  76. for (int i = 0; i < length; i++) {
  77. Serial.print((char)payload[i]);
  78. }
  79. Serial.println();
  80. }
  81.  
  82. void reconnect() {
  83. // Loop until we're reconnected
  84. while (!client.connected()) {
  85. Serial.print("Attempting MQTT connection...");
  86. // Attempt to connect
  87. if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
  88. Serial.println("connected");
  89. } else {
  90. Serial.print("failed, rc=");
  91. Serial.print(client.state());
  92. Serial.println(" try again in 5 seconds");
  93. // Wait 5 seconds before retrying
  94. delay(5000);
  95. }
  96. }
  97. }
  98.  
  99. float getTemperature() {
  100. Serial << "Requesting DS18B20 temperature..." << endl;
  101. float temp;
  102. do {
  103. DS18B20.requestTemperatures();
  104. temp = DS18B20.getTempCByIndex(0);
  105. delay(100);
  106. } while (temp == 85.0 || temp == (-127.0));
  107. return temp;
  108. }
  109.  
  110. void loop() {
  111. if (!client.connected()) {
  112. reconnect();
  113. }
  114. client.loop();
  115.  
  116. float temperature = getTemperature();
  117. // convert temperature to a string with two digits before the comma and 2 digits for precision
  118. dtostrf(temperature, 2, 2, temperatureString);
  119. // send temperature to the serial console
  120. Serial << "Sending temperature: " << temperatureString << endl;
  121. // send temperature to the MQTT topic
  122. client.publish(mqtt_topic, temperatureString);
  123.  
  124. Serial << "Closing MQTT connection..." << endl;
  125. client.disconnect();
  126.  
  127. Serial << "Closing WiFi connection..." << endl;
  128. WiFi.disconnect();
  129.  
  130. delay(100);
  131.  
  132. Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
  133. ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
  134. //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  135. delay(500); // wait for deep sleep to happen
  136. }
Add Comment
Please, Sign In to add comment