Advertisement
tiodocomputador

Chovendo na horta

Nov 1st, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.75 KB | None | 0 0
  1. /*
  2.   PROTOCOLO NE-555
  3.   Leitura de sensores a distancia do Arduino
  4.   com ligacao por meio de fios
  5.   _____________________________
  6.   Desenvolvido por Alexandre Aravecchia
  7.   a.k.a. O Tio do Computador
  8.   Projetista, desenvolvedor, programador
  9.   http://tiodocomputador.tumblr.com/
  10.   Com o apoio de http://labdegaragem.com.br/ e http://www.labirito.com/
  11.  
  12.   Publicado no Labdegaragem em 10/outubro/2016.
  13. */
  14.  
  15. #include <math.h>
  16. #include <Arduino.h>
  17. #include <SPI.h>
  18. #include <Ethernet.h>
  19.  
  20. const int t = 10;
  21.  
  22. // Enter a MAC address and IP address for your controller below.
  23. // The IP address will be dependent on your local network:
  24. byte mac[] = {
  25.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  26. };
  27. IPAddress ip(192, 168, 0, 111);
  28.  
  29. // Initialize the Ethernet server library
  30. // with the IP address and port you want to use
  31. // (port 80 is default for HTTP):
  32. EthernetServer server(80);
  33. String readString;
  34.  
  35. int entrada = 2;
  36. int lulz = A0;
  37. int L1 = 4; //lampada 1
  38. int L2 = 5; //lampada 2
  39. int L3 = 6; //lampada 3
  40. int AGUA = 7;
  41.  
  42. int LED = 13;
  43. int LedState;
  44.  
  45. int BETA =  3870; //Calibrar este valor utilizando um termometro de precisao externo como comparacao
  46. float Ro = 100000; //Resistencia tpica de um termistor de 100k
  47. float To = 25 + 273; //Transforma temperatura de referencia do Termistor de Celsius para Kelvin
  48.  
  49. float pico; //Tempo
  50. float vale;
  51. float Periodo;
  52. float Frequencia;
  53.  
  54. float Kelvin;
  55. float Celsius = Kelvin - 273;
  56. float media;
  57.  
  58. float R; // Resistencia do Termistor
  59. float R2 = 10000; //Segundo resistor do NE-222
  60. float C1 = 0.92; // Capacitor principal em MICROFARADAY;
  61.  
  62. float LUZ;
  63.  
  64.  
  65. void setup()
  66. {
  67.   pinMode(entrada, INPUT_PULLUP);
  68.   pinMode(lulz, INPUT);
  69.   pinMode(LED, OUTPUT);
  70.   pinMode(AGUA, OUTPUT);
  71.   pinMode(L1, OUTPUT);
  72.   pinMode(L2, OUTPUT);
  73.   pinMode(L3, OUTPUT);
  74.  
  75.  
  76.   // Open serial communications and wait for port to open:
  77.   Serial.begin(9600);
  78.   while (!Serial)
  79.   {
  80.     ; // wait for serial port to connect. Needed for native USB port only
  81.   }
  82.   // start the Ethernet connection and the server:
  83.   Ethernet.begin(mac, ip);
  84.   String readString;
  85.   server.begin();
  86.   // Serial.print("server is at ");
  87.   // Serial.println(Ethernet.localIP());
  88.   LedState = 0;
  89. }
  90.  
  91. void loop()
  92. {
  93.       digitalWrite(LED, LedState);
  94.  
  95.   LUZ = map(analogRead(lulz), 0, 1023, 0, 100);
  96.   pico = pulseIn(entrada, HIGH);
  97.   vale = pulseIn(entrada, LOW);
  98.   Periodo = (pico + vale);
  99.   Frequencia = 1000000 / Periodo;
  100.  
  101.   R = ((1.44 * Periodo) / C1) - (2 * R2);
  102.  
  103.   // Kelvin =  1 / ( (1/To) + ((log(R/Ro)/ BETA)) ); // Calcula a temperatura por Steinhart-Hart
  104.   Kelvin = 1 / ( (1 / To) + ((log(R / Ro) / BETA))); // Sensor NTC
  105.   //Kelvin = 1/To;
  106.  
  107.   Celsius = Kelvin - 273; // Convert Kelvin to Celsius
  108.  
  109.   //Irrigar ou nao irrigar:
  110.   if ((Celsius > 30) && (LUZ < 50))
  111.   {
  112.     digitalWrite(AGUA, HIGH);
  113.   }
  114.   else
  115.   {
  116.     digitalWrite(AGUA, LOW);
  117.   }
  118.   if (LUZ < 60)
  119.   {
  120.     if ((Celsius > 0) && (Celsius <= 20))
  121.     {
  122.       digitalWrite(L1, HIGH);
  123.       digitalWrite(L2, LOW);
  124.       digitalWrite(L3, LOW);
  125.       digitalWrite(AGUA, LOW);
  126.     }
  127.     else if ((Celsius > 20) && (Celsius <= 25))
  128.     {
  129.       digitalWrite(L1, HIGH);
  130.       digitalWrite(L2, HIGH);
  131.       digitalWrite(L3, LOW);
  132.       digitalWrite(AGUA, LOW);
  133.     }
  134.     else if ((Celsius > 25) && (Celsius <= 30))
  135.     {
  136.       digitalWrite(L1, HIGH);
  137.       digitalWrite(L2, HIGH);
  138.       digitalWrite(L3, HIGH);
  139.       digitalWrite(AGUA, LOW);
  140.     }
  141.     else if ((Celsius > 30))
  142.     {
  143.       digitalWrite(L1, HIGH);
  144.       digitalWrite(L2, HIGH);
  145.       digitalWrite(L3, HIGH);
  146.       digitalWrite(AGUA, HIGH);
  147.     }
  148.   }
  149.  
  150.  
  151.   // listen for incoming clients
  152.   EthernetClient client = server.available();
  153.   if (client)
  154.   {
  155.     Serial.println("new client");
  156.     // an http request ends with a blank line
  157.     boolean currentLineIsBlank = true;
  158.     while (client.connected())
  159.     {
  160.       if (client.available())
  161.       {
  162.         digitalWrite(LED, HIGH);
  163.         char c = client.read();
  164.         if (readString.length() < 100) {
  165.           readString += c;
  166.         }
  167.         Serial.write(c);
  168.         // if you've gotten to the end of the line (received a newline
  169.         // character) and the line is blank, the http request has ended,
  170.         // so you can send a reply
  171.         if (c == '\n' && currentLineIsBlank)
  172.         {
  173.           // send a standard http response header
  174.           client.println("HTTP/1.1 200 OK");
  175.           client.println("Content-Type: text/html");
  176.           client.println("Connection: close");  // the connection will be closed after completion of the response
  177.           client.println("Refresh: 30");  // refresh the page automatically every 5 sec (default)
  178.           client.println();
  179.           client.println("<!DOCTYPE HTML>");
  180.           client.println("<html>");
  181.           //_________________________________________________HEAD_________________________________________________
  182.           client.println("<head>");
  183.           client.println("<title> Arduino </title>");
  184.           //_____________________________________________CSS STYLE_________________________________________________
  185.  
  186.           client.println("<style type=text/css>");
  187.  
  188.           client.println("h1 {font-size: 42px; font-family: arial; color: white; align: center}");
  189.           client.println("h2 {font-size: 28px; font-family: arial; color: white; align: center}");
  190.           client.println("h3 {font-size: 18px; font-family: arial; color: green}");
  191.           //client.println("h4 {font-size: 18px; font-family: verdana; color: white}");
  192.           //client.println("h5 {font-size: 10px; font-family: verdana; color: red}");
  193.           //client.println("h6 {font-size: 8px; font-family: courier; color: red}");
  194.  
  195.           client.println("</style>");
  196.           client.println("</head>");
  197.           //_________________________________________________BODY_________________________________________________
  198.           client.println("<body bgcolor=black>");
  199.           client.println("<h2 align=center>");
  200.           client.print("<table width=700 align=center>");
  201.           //<table cellpadding="0" cellspacing="2" border="0" align="center" width="1000" style="border-color: black; font-family: monospace; color:#FFFFFF">
  202.  
  203.           client.print("<tr align=center border-color=white>");
  204.  
  205.  
  206.           client.print("<td>");
  207.           client.print(Celsius);
  208.           client.print(" &#176;C");
  209.           client.print("</td>");
  210.  
  211.           client.print("<td>");
  212.           client.print(LUZ);
  213.           client.print(" &#37;");
  214.           client.print("</td>");
  215.  
  216.           client.print("<td>");
  217.           client.println("<a href=\"/?ledon\"\">Ligar o led</a>");
  218.           client.print("</td>");
  219.  
  220.           client.print("<td>");
  221.           client.println("<a href=\"/?ledoff\"\">Desligar o led</a>");
  222.           client.print("</td>");
  223.           client.print(" </tr>");
  224.           client.print("</table>");
  225.           client.println(" </h2> ");
  226.           client.println(" </body> ");
  227.           client.println(" </html> ");
  228.           break;
  229.         }
  230.         if (c == '\n')
  231.         {
  232.           // you're starting a new line
  233.           currentLineIsBlank = true;
  234.         }
  235.         else if (c != '\r')
  236.         {
  237.           // you've gotten a character on the current line
  238.           currentLineIsBlank = false;
  239.         }
  240.       }
  241.     }
  242.     // give the web browser time to receive the data
  243.     delay(t);
  244.     // close the connection:
  245.     client.stop();
  246.     //Serial.println("client disconnected");
  247.     // Ethernet.maintain();
  248.     if ((readString.indexOf("?ledon") > 0) && (readString.indexOf("?ledoff") < 0))
  249.     {
  250.       digitalWrite(LED, 1);
  251.       LedState =1;
  252.     }
  253.     else if (readString.indexOf("?ledoff") < 0)
  254.     {
  255.       digitalWrite(LED, 0);
  256.       LedState =0;
  257.     }
  258.     readString = "";
  259.   }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement