Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 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 300
  21. #define ONE_WIRE_BUS 13 // DS18B20 pin
  22. // number of analog samples to take per reading voltage
  23. #define NUM_SAMPLES 10
  24.  
  25. const char* ssid = "Pihatalkoot";
  26. const char* password = "tasankolmetoi";
  27.  
  28. const char* mqtt_server = "192.168.0.162";
  29. const char* mqtt_username = "esp1";
  30. const char* mqtt_password = "esp1";
  31. const char* mqtt_topic = "oma/temperature/ulko";
  32. const char* mqtt_topic2 = "oma/voltage/ulko";
  33.  
  34. WiFiClient espClient;
  35. PubSubClient client(espClient);
  36.  
  37. OneWire oneWire(ONE_WIRE_BUS);
  38. DallasTemperature DS18B20(&oneWire);
  39.  
  40. char temperatureString[6];
  41. char voltageString[7];
  42.  
  43. void setup() {
  44. // setup serial port
  45. Serial.begin(115200);
  46.  
  47. // setup WiFi
  48. setup_wifi();
  49. client.setServer(mqtt_server, 8883);
  50. client.setCallback(callback);
  51.  
  52. // setup OneWire bus
  53. DS18B20.begin();
  54. }
  55.  
  56. void setup_wifi() {
  57. delay(10);
  58. // We start by connecting to a WiFi network
  59. Serial.println();
  60. Serial.print("Connecting to ");
  61. Serial.println(ssid);
  62.  
  63. WiFi.begin(ssid, password);
  64.  
  65. while (WiFi.status() != WL_CONNECTED) {
  66. delay(500);
  67. Serial.print(".");
  68. }
  69.  
  70. Serial.println("");
  71. Serial.println("WiFi connected");
  72. Serial.println("IP address: ");
  73. Serial.println(WiFi.localIP());
  74. }
  75.  
  76. void callback(char* topic, byte* payload, unsigned int length) {
  77. Serial.print("Message arrived [");
  78. Serial.print(topic);
  79. Serial.print("] ");
  80. for (int i = 0; i < length; i++) {
  81. Serial.print((char)payload[i]);
  82. }
  83. Serial.println();
  84. }
  85.  
  86. void reconnect() {
  87. // Loop until we're reconnected
  88. while (!client.connected()) {
  89. Serial.print("Attempting MQTT connection...");
  90. // Attempt to connect
  91. if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
  92. Serial.println("connected");
  93. } else {
  94. Serial.print("failed, rc=");
  95. Serial.print(client.state());
  96. Serial.println(" try again in 5 seconds");
  97. // Wait 5 seconds before retrying
  98. delay(5000);
  99. }
  100. }
  101. }
  102.  
  103. float getTemperature() {
  104. Serial << "Requesting DS18B20 temperature..." << endl;
  105. float temp;
  106. do {
  107. DS18B20.requestTemperatures();
  108. temp = DS18B20.getTempCByIndex(0);
  109. delay(100);
  110. } while (temp == 85.0 || temp == (-127.0));
  111. return temp;
  112. }
  113.  
  114. float getVoltage() {
  115. int sum = 0; // sum of samples taken
  116. unsigned char sample_count = 0; // current sample number
  117. float voltage = 0.0; // calculated voltage
  118. Serial << "Measuring voltage from adc-pin" << endl;
  119.  
  120.  
  121. // take a number of analog samples and add them up
  122. while (sample_count < NUM_SAMPLES) {
  123. sum += analogRead(A0);
  124. sample_count++;
  125. delay(10);
  126. }
  127. // calculate the voltage
  128. // use 5.0 for a 5.0V ADC reference voltage
  129. // 5.015V is the calibrated reference voltage
  130. voltage = ((float)sum / (float)NUM_SAMPLES * 5.015) / 1024.0;
  131. // send voltage for display on Serial Monitor
  132. // voltage multiplied by 11 when using voltage divider that
  133. // divides by 11. 11.132 is the calibrated voltage divide
  134. // value
  135.  
  136. Serial.print(voltage * 11.132);
  137. Serial.println (" V");
  138. sample_count = 0;
  139. sum = 0;
  140. }
  141.  
  142.  
  143. void loop() {
  144. if (!client.connected()) {
  145. reconnect();
  146. }
  147. client.loop();
  148. //get voltage
  149. float voltageadc = getVoltage();
  150. // convert voltage to a string with two digits before the comma and 2 digits for precision
  151. dtostrf(voltageadc, 2, 2, voltageString);
  152.  
  153. float temperature = getTemperature();
  154. // convert temperature to a string with two digits before the comma and 2 digits for precision
  155. dtostrf(temperature, 2, 2, temperatureString);
  156.  
  157. // send voltage to the serial console
  158. Serial << "Sending voltage: " << voltageadc << endl;
  159. // send voltage to the MQTT topic
  160. client.publish(mqtt_topic2, voltageString);
  161.  
  162. // send temperature to the MQTT serial console
  163. Serial << "Sending temperature: " << temperatureString << endl;
  164. // send temperature to the MQTT topic
  165. client.publish(mqtt_topic, temperatureString);
  166. Serial << "Closing MQTT connection..." << endl;
  167. client.disconnect();
  168.  
  169. Serial << "Closing WiFi connection..." << endl;
  170. WiFi.disconnect();
  171.  
  172. delay(100);
  173.  
  174. Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
  175. ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
  176. //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  177. delay(500); // wait for deep sleep to happen
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement