Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. #undef PROGMEM
  2. #define PROGMEM __attribute__(( section(".progmem.data") ))
  3. #undef PSTR
  4. #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
  5.  
  6. #include <String.h>
  7. #include <Ethernet.h>
  8. #include "Arduino.h"
  9. #include <WiFlyHQ.h>
  10. #include <SoftwareSerial.h>
  11.  
  12. SoftwareSerial wifiSerial(2,3);
  13.  
  14. WiFly wifly;
  15.  
  16. void pag_principal();
  17. void erro404();
  18.  
  19. char buf[80];
  20.  
  21.  
  22. const char rede[] = "000000000000000";
  23. const char senha[] = "00000000000000";
  24.  
  25. String readString;////////////////////////////////////////////////////////
  26. int flagLed1=0; //Flag para comando do Led 1///////////////////////////////
  27.  
  28. void setup()
  29. {
  30. pinMode(9, OUTPUT);
  31.  
  32. Serial.begin(115200);
  33. Serial.println(F("Inicializando"));
  34. Serial.print(F("Memoria livre: "));
  35. Serial.println(wifly.getFreeMemory(),DEC);
  36.  
  37. wifiSerial.begin(9600);
  38. if (!wifly.begin(&wifiSerial, &Serial)) {
  39. Serial.println(F("Falha ao inicializar o wifly"));
  40. wifly.terminal();
  41. }
  42.  
  43. //Se não estiver associado a uma rede configura a rede no módulo
  44. if (!wifly.isAssociated())
  45. {
  46. Serial.println(F("Conectando..."));
  47. wifly.setSSID(rede);
  48. wifly.setPassphrase(senha);
  49. wifly.enableDHCP();
  50. wifly.save();
  51.  
  52. if (wifly.join())
  53. {
  54. Serial.println(F("Conectado a Rede"));
  55. }
  56. else
  57. {
  58. Serial.println(F("Falha ao conectar na rede"));
  59. wifly.terminal();
  60. }
  61. }
  62. else
  63. {
  64. Serial.println(F("Pronto para entrar na rede"));
  65. }
  66.  
  67. wifly.setBroadcastInterval(0); // Desliga UPD broadcast
  68.  
  69. Serial.print(F("MAC: "));
  70. Serial.println(wifly.getMAC(buf, sizeof(buf)));
  71. Serial.print(F("IP: "));
  72. Serial.println(wifly.getIP(buf, sizeof(buf)));
  73. wifly.setDeviceID("ShieldWiFi");
  74.  
  75. if (wifly.isConnected())
  76. {
  77. Serial.println(F("Fechando outras conexões ativas"));
  78. wifly.close();
  79. }
  80.  
  81. wifly.setProtocol(WIFLY_PROTOCOL_TCP);
  82. if (wifly.getPort() != 80) // ------------------
  83. {
  84. wifly.setPort(80);// ----------------------
  85. wifly.save();
  86. Serial.println(F("Trocando para a porta 80, reiniciando...")); //------ Era 80 foi 80
  87. wifly.reboot(); // Reiniciar o módulo para alterar a número da porta
  88. delay(3000);
  89. }
  90. Serial.println(F("Pronto"));
  91. }
  92.  
  93. void loop()
  94. {
  95. //====================================================================================================
  96. {
  97. Serial.println(readString); //Mostra no Serial Monitor o debuging
  98. // HTML data header
  99. if(readString.indexOf('?') >=0) {
  100. wifly.println("HTTP/1.1 Controle de LED's");
  101. wifly.println();
  102. wifly.println();
  103. }
  104. else {
  105.  
  106. /*LED 1*/
  107. if(readString.indexOf("led1on") >0) //checa se o led 1 foi aceso
  108. {
  109. flagLed1=1;
  110. digitalWrite(9, 1); // set pin 9 high
  111. Serial.println("Led1 On");
  112. }
  113. if(readString.indexOf("led1off") >0) //checa se o led 1 foi apagado
  114. {
  115. flagLed1=0;
  116. digitalWrite(9, 0); // set pin 9 low
  117. Serial.println("Led1 Off");
  118. }
  119. //====================================================================================================
  120. if (wifly.available() > 0)
  121. {
  122. if (wifly.gets(buf, sizeof(buf)))
  123. {
  124. if (strncmp_P(buf, PSTR("GET / "), 6) == 0)
  125.  
  126. {
  127. while (wifly.gets(buf, sizeof(buf)) > 0)
  128. {
  129. // Ignora o restante da requisição
  130. }
  131. pag_principal(); // Pula para a rotina que imprime a página principal
  132. }
  133. {
  134. while (wifly.gets(buf, sizeof(buf)) > 0)
  135. {
  136.  
  137. }
  138. }
  139.  
  140. {
  141.  
  142. {
  143.  
  144. }
  145. }
  146.  
  147. {
  148. wifly.flushRx(); // Descarta o restante da transmissão
  149. erro404();
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156.  
  157.  
  158. void pag_principal()
  159. {
  160. /* Envia Cabeçalho HTML*/
  161. wifly.println(F("HTTP/1.1 200 OK"));
  162. wifly.println(F("Content-Type: text/html"));
  163. wifly.println(F("Transfer-Encoding: chunked"));
  164. wifly.println();
  165. //==================================================================================================================================
  166.  
  167.  
  168. //===================================================================================================================================
  169. wifly.sendChunkln(F("<html>"));
  170. wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
  171.  
  172. //==================================================================================================================================
  173. //Led 1 - controle de imagem
  174. if(flagLed1==0)
  175. {
  176. wifly.sendChunkln(F("<table border='10px' align='center'><tr><td><input type=submit value='LIGAR AREA'"));
  177. wifly.sendChunkln(F("style=width:130px;height:45px onclick=\"document.location='led1on'\"/></td>"));
  178. wifly.sendChunkln(F("<td><img alt='Apagada' src='http://imagizer.imageshack.us/v2/100x75q90/924/nSzZ50.jpg' width='80px'/></td></tr>"));
  179. }
  180. else
  181. {
  182. wifly.sendChunkln(F("<tr><td><input type=submit value='DESLIGAR SALA' style=width:130px;height:45px onclick=\"document.location='led1off'\"/> </td>"));
  183. wifly.sendChunkln(F("<td><img alt='Ligada' src='http://imagizer.imageshack.us/v2/100x75q90/922/vJbJI5.jpg' width='80px'/></td></tr>"));
  184. }
  185. //==================================================================================================================================
  186.  
  187.  
  188.  
  189. }
  190. void erro404()
  191. {
  192.  
  193. wifly.println(F("HTTP/1.1 200 OK"));
  194. wifly.println(F("Content-Type: text/html"));
  195. wifly.println(F("Transfer-Encoding: chunked"));
  196. wifly.println();
  197.  
  198. wifly.sendChunkln(F("<html><head>"));
  199. wifly.sendChunkln(F("<title></title>"));
  200. wifly.sendChunkln(F("</head><body>"));
  201. wifly.sendChunkln(F("<h1>P&aacute;gina n&atilde;o encontrada</h1>"));
  202. wifly.sendChunkln(F("</body></html>"));
  203. wifly.sendChunkln();
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement