Advertisement
RuiViana

Ethrenet_W5100_HC_SR04

Dec 14th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. //Programa : Arduino Ethernet Shield W5100 e HC-SR04
  2. //Alteracoes e adaptacoes : FILIPEFLOP
  3. //
  4. //Baseado no programa exemplo de
  5. //by David A. Mellis e Tom Igoe
  6.  
  7. #include <Ultrasonic.h>
  8. #include <SPI.h>
  9. #include <Ethernet.h>
  10.  
  11. //Define os parametros para o sensor ultrasonico HC-SR04
  12. #define PINO_TRIGGER 6 //Porta ligada ao pino Trigger do sensor
  13. #define PINO_ECHO 7 //Porta ligada ao pino Echo do sensor
  14. //Inicializa o sensor ultrasonico
  15. Ultrasonic ultrasonic(PINO_TRIGGER, PINO_ECHO);
  16.  
  17. //Definicoes de IP, mascara de rede e gateway
  18. byte mac[] = {
  19. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  20. IPAddress ip(192,168,0,5); //Define o endereco IP
  21. IPAddress gateway(192,168,1,1); //Define o gateway
  22. IPAddress subnet(255, 255, 255, 0); //Define a máscara de rede
  23.  
  24. //Inicializa o servidor web na porta 80
  25. EthernetServer server(80);
  26.  
  27. void setup()
  28. {
  29. //Inicializa a interface de rede
  30. Ethernet.begin(mac, ip, gateway, subnet);
  31. server.begin();
  32. }
  33.  
  34. void loop() {
  35. float cmMsec;
  36. long microsec = ultrasonic.timing();
  37. //Le e armazena as informacoes do sensor ultrasonico
  38. cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  39.  
  40. //Aguarda conexao do browser
  41. EthernetClient client = server.available();
  42. if (client) {
  43. Serial.println("new client");
  44. // an http request ends with a blank line
  45. boolean currentLineIsBlank = true;
  46. while (client.connected()) {
  47. if (client.available()) {
  48. char c = client.read();
  49. Serial.write(c);
  50. // if you've gotten to the end of the line (received a newline
  51. // character) and the line is blank, the http request has ended,
  52. // so you can send a reply
  53. if (c == 'n' && currentLineIsBlank) {
  54. // send a standard http response header
  55. client.println("HTTP/1.1 200 OK");
  56. client.println("Content-Type: text/html");
  57. client.println("Connection: close");
  58. client.println("Refresh: 2"); //Recarrega a pagina a cada 2seg
  59. client.println();
  60. client.println("<!DOCTYPE HTML>");
  61. client.println("<html>");
  62. //Configura o texto e imprime o titulo no browser
  63. client.print("<font color=#FF0000><b><u>");
  64. client.print("Envio de informacoes pela rede utilizando Arduino");
  65. client.print("</u></b></font>");
  66. client.println("<br />");
  67. client.println("<br />");
  68. //Mostra o estado da porta digital 3
  69. int porta_digital = digitalRead(3);
  70. client.print("Porta Digital 3 : ");
  71. client.print("<b>");
  72. client.print(porta_digital);
  73. client.println("</b>");
  74. client.print(" (0 = Desligada, 1 = Ligada)");
  75. client.println("<br />");
  76. //Mostra as informacoes lidas pelo sensor ultrasonico
  77. client.print("Sensor Ultrasonico : ");
  78. client.print("<b>");
  79. client.print(cmMsec);
  80. client.print(" cm");
  81. client.println("</b></html>");
  82. break;
  83. }
  84. if (c == 'n') {
  85. // you're starting a new line
  86. currentLineIsBlank = true;
  87. }
  88. else if (c != 'r') {
  89. // you've gotten a character on the current line
  90. currentLineIsBlank = false;
  91. }
  92. }
  93. }
  94. // give the web browser time to receive the data
  95. delay(1);
  96. // close the connection:
  97. client.stop();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement