Guest User

test

a guest
Feb 10th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 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.  
  21. #include <Adafruit_NeoPixel.h>
  22. #ifdef __AVR__
  23. #include <avr/power.h>
  24. #endif
  25.  
  26. #define SLEEP_DELAY_IN_SECONDS 120
  27. #define ONE_WIRE_BUS D4 // DS18B20 pin
  28. #define PIN 2
  29. const char* ssid = "Time";
  30. const char* password = "motocarro";
  31.  
  32. //const char* ssid = "sfy";
  33. //const char* password = "WifiSoftForYou17";
  34.  
  35. const char* mqtt_server = "mqtt.thingspeak.com";
  36. const char* mqtt_username = "joaquinsfy@gmail.com";
  37. const char* mqtt_password = "Motocarro13";
  38. const char* mqtt_topic = "sensors/test/temperature";
  39.  
  40. WiFiClient espClient;
  41. PubSubClient client(espClient);
  42.  
  43. OneWire oneWire(ONE_WIRE_BUS);
  44. DallasTemperature DS18B20(&oneWire);
  45.  
  46. char temperatureString[6];
  47.  
  48. void setup() {
  49. // setup serial port
  50. Serial.begin(115200);
  51. // setup WiFi
  52. setup_wifi();
  53. client.setServer(mqtt_server, 1883);
  54. client.setCallback(callback);
  55. // setup OneWire bus
  56. DS18B20.begin();
  57. }
  58.  
  59. void setup_wifi() {
  60. Serial.println();
  61. Serial.print("Connecting to ");
  62. Serial.println(ssid);
  63. WiFi.begin(ssid, password);
  64. while (WiFi.status() != WL_CONNECTED ) {
  65. Serial.println(".");
  66. delay(500);
  67. }
  68. Serial.println("");
  69. Serial.println("WiFi connected");
  70. Serial.println("IP address: ");
  71. Serial.println(WiFi.localIP());
  72. }
  73.  
  74. void callback(char* topic, byte* payload, unsigned int length) {
  75. Serial.print("Message arrived [");
  76. Serial.print(topic);
  77. Serial.print("] ");
  78. for (int i = 0; i < length; i++) {
  79. Serial.print((char)payload[i]);
  80. }
  81. Serial.println();
  82. }
  83.  
  84. void reconnect() {
  85. // Loop until we're reconnected
  86. while (!client.connected()) {
  87. Serial.print("Attempting MQTT connection...");
  88. // Attempt to connect
  89. if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
  90. Serial.println("connected");
  91. } else {
  92. Serial.print("failed, rc=");
  93. Serial.print(client.state());
  94. Serial.println(" try again in 5 seconds");
  95. // Wait 5 seconds before retrying
  96. delay(5000);
  97. }
  98. }
  99. }
  100.  
  101. float getTemperature(unsigned int sensor) {
  102. Serial << "Requesting DS18B20 temperature..." << endl;
  103. float temp;
  104. do {
  105. DS18B20.requestTemperatures();
  106. temp = DS18B20.getTempCByIndex(sensor);
  107. delay(100);
  108. } while (temp == 85.0 || temp == (-127.0));
  109.  
  110. return temp;
  111. }
  112.  
  113. void loop()
  114. {
  115. enviatemperatura1();
  116. Serial << "Closing WiFi connection..." << endl;
  117. WiFi.disconnect();
  118.  
  119. Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
  120. ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
  121. //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  122. delay(500); // wait for deep sleep to happen
  123. }
  124.  
  125. void enviatemperatura1()
  126. { if (!client.connected()) {
  127. reconnect();
  128. }
  129. client.loop();
  130.  
  131. // temperatura 1
  132. float temperature = getTemperature(0);
  133. // convert temperature to a string with two digits before the comma and 2 digits for precision
  134. dtostrf(temperature, 2, 2, temperatureString);
  135. // send temperature to the serial console
  136. Serial << "Sending temperature: " << temperatureString << endl;
  137. // send temperature to the MQTT topic
  138. String data = String(temperature, DEC);
  139. // Get the data string length
  140. int length = data.length();
  141. char msgBuffer[length];
  142. data.toCharArray(msgBuffer, length + 1);
  143. Serial.println(msgBuffer);
  144. // Publish data to ThingSpeak. Replace <YOUR-CHANNEL-ID> with your channel ID and <YOUR-CHANNEL-WRITEAPIKEY> with your write API key
  145. client.publish("channels/218099/publish/fields/field3/P0FMMIR5ADIW03HO", msgBuffer);
  146.  
  147. Serial << "Closing MQTT connection..." << endl;
  148. client.disconnect();
  149.  
  150. }
Add Comment
Please, Sign In to add comment