SHARE
TWEET

proiectmetode2/3

a guest Nov 21st, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <ArduinoJson.h>
  3. #include <ESP8266WiFi.h>
  4.  
  5. #define SensorUmiditate A0
  6. //Programare in timp
  7. unsigned long ultimuTimp = 0;
  8. bool seUda = false;
  9. //8 ore
  10. int timpDeAsteptat = 8; //In ore
  11. long timpDeAsteptatTradus = 0; //Va tine valoare orelor in milisecunde
  12. unsigned long timpCurent;
  13. //Connectare la WiFi
  14. const char* ssid = "ProiectMat";
  15. const char* password = "12345678";
  16. //Connectare la serverul de vreme
  17. String apiKey = "1be7ec81c12edde1bb2327d188da9f1f";
  18. String locatie = "arad,RO";
  19. const char* host = "api.openweathermap.org";
  20. const char* sursa = "/data/2.5/forecast?q=arad,RO&cnt=1&appid=1be7ec81c12edde1bb2327d188da9f1f";
  21. //Marimea maxima buffer JSON
  22. const size_t MAX_CONTENT_SIZE = 512;
  23. //Structura pt date vreme
  24. struct dateVreme{
  25.   char* temperatura;
  26.   char* umiditate;
  27. };
  28.  
  29. //Copie pentru temperatura/umiditate
  30. double cTemperatura;
  31. double cUmiditate;
  32. //Variabile pentru vreme
  33. const char* stareVreme;
  34. String cStareVreme;
  35. String stareVremeTradusa;
  36. long presiune;
  37. //Nr pin pentru electrovalva
  38. int pinValva = 12;
  39. bool MAuto = true;
  40. int value = HIGH;
  41. //Creare server pe port 1133 si un al doilea client pentru a accesa vremea
  42.  WiFiServer server(1133);
  43.  WiFiClient client2;
  44. void setup() {
  45.   Serial.begin(115200);
  46.   delay(10);
  47.   pinMode(pinValva, OUTPUT);
  48.   digitalWrite(pinValva, HIGH);
  49.  
  50.   Serial.println();
  51.   Serial.println();
  52.   Serial.print("Se conecteaza ");
  53.   Serial.println(ssid);
  54.  
  55.   WiFi.begin(ssid, password);
  56.  
  57.   while (WiFi.status() != WL_CONNECTED) {
  58.     delay(500);
  59.     Serial.print(".");
  60.   }
  61.   Serial.println("");
  62.   Serial.println("WiFi connected");
  63.  
  64.   // Start the server
  65.   server.begin();
  66.   Serial.println("Serverul a pornit");
  67.  
  68.   // Print the IP address
  69.   Serial.print("Foloseste acest site pentru a intra: ");
  70.   Serial.print("http://");
  71.   Serial.print(WiFi.localIP());
  72.   Serial.println("/");
  73.   acceseazaVreme();
  74.  
  75. }
  76.  
  77. void loop() {
  78.   WiFiClient client = server.available();
  79.   if (!client) {
  80.     return;
  81.   }
  82.  
  83.   Serial.println("new client");
  84.   while(!client.available()){
  85.     delay(1);
  86.   }
  87.  
  88.   String request = client.readStringUntil('\r');
  89.   Serial.println(request);
  90.   client.flush();
  91.  
  92.  //Mod manual/auto
  93.  
  94.  if(request.indexOf("/MODE=AUTO") != -1){
  95.   MAuto = true;  //Mod automatic
  96.  }
  97.  if(request.indexOf("/MODE=MANUAL") != -1){
  98.   MAuto = false; //Mod manual
  99.  }
  100.  //Control electrovalva
  101.  
  102.  
  103.   if (request.indexOf("/EVALVA=ON") != -1)  {
  104.     digitalWrite(pinValva, LOW); //Porneste electrovalva
  105.     value = LOW;
  106.   }
  107.   if (request.indexOf("/EVALVA=OFF") != -1)  {
  108.     digitalWrite(pinValva, HIGH);  //Inchide electrovalva
  109.     value = HIGH;
  110.   }
  111.   //Ore mod manual
  112.   if(request.indexOf("/ORA=2") != -1){
  113.     timpDeAsteptat = 2;
  114.   }
  115.   if(request.indexOf("/ORA=4") != -1){
  116.     timpDeAsteptat =4;
  117.   }
  118.   if(request.indexOf("/ORA=6") != -1){
  119.     timpDeAsteptat = 6;
  120.   }
  121.   client.println("HTTP/1.1 200 OK");
  122.   client.println("Content-Type: text/html");
  123.   client.println("");
  124.   client.println("<!DOCTYPE HTML>");
  125.   client.println("<html>");
  126.  
  127.   client.print("Electrovalva este: ");
  128.   if(value == LOW){
  129.     client.print("Pornita");
  130.   }else{
  131.     client.print("Oprita");
  132.   }
  133.   client.println("<br>");
  134.   client.print("Instalatia este in mod: ");
  135.   if(MAuto == true){
  136.     client.print("Automatic");
  137.   }else{
  138.     client.print("Manual");
  139.     }
  140.   client.println("<p>Nivel umiditate sol: " + String(verificaUmiditate()) + "%" + "</p>");
  141.   client.println("<p>Timp de asteptat: "+ String(timpDeAsteptat) + " ore" + "</p>");
  142.   client.println("<p>Temperatura afara: " + String(cTemperatura) + "</p>");
  143.   client.println("<p>Umiditatea afara: " + String(cUmiditate) + "</p>");
  144.   client.println("<p>Pressiunea atmosferica: " + String(presiune) + "</p>");
  145.   client.println("<p>Stare vreme: " + stareVremeTradusa + "</p>");
  146.   client.println("Control electrovalva: ");
  147.   if(MAuto == true){
  148.     client.println("<br>");
  149.     client.println("Instalatia este in modul automatic!");
  150.   }else{
  151.   client.println("<br>");
  152.   client.println("<a href=\"/EVALVA=ON\"\"><button>Porneste</button></a>");
  153.   client.println("<a href=\"/EVALVA=OFF\"\"><button>Opreste</button></a><br/>");
  154.   }
  155.   client.println("<br>");
  156.   client.println("Control mod: ");
  157.   client.println("<br>");
  158.   client.println("<a href=\"/MODE=AUTO\"\"><button>Automatic</button></a>");
  159.   client.println("<a href=\"/MODE=MANUAL\"\"><button>Manual</button></a><br/>");
  160.   if(MAuto == false){
  161.   client.println("Control manual: ");
  162.   client.println("<br>");
  163.   client.println("<a href=\"/ORA=2\"\"><button>2H</button></a>");
  164.   client.println("<a href=\"/ORA=4\"\"><button>4H</button></a><br/>");
  165.   client.println("<a href=\"/ORA=6\"\"><button>6H</button></a>");
  166.   }
  167.   client.println("</html>");
  168.  
  169.   delay(1);
  170.   Serial.println("Client disonnected");
  171.   Serial.println("");
  172. }
  173.  
  174. void acceseazaVreme() {
  175.   if(conecteazaVreme(host)){
  176.   if(trimiteCerere(host,sursa)){
  177.     dateVreme dateVreme;
  178.     if(citesteJson(&dateVreme)){
  179.       scrieDateVreme(&dateVreme);
  180.     }
  181.   }
  182.  }
  183.  
  184. }
  185.  bool citesteJson(struct dateVreme* dateVreme) {
  186.   String line = "";
  187.   line = client2.readStringUntil('\n');
  188.   Serial.println("Se iau valorile JSON!");
  189.   const size_t bufferSize = 2*JSON_ARRAY_SIZE(1) + 2*JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) +
  190.   JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(7) + 2*JSON_OBJECT_SIZE(8) + 435;
  191.   DynamicJsonBuffer jsonBuffer(bufferSize);
  192.   JsonObject& root = jsonBuffer.parseObject(line);
  193.  
  194.   if (!root.success()) {
  195.     Serial.println("JSON esuat!");
  196.     return false;
  197.   }
  198.  
  199.   Serial.println("Se stocheaza datele!");
  200.   strcpy(dateVreme->temperatura, root["list"][0]["main"]["temp"]);
  201.   strcpy(dateVreme->umiditate, root["list"][0]["main"]["humidity"]);
  202.   cTemperatura = root["list"][0]["main"]["temp"];
  203.   cUmiditate = root["list"][0]["main"]["humidity"];
  204.   presiune = root["list"][0]["main"]["pressure"];
  205.   stareVreme = root["list"][0]["weather"][0]["main"];
  206.   cStareVreme = String(stareVreme);
  207.   if(cStareVreme == "Clouds"){
  208.     stareVremeTradusa = "Innorat";
  209.   }else if(cStareVreme == "Rain"){
  210.     stareVremeTradusa = "Ploaie";
  211.   }
  212.  
  213.   return true;
  214. }
  215. bool trimiteCerere(const char* host,const char* sursa){
  216.   client2.print("GET /data/2.5/forecast?");
  217.    client2.print("q="+locatie);
  218.    client2.print("&appid="+apiKey);
  219.    client2.print("&cnt=1");
  220.    client2.println("&units=metric");
  221.    client2.println("Host: api.openweathermap.org");
  222.    client2.println("Connection: close");
  223.    client2.println();
  224.  
  225.   return true;
  226. }
  227. void scrieDateVreme(const struct dateVreme* dateVreme){
  228.   Serial.print("Temeperatura aerului este: ");
  229.   Serial.println(dateVreme->temperatura);
  230.   Serial.print("Umiditate aerului este: ");
  231.   Serial.println(dateVreme->umiditate);
  232. }
  233. bool conecteazaVreme(const char* numeServer){
  234.   Serial.print("Se conecteaza la ");
  235.   Serial.println(numeServer);
  236.   bool ok = client2.connect(numeServer,80);
  237.  
  238.   Serial.println(ok ? "Connectat la serverul de vreme" : "Nu sa conectat la serverul de vreme!");
  239.   return ok;
  240. }
  241. void deconecteazaVreme(){
  242.   Serial.println("Deconectat de la serverul de vreme!");
  243.   client2.stop();
  244. }
  245. void asteapta(){
  246.   Serial.println("Se asteapta 30 secunde");
  247.   delay(30000);
  248. }
  249. int verificaUmiditate(){
  250.   float valoareSensor;
  251.   valoareSensor = analogRead(SensorUmiditate);
  252.   int procentaj;
  253.   int map_low = 378;
  254.   int map_high = 56;
  255.   procentaj = map(valoareSensor,map_low,map_high,0,100);
  256.   if(procentaj <= 0){
  257.     procentaj = 0;
  258.   }
  259.   if(procentaj >= 100){
  260.     procentaj = 100;
  261.   }
  262.  
  263.   return procentaj;
  264. }
  265. void verificaTimp(){
  266.   timpCurent = millis();
  267.   timpDeAsteptatTradus = timpDeAsteptat * 1000 * 60 *60;
  268.   if(timpCurent - ultimuTimp >= timpDeAsteptatTradus){
  269.     ultimuTimp = timpCurent;
  270.     if(MAuto == false){
  271.       digitalWrite(pinValva,HIGH);
  272.       value = HIGH;
  273.       seUda = true;
  274.       //TODO
  275.     }
  276.   }
  277. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top