Advertisement
lukbe

ESP, Domoticz, SHT

Mar 5th, 2018
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. // cteni dat z SHT, odeslani na domoticz, spanek
  2.  
  3.  
  4. #include <ESP8266WiFi.h>
  5.  
  6. const int sleepTimeS = 10; // sleep time in minutes
  7. WiFiClient client;
  8. // domoticz
  9. const char * domoticz_server = "IP";
  10. int port = 8080; //Domoticz port
  11. int idx = 6; //IDX for virtual sensor, found in Setup -> Devices
  12.  
  13. //wifi
  14. #define AP_SSID "ssid"
  15. #define AP_PASSWORD "heslo"
  16. int wifi_connect = 0;
  17.  
  18. //SHT or Si7020 setup
  19. #if defined(ARDUINO)
  20. #include "Wire.h"
  21. #include "Si7020.h"
  22. #elif defined(SPARK)
  23. #include "Si7020/Si7020.h"
  24. #endif
  25. Si7020 sensor;
  26.  
  27. float hum = 0;
  28. float temp = 0;
  29.  
  30. void setup()
  31. {
  32. sensor.begin();
  33. Serial.begin(115200);
  34. wifiConnect();
  35. }
  36.  
  37. void loop()
  38. {
  39.  
  40. getData();
  41. senddata();
  42.  
  43. Serial.println("Jdu spat");
  44. delay(100);
  45. ESP.deepSleep(sleepTimeS * 1000000 * 60, WAKE_RF_DEFAULT);
  46. delay(100);
  47.  
  48. }
  49. void wifiConnect()
  50. {
  51. Serial.print("Connecting to: ");
  52. Serial.println(AP_SSID);
  53. WiFi.begin(AP_SSID, AP_PASSWORD);
  54.  
  55. while (WiFi.status() != WL_CONNECTED) {
  56. delay(1000);
  57. Serial.print(".");
  58.  
  59. wifi_connect++;
  60. Serial.print(wifi_connect);
  61. if (wifi_connect == 20) {
  62. wifi_connect = 0;
  63. Serial.println("nemuzu se pripojit jdu spat");
  64. ESP.deepSleep(30 * 1000000 * 60, WAKE_RF_DEFAULT);
  65. }
  66. }
  67. Serial.println("");
  68. Serial.println("WiFi connected");
  69. }
  70.  
  71. void getData()
  72. {
  73. hum = sensor.getRH();
  74. temp = sensor.getTemp();
  75.  
  76. Serial.println();
  77. Serial.println("**********************************************");
  78. Serial.println("Humidity (%)\tTemperature (C)");
  79. Serial.print("\t");
  80. Serial.print(hum, 1);
  81. Serial.print("\t\t");
  82. Serial.print(temp, 1);
  83. Serial.println("\t\t");
  84. Serial.println("**********************************************");
  85. Serial.println();
  86. }
  87.  
  88. void senddata()
  89. {
  90. // Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
  91.  
  92.  
  93. while (!client.connect(domoticz_server, port)) {
  94. Serial.println("connection failed");
  95. Serial.println("nemuzu se pripojit na domoticz jdu spat");
  96. ESP.deepSleep(30 * 1000000 * 60, WAKE_RF_DEFAULT);
  97. }
  98.  
  99. client.print("GET /json.htm?type=command&param=udevice&idx=");
  100. client.print(idx);
  101. client.print("&nvalue=0&svalue=");
  102. client.print(temp);
  103. client.print(";");
  104. client.print(hum);
  105. client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
  106. client.println(" HTTP/1.1");
  107. client.print("Host: ");
  108. client.print(domoticz_server);
  109. client.print(":");
  110. client.println(port);
  111. client.println("User-Agent: Arduino-ethernet");
  112. client.println("Connection: close");
  113. client.println();
  114.  
  115. delay(100);
  116. while (client.available()) {
  117. String line = client.readStringUntil('\r');
  118. Serial.print(line);
  119. }
  120.  
  121.  
  122. Serial.println("Odeslano na domoticz");
  123. delay(200);
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement