Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1.  
  2. #include <ESP8266WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include <Streaming.h>
  7.  
  8. #define SLEEP_DELAY_IN_SECONDS 30
  9. #define ONE_WIRE_BUS D5 // DS18B20 pin
  10.  
  11. const char* ssid = "xxxxx";
  12. const char* password = "xxxxxx";
  13.  
  14. const char* mqtt_server = "192.168.2.113";
  15. const char* mqtt_username = "xxx";
  16. const char* mqtt_password = "xxx";
  17. const char* mqtt_topic = "ha/test/test";
  18. float temperature;
  19.  
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22.  
  23. OneWire oneWire(ONE_WIRE_BUS);
  24. DallasTemperature DS18B20(&oneWire);
  25.  
  26. char temperatureString[6];
  27.  
  28. void setup() {
  29. // setup serial port
  30. Serial.begin(115200);
  31.  
  32. // setup WiFi
  33. setup_wifi();
  34. client.setServer(mqtt_server, 1883);
  35. client.setCallback(callback);
  36.  
  37. // setup OneWire bus
  38. DS18B20.begin();
  39. }
  40.  
  41. void setup_wifi() {
  42. delay(10);
  43. // We start by connecting to a WiFi network
  44. Serial.println();
  45. Serial.print("Connecting to ");
  46. Serial.println(ssid);
  47.  
  48. WiFi.begin(ssid, password);
  49.  
  50. while (WiFi.status() != WL_CONNECTED) {
  51. delay(500);
  52. Serial.print(".");
  53. }
  54.  
  55. Serial.println("");
  56. Serial.println("WiFi connected");
  57. Serial.println("IP address: ");
  58. Serial.println(WiFi.localIP());
  59. }
  60.  
  61. void callback(char* topic, byte* payload, unsigned int length) {
  62. Serial.print("Message arrived [");
  63. Serial.print(topic);
  64. Serial.print("] ");
  65. for (int i = 0; i < length; i++) {
  66. Serial.print((char)payload[i]);
  67. }
  68. Serial.println();
  69. }
  70.  
  71. void reconnect() {
  72. // Loop until we're reconnected
  73. while (!client.connected()) {
  74. Serial.print("Attempting MQTT connection...");
  75. // Attempt to connect
  76. if (client.connect("ESP8266Client", mqtt_username, mqtt_password)) {
  77. Serial.println("connected");
  78. } else {
  79. Serial.print("failed, rc=");
  80. Serial.print(client.state());
  81. Serial.println(" try again in 5 seconds");
  82. // Wait 5 seconds before retrying
  83. delay(5000);
  84. }
  85. }
  86. }
  87.  
  88. float getTemperature() {
  89.  
  90. Serial << "Hämtar temperatur.." << endl;
  91. float temp;
  92. DS18B20.requestTemperatures();
  93. temp = DS18B20.getTempCByIndex(0);
  94. Serial.print(temp);
  95. return temp;
  96.  
  97. }
  98.  
  99. void loop() {
  100. if (!client.connected()) {
  101. reconnect();
  102. }
  103. client.loop();
  104. client.publish(mqtt_topic, "temp2");
  105. temperature = getTemperature();
  106. client.publish(mqtt_topic, "temp3");
  107. client.publish(mqtt_topic, "temp4");
  108.  
  109. // konvertera till rätt format
  110. dtostrf(temperature, 2, 2, temperatureString);
  111.  
  112.  
  113. Serial << "Skickar temperatur: " << temperatureString << endl;
  114. client.publish(mqtt_topic, temperatureString);
  115.  
  116. // Serial << "Closing MQTT connection..." << endl;
  117. client.disconnect();
  118.  
  119. // Serial << "Closing WiFi connection..." << endl;
  120. WiFi.disconnect();
  121.  
  122. delay(100);
  123.  
  124. Serial << "Entering deep sleep mode for " << SLEEP_DELAY_IN_SECONDS << " seconds..." << endl;
  125. ESP.deepSleep(SLEEP_DELAY_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
  126. //ESP.deepSleep(10 * 1000, WAKE_NO_RFCAL);
  127. delay(500); // wait for deep sleep to happen
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement