Advertisement
Guest User

Untitled

a guest
May 26th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include "DHT.h"
  3. #include <Adafruit_BMP085.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>
  6.  
  7. LiquidCrystal_I2C lcd(0x27, 20, 4);
  8. #define pinDHT D13
  9. #define typDHT22 DHT22 // DHT 22 (AM2302)
  10. DHT mojeDHT(pinDHT, typDHT22);
  11. const int myPeriodic = 300; // Time to sleep (in seconds):
  12. Adafruit_BMP085 bmp;
  13. // Nazev Wi-Fi site, do ktere se mam pripojit
  14. const char* ssid = "Kony";
  15. // Heslo Wi-Fi site, do ktere se mam pripojit
  16. const char* password = "dfgdfgdfg";
  17. char server[] = "dfgdfgdfgfdg"; //URL adresa serveru
  18. WiFiClient client;
  19. unsigned long cas = 0;
  20. String cas_aktualni;
  21. String den;
  22. float tep;
  23. float vlh;
  24. float tlak_aktualni;
  25. unsigned long aktualniMillis1; //aktualni cas
  26. unsigned long predchoziMillis1; //cas poseldni akce
  27. unsigned long aktualniMillis5; //aktualni cas
  28. unsigned long predchoziMillis5; //cas poseldni akce
  29.  
  30. void setup() {
  31. lcd.begin();
  32. lcd.backlight();
  33. // komunikace přes sériovou linku rychlostí 9600 baud
  34. //Serial.begin(9600);
  35. // zapnutí komunikace s teploměrem DHT
  36. mojeDHT.begin();
  37. if (!bmp.begin()) {
  38. Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
  39. while (1) {}
  40. }
  41. Serial.println();
  42. Serial.print("Pripojuji k ");
  43. Serial.println(ssid);
  44. WiFi.begin(ssid, password);
  45.  
  46. // Dokud nejsem pripojeny k Wi-Fi,zapisuj do seriove linky tecky progressbaru
  47. while (WiFi.status() != WL_CONNECTED) {
  48. delay(100);
  49. Serial.print(".");
  50. lcd.setCursor ( 0, 0 );
  51. lcd.print(".");
  52. }
  53.  
  54. // Jsem pripojeny k Wi-Fi a mohu pokracovat
  55. Serial.println();
  56. Serial.println("WiFi pripojena!");
  57. lcd.setCursor ( 0, 1 );
  58. lcd.print("Wifi Pripojena!");
  59.  
  60. // Napis IP adresu, kterou mikropocitac dostal
  61. Serial.print("Pouzij k pripojeni tuto adresu: ");
  62. Serial.print("http://");
  63. Serial.println(WiFi.localIP());
  64. lcd.setCursor ( 0, 2 );
  65. lcd.print(WiFi.localIP());
  66. delay(2000);
  67. lcd.clear();
  68. }
  69.  
  70. void teplota_vlhkost() {
  71. tep = mojeDHT.readTemperature();
  72. delay(2000);
  73. vlh = mojeDHT.readHumidity();
  74. // kontrola, jestli jsou načtené hodnoty čísla pomocí funkce isnan
  75. if (isnan(tep) || isnan(vlh)) {
  76. // při chybném čtení vypiš hlášku
  77. Serial.println("Chyba při čtení z DHT senzoru!");
  78. } else {
  79. // pokud jsou hodnoty v pořádku,
  80. // vypiš je po sériové lince
  81. Serial.print("Teplota: ");
  82. Serial.print(tep,1);
  83. Serial.println(" °C");
  84. Serial.print("Vlhkost: ");
  85. Serial.print(vlh,0);
  86. Serial.println(" %");
  87. lcd.setCursor ( 1, 1 );
  88. lcd.print("Teplota : ");
  89. lcd.setCursor ( 11, 1 );
  90. lcd.print(" ");
  91. lcd.setCursor ( 11, 1 );
  92. lcd.print(tep,1);
  93. lcd.print(" ");
  94. lcd.print((char)223);
  95. lcd.print("C");
  96. lcd.setCursor ( 1,2 );
  97. lcd.print("Vlhkost : ");
  98. lcd.setCursor ( 11,2 );
  99. lcd.print(vlh,0);
  100. lcd.print(F(" %"));
  101. }
  102. // pauza pro přehlednější výpis
  103. delay(2000);
  104. }
  105.  
  106. void tlak() {
  107. tlak_aktualni = (bmp.readPressure()/100)+38;
  108. Serial.print("Tlak : ");
  109. Serial.print(tlak_aktualni,0);
  110. Serial.println(" hPa");
  111. lcd.setCursor ( 2,3 );
  112. lcd.print("Tlak :");
  113. lcd.setCursor ( 11,3 );
  114. lcd.print(tlak_aktualni,0);
  115. lcd.print(F(" hPa"));
  116. }
  117.  
  118. void hodiny() {
  119. if (millis() - cas >= 6000 || cas == 0) {
  120. cas = millis();
  121. if (client.connect(server, 80)) { //starts client connection, checks for connection
  122. client.println("GET http://fdgdfgdfg/cas.php");
  123. client.println("Host: fdgdfgfdgfdg");
  124. client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  125. client.println("Connection: close");
  126. client.println();
  127. delay(1000);
  128. while (client.connected()) {
  129. String line = client.readStringUntil('\n');
  130. // Serial.println(line); //ak chceme vypísať HTTP header
  131. if (line == "\r") {
  132. break;
  133. }
  134. }
  135. String line = client.readStringUntil('\n');
  136. // Serial.println("Nacitany payload response: Cas - ");
  137. // Serial.println(line); //odpoveď webservera - naše dáta
  138. cas_aktualni = line;
  139. }
  140. else {
  141. Serial.println("Pripojenie na webserver sa nepodarilo");
  142. }
  143. client.stop();
  144. }
  145. Serial.print("Cas : ");
  146. Serial.println(cas_aktualni);
  147. lcd.setCursor ( 15,0 );
  148. lcd.print(cas_aktualni);
  149. }
  150.  
  151. void den_tydnu() {
  152. if (millis() - cas >= 6000 || cas == 0) {
  153. cas = millis();
  154. if (client.connect(server, 80)) { //starts client connection, checks for connection
  155. client.println("GET http://dfgdfgdfg/datum.php");
  156. client.println("Host: dfgdfgdfg");
  157. client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  158. client.println("Connection: close");
  159. client.println();
  160. delay(1000);
  161. while (client.connected()) {
  162. String line = client.readStringUntil('\n');
  163. // Serial.println(line); //ak chceme vypísať HTTP header
  164. if (line == "\r") {
  165. break;
  166. }
  167. }
  168. String line = client.readStringUntil('\n');
  169. // Serial.println("Nacitany payload response: Cas - ");
  170. // Serial.println(line); //odpoveď webservera - naše dáta
  171. den = line;
  172. }
  173. else {
  174. Serial.println("Pripojenie na webserver sa nepodarilo");
  175. }
  176. client.stop();
  177. }
  178. Serial.print("Den : ");
  179. Serial.println(den);
  180. lcd.setCursor ( 0,0 );
  181. lcd.print(" ");
  182. lcd.setCursor ( 0,0 );
  183. lcd.print(den);
  184. }
  185.  
  186. void odesli() {
  187. if(client.connect(server, 80)){
  188. client.print("GET http://dfgdfgdfgdfg/logger_venku.php?teplota=");
  189. client.print(tep,0);
  190. client.print("&vlhkost=");
  191. client.print(vlh,0);
  192. client.print("&tlak=");
  193. client.println(tlak_aktualni,0);
  194. client.println("Host: dfgdfgdfgdfg");
  195. client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
  196. client.println("Connection: close");
  197. client.println();
  198. client.stop();
  199. Serial.println("odeslano");
  200. Serial.println("_________________________");
  201. }
  202. }
  203.  
  204. void loop() {
  205. aktualniMillis1 = millis(); //podivam se na hodinky
  206. aktualniMillis5 = millis(); //podivam se na hodinky
  207. if(aktualniMillis1 - predchoziMillis1 > 60000) { //pokud zhasnout ubehlu uz 500ms od posledni akce?
  208. predchoziMillis1 = aktualniMillis1; //v tento cas jsem provedl posledni akci
  209. teplota_vlhkost();
  210. tlak();
  211. hodiny();
  212. den_tydnu();
  213. Serial.println("_________________________");
  214. }
  215. else if(aktualniMillis5 - predchoziMillis5 > 600000) { //pokud zhasnout ubehlu uz 500ms od posledni akce?
  216. predchoziMillis5 = aktualniMillis5; //v tento cas jsem provedl posledni akci
  217. odesli();
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement