Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include "DHT.h"
- #include <Adafruit_BMP085.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- #define pinDHT D13
- #define typDHT22 DHT22 // DHT 22 (AM2302)
- DHT mojeDHT(pinDHT, typDHT22);
- const int myPeriodic = 300; // Time to sleep (in seconds):
- Adafruit_BMP085 bmp;
- // Nazev Wi-Fi site, do ktere se mam pripojit
- const char* ssid = "Kony";
- // Heslo Wi-Fi site, do ktere se mam pripojit
- const char* password = "dfgdfgdfg";
- char server[] = "dfgdfgdfgfdg"; //URL adresa serveru
- WiFiClient client;
- unsigned long cas = 0;
- String cas_aktualni;
- String den;
- float tep;
- float vlh;
- float tlak_aktualni;
- unsigned long aktualniMillis1; //aktualni cas
- unsigned long predchoziMillis1; //cas poseldni akce
- unsigned long aktualniMillis5; //aktualni cas
- unsigned long predchoziMillis5; //cas poseldni akce
- void setup() {
- lcd.begin();
- lcd.backlight();
- // komunikace přes sériovou linku rychlostí 9600 baud
- //Serial.begin(9600);
- // zapnutí komunikace s teploměrem DHT
- mojeDHT.begin();
- if (!bmp.begin()) {
- Serial.println("Could not find BMP180 or BMP085 sensor at 0x77");
- while (1) {}
- }
- Serial.println();
- Serial.print("Pripojuji k ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- // Dokud nejsem pripojeny k Wi-Fi,zapisuj do seriove linky tecky progressbaru
- while (WiFi.status() != WL_CONNECTED) {
- delay(100);
- Serial.print(".");
- lcd.setCursor ( 0, 0 );
- lcd.print(".");
- }
- // Jsem pripojeny k Wi-Fi a mohu pokracovat
- Serial.println();
- Serial.println("WiFi pripojena!");
- lcd.setCursor ( 0, 1 );
- lcd.print("Wifi Pripojena!");
- // Napis IP adresu, kterou mikropocitac dostal
- Serial.print("Pouzij k pripojeni tuto adresu: ");
- Serial.print("http://");
- Serial.println(WiFi.localIP());
- lcd.setCursor ( 0, 2 );
- lcd.print(WiFi.localIP());
- delay(2000);
- lcd.clear();
- }
- void teplota_vlhkost() {
- tep = mojeDHT.readTemperature();
- delay(2000);
- vlh = mojeDHT.readHumidity();
- // kontrola, jestli jsou načtené hodnoty čísla pomocí funkce isnan
- if (isnan(tep) || isnan(vlh)) {
- // při chybném čtení vypiš hlášku
- Serial.println("Chyba při čtení z DHT senzoru!");
- } else {
- // pokud jsou hodnoty v pořádku,
- // vypiš je po sériové lince
- Serial.print("Teplota: ");
- Serial.print(tep,1);
- Serial.println(" °C");
- Serial.print("Vlhkost: ");
- Serial.print(vlh,0);
- Serial.println(" %");
- lcd.setCursor ( 1, 1 );
- lcd.print("Teplota : ");
- lcd.setCursor ( 11, 1 );
- lcd.print(" ");
- lcd.setCursor ( 11, 1 );
- lcd.print(tep,1);
- lcd.print(" ");
- lcd.print((char)223);
- lcd.print("C");
- lcd.setCursor ( 1,2 );
- lcd.print("Vlhkost : ");
- lcd.setCursor ( 11,2 );
- lcd.print(vlh,0);
- lcd.print(F(" %"));
- }
- // pauza pro přehlednější výpis
- delay(2000);
- }
- void tlak() {
- tlak_aktualni = (bmp.readPressure()/100)+38;
- Serial.print("Tlak : ");
- Serial.print(tlak_aktualni,0);
- Serial.println(" hPa");
- lcd.setCursor ( 2,3 );
- lcd.print("Tlak :");
- lcd.setCursor ( 11,3 );
- lcd.print(tlak_aktualni,0);
- lcd.print(F(" hPa"));
- }
- void hodiny() {
- if (millis() - cas >= 6000 || cas == 0) {
- cas = millis();
- if (client.connect(server, 80)) { //starts client connection, checks for connection
- client.println("GET http://fdgdfgdfg/cas.php");
- client.println("Host: fdgdfgfdgfdg");
- client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
- client.println("Connection: close");
- client.println();
- delay(1000);
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- // Serial.println(line); //ak chceme vypísať HTTP header
- if (line == "\r") {
- break;
- }
- }
- String line = client.readStringUntil('\n');
- // Serial.println("Nacitany payload response: Cas - ");
- // Serial.println(line); //odpoveď webservera - naše dáta
- cas_aktualni = line;
- }
- else {
- Serial.println("Pripojenie na webserver sa nepodarilo");
- }
- client.stop();
- }
- Serial.print("Cas : ");
- Serial.println(cas_aktualni);
- lcd.setCursor ( 15,0 );
- lcd.print(cas_aktualni);
- }
- void den_tydnu() {
- if (millis() - cas >= 6000 || cas == 0) {
- cas = millis();
- if (client.connect(server, 80)) { //starts client connection, checks for connection
- client.println("GET http://dfgdfgdfg/datum.php");
- client.println("Host: dfgdfgdfg");
- client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
- client.println("Connection: close");
- client.println();
- delay(1000);
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- // Serial.println(line); //ak chceme vypísať HTTP header
- if (line == "\r") {
- break;
- }
- }
- String line = client.readStringUntil('\n');
- // Serial.println("Nacitany payload response: Cas - ");
- // Serial.println(line); //odpoveď webservera - naše dáta
- den = line;
- }
- else {
- Serial.println("Pripojenie na webserver sa nepodarilo");
- }
- client.stop();
- }
- Serial.print("Den : ");
- Serial.println(den);
- lcd.setCursor ( 0,0 );
- lcd.print(" ");
- lcd.setCursor ( 0,0 );
- lcd.print(den);
- }
- void odesli() {
- if(client.connect(server, 80)){
- client.print("GET http://dfgdfgdfgdfg/logger_venku.php?teplota=");
- client.print(tep,0);
- client.print("&vlhkost=");
- client.print(vlh,0);
- client.print("&tlak=");
- client.println(tlak_aktualni,0);
- client.println("Host: dfgdfgdfgdfg");
- client.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
- client.println("Connection: close");
- client.println();
- client.stop();
- Serial.println("odeslano");
- Serial.println("_________________________");
- }
- }
- void loop() {
- aktualniMillis1 = millis(); //podivam se na hodinky
- aktualniMillis5 = millis(); //podivam se na hodinky
- if(aktualniMillis1 - predchoziMillis1 > 60000) { //pokud zhasnout ubehlu uz 500ms od posledni akce?
- predchoziMillis1 = aktualniMillis1; //v tento cas jsem provedl posledni akci
- teplota_vlhkost();
- tlak();
- hodiny();
- den_tydnu();
- Serial.println("_________________________");
- }
- else if(aktualniMillis5 - predchoziMillis5 > 600000) { //pokud zhasnout ubehlu uz 500ms od posledni akce?
- predchoziMillis5 = aktualniMillis5; //v tento cas jsem provedl posledni akci
- odesli();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement