Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h> //KNIZNICA pre HTTPS spojenia
  3. #include "DHT.h"
  4. #define DHTPIN 2 //GPIO2 = D4
  5. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  6. DHT dht(DHTPIN, DHTTYPE);
  7. const char * ssid = "marvel"; //meno wifi siete
  8. const char * password = "zdenek23"; //Heslo na wifi siet
  9. const char * host = "tausha.php5.cz"; //bez https a www
  10. const int httpsPort = 443; //https port zabezpeceny prenos
  11. const char * fingerprint ="‎34 62 31 d2 6d 18 cf d0 4c 2c cc 7a f8 85 d0 6a f6 ae 5b 13"; //odtlacok HTTPS certifikatu v SHA1 formate
  12. const int rele = 0; //GPIO0 = D3
  13. void setup() {
  14.   Serial.begin(115200); //rychlost seriovej linky
  15.   pinMode(rele, OUTPUT);
  16.     dht.begin();
  17.   Serial.println();
  18.   Serial.println("pripajam na ");
  19.   Serial.println(ssid);
  20.   WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom
  21.   while (WiFi.status() != WL_CONNECTED) { //pokial sa nepripojime na wifi opakuj pripajanie a spustaj funkcie pre ovladanie v offline rezime
  22.     delay(500);
  23.     Serial.println(".");
  24.   }
  25.   Serial.println("");
  26.   Serial.println("WiFi pripojene"); //uspesne pripojenie na wifi siet
  27.   Serial.println("IP adresa: ");
  28.   Serial.println(WiFi.localIP()); // pridelena IP adresa pre dosku
  29. }
  30.  
  31. void odosli_data() {
  32.  
  33.   WiFiClientSecure client;
  34.   if (client.verify(fingerprint, host)) {} else {}
  35.   if (client.connect(host, httpsPort)) {
  36.      String h = String(dht.readHumidity());
  37.   // Read temperature as Celsius (the default)
  38.   String t = String(dht.readTemperature());
  39. String url = "/zapisdata.php?teplota=" + t + "&vlhkost=" + h;
  40.     client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: NodeMCU\r\n" + "Connection: close\r\n\r\n");
  41.     Serial.println("Odoslane data do db:");
  42.    Serial.println(h);
  43.     Serial.println(t);
  44.   } else if (!client.connect(host, httpsPort)) {
  45.     Serial.println("Neuspesne pripojenie pre odoslanie teplot do DB!");
  46.   }
  47. }
  48.  
  49.  
  50.  
  51.  
  52. void stav_rele() {
  53.   WiFiClientSecure client;
  54.   if (client.verify(fingerprint, host)) {} else {}
  55.   if (client.connect(host, httpsPort)) {
  56.     String url = "/rele1.txt";
  57.     client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "User-Agent: NodeMCU\r\n" + "Connection: close\r\n\r\n");
  58.  
  59.     while (client.connected()) {
  60.       String line = client.readStringUntil('\n');
  61.       if (line == "\r") {
  62.         break;
  63.       }
  64.     }
  65.     String line = client.readStringUntil('\n');
  66.    
  67.     if (line == "ZAP") {
  68.       digitalWrite(rele, LOW);
  69.       Serial.println("Rele zapnute");
  70.     } else if (line == "VYP") {
  71.       digitalWrite(rele, HIGH);
  72.       Serial.println("Rele vypnute");
  73.     }
  74.   } else if (!client.connect(host, httpsPort)) {
  75.     Serial.println("Neuspesne pripojenie pre ovladanie rele!");
  76.    
  77.   }
  78. }
  79. void loop() {
  80.  
  81.     odosli_data();
  82.     stav_rele();
  83.  delay(10000);
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement