Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include "RTClib.h"
  4. #include <EEPROM.h>
  5.  
  6. #define time 1
  7. // Enter a MAC address and IP address for your controller below.
  8. // The IP address will be dependent on your local network:
  9. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  10. IPAddress ip(192, 168, 0, 177);
  11.  
  12. // Initialize the Ethernet server library
  13. // with the IP address and port you want to use
  14. // (port 80 is default for HTTP):
  15. EthernetServer server(80);
  16. //----------------------------------------------------------------------------------------------------------------
  17. int i=0;
  18. String hora="";
  19. String readString;
  20.  
  21.  
  22. RTC_DS1307 RTC;
  23.  
  24.  
  25. void setup() {
  26.  
  27. pinMode(2,OUTPUT);
  28. // Open serial communications and wait for port to open:
  29. Serial.begin(9600);
  30.  
  31.  
  32.  
  33.  
  34.  
  35. RTC.begin(); // Inicia la comunicaci¢n con el RTC
  36.  
  37. RTC.adjust(DateTime(__DATE__, __TIME__)); // Establece la fecha y hora (Comentar una vez establecida la hora)
  38.  
  39.  
  40. while (!Serial) {
  41. ; // wait for serial port to connect. Needed for native USB port only
  42. }
  43. Serial.println("Sistemas Digitales ;D");
  44.  
  45. // start the Ethernet connection and the server:
  46. Ethernet.begin(mac, ip);
  47.  
  48. // Check for Ethernet hardware present
  49. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  50. Serial.println("Ethernet shield no se encontro. No se puede correr el programa sin el hardware. :(");
  51. while (true) {
  52. delay(1); // do nothing, no point running without Ethernet hardware
  53. }
  54. }
  55. if (Ethernet.linkStatus() == LinkOFF) {
  56. Serial.println("El cable Ethernet no esta onectado.");
  57. }
  58.  
  59. // start the server
  60. server.begin();
  61. Serial.print("El servidor esta en: ");
  62. Serial.println(Ethernet.localIP());
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. void loop() {
  71. // listen for incoming clients
  72.  
  73.  
  74. delay(1000);
  75. DateTime now = RTC.now(); // Obtiene la fecha y hora del RTC
  76.  
  77.  
  78.  
  79.  
  80. EthernetClient client = server.available();
  81. if (client) {
  82. while (client.connected()) {
  83. if (client.available()) {
  84. //---------------------------------------------------------------------------------------------------------------------------------
  85. char c = client.read();
  86.  
  87.  
  88. if(i==11 || i==12 || i==16 || i==17){
  89. if(i==16){
  90. hora +=':';
  91. }
  92. hora += c;
  93. }
  94.  
  95.  
  96. if(readString.length()< 100){
  97. readString += c;
  98. }
  99. i++;
  100. //Este escribe el texto en pantalla letra por letra
  101. Serial.write(c);
  102.  
  103.  
  104.  
  105.  
  106. // if you've gotten to the end of the line (received a newline
  107. // character) and the line is blank, the http request has ended,
  108. // so you can send a reply
  109. if (c == 'n') {
  110. Serial.println("acabo lectura");
  111.  
  112. Serial.println(hora);
  113. Serial.println(readString);
  114.  
  115.  
  116. client.println("HTTP/1.1 200 OK"); // Enviar una respuesta tipica
  117. client.println("Content-Type: text/html");
  118. client.println("Connection: close");
  119. client.println("Refresh: 10");
  120. client.println();
  121. client.println("<!DOCTYPE HTML>");
  122. client.println(" <html lang='en-US'>");
  123. client.println("<html>");
  124. client.println("<head><meta charset='UTF-8'><title>Sistemas Digitales</title></head>");
  125. client.println("<body>");
  126. client.println("<div style='text-align:center;'>");
  127. client.print("<h1>Fecha y hora del servidor:");
  128. client.print(now.day(), DEC);
  129. client.print('/');
  130. client.print(now.month(), DEC);
  131. client.print('/');
  132. client.print(now.year(), DEC);
  133. client.print(' ');
  134. client.print(' ');
  135. int horini;
  136. horini=now.hour();
  137. String horartc = "0";
  138. if(horini<10){
  139. horartc += horini;
  140. }else{
  141. horartc= String(horini);
  142. }
  143. client.print(horartc);
  144. client.print(':');
  145. int minuto;
  146. minuto=now.minute();
  147. String mainut;
  148. if(minuto<10){
  149. mainut='0'+ String(minuto);
  150. }else{
  151. mainut= String(minuto);
  152. }
  153. client.print(mainut);
  154. client.println("</h1>");
  155. client.println("<form method='get'>");
  156. client.println("<br /><h2>¿A que horas le quiere dar de comer al chucho?</h2><br />");
  157. client.println("<input type='time' name='hora'>");
  158. client.println("<br/><h2>que dias lo quiere alimentar?</h2><br />");
  159. //dias en checkbox
  160. //client.println("<label><input type='checkbox' name='cbox1' value='?LunesOn'>Lunes</label><br><label><input type='checkbox' name='cbox2' value='?MartesOn'>Martes</label><br><label><input type='checkbox' name='cbox3' value='?MiercolesOn'>Miercoles</label><br><label><input type='checkbox' name='cbox4' value='?JuevesOn'>Jueves</label><br><label><input type='checkbox' name='cbox5' value='?ViernesOn'>Viernes</label><br><label><input type='checkbox' name='cbox6' value='?SabadoOn'>Sabado</label><br><label><input type='checkbox' name='cbox7' value='?DomingoOn'>Domingo</label><br>");
  161. client.println("<input type='submit' value='Enviar' id='enviar'> ");
  162. client.println("</form>");
  163. if (readString.length() >0) {
  164. Serial.println("hora del rtc");
  165. String horiux = horartc +':'+ mainut;
  166. Serial.println(horiux);
  167. EEPROM.put(0, hora);
  168.  
  169. if(EEPROM.get( 0, hora ) == horiux){
  170. digitalWrite(2, HIGH);
  171. delay(10000);
  172. digitalWrite(2, LOW);
  173. delay(60000);
  174. }
  175. readString="";
  176. i=0;
  177. hora="";
  178.  
  179.  
  180. client.println("</body>");
  181. client.println("</html>");
  182.  
  183. delay(1);
  184. // close the connection:
  185. client.stop();
  186.  
  187. }
  188.  
  189. }
  190. }
  191.  
  192. }
  193.  
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement