Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1.  
  2. //----------- Consegui depois de 4 anos ------------------
  3. //----------- Deus me ensinou sem ele não conseguiram nunca ---------
  4. //----------- Code do lbg ------------------------------------------
  5.  
  6. /*
  7. * WiFiShield
  8. * Baseado no exemplo httpserver da biblioteca WiFlyHQmaster
  9. */
  10.  
  11. #undef PROGMEM
  12. #define PROGMEM __attribute__(( section(".progmem.data") ))
  13. #undef PSTR
  14. #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
  15.  
  16.  
  17. #include "Arduino.h"
  18. #include <WiFlyHQ.h>
  19. #include <SoftwareSerial.h>
  20.  
  21. SoftwareSerial wifiSerial(2,3);
  22.  
  23. WiFly wifly;
  24.  
  25. void pag_principal();
  26. void pag_analogica();
  27. void pag_ola(char *name);
  28. void erro404();
  29.  
  30. char buf[80];
  31. boolean ledstatus = false;
  32.  
  33. // Altere as duas variáveis abaixo de acordo com sua rede Wifi
  34. const char rede[] = "INTEL";
  35. const char senha[] = "12345678";
  36.  
  37.  
  38. void setup()
  39. {
  40. pinMode(11, OUTPUT);
  41. pinMode(10, OUTPUT);
  42.  
  43.  
  44. Serial.begin(115200);
  45. Serial.println(F("Inicializando"));
  46. Serial.print(F("Memoria livre: "));
  47. Serial.println(wifly.getFreeMemory(),DEC);
  48.  
  49. wifiSerial.begin(9600);
  50. if (!wifly.begin(&wifiSerial, &Serial)) {
  51. Serial.println(F("Falha ao inicializar o wifly"));
  52. wifly.terminal();
  53. }
  54.  
  55. //Se não estiver associado a uma rede configura a rede no módulo
  56. if (!wifly.isAssociated())
  57. {
  58. Serial.println(F("Conectando..."));
  59. wifly.setSSID(rede);
  60. wifly.setPassphrase(senha);
  61. wifly.enableDHCP();
  62. wifly.save();
  63.  
  64. if (wifly.join())
  65. {
  66. Serial.println(F("Conectado a Rede"));
  67. }
  68. else
  69. {
  70. Serial.println(F("Falha ao conectar na rede"));
  71. wifly.terminal();
  72. }
  73. }
  74. else
  75. {
  76. Serial.println(F("Pronto para entrar na rede"));
  77. }
  78.  
  79. wifly.setBroadcastInterval(0); // Desliga UPD broadcast
  80.  
  81. Serial.print(F("MAC: "));
  82. Serial.println(wifly.getMAC(buf, sizeof(buf)));
  83. Serial.print(F("IP: "));
  84. Serial.println(wifly.getIP(buf, sizeof(buf)));
  85. wifly.setDeviceID("ShieldWiFi");
  86.  
  87. if (wifly.isConnected())
  88. {
  89. Serial.println(F("Fechando outras conexões ativas"));
  90. wifly.close();
  91. }
  92.  
  93. wifly.setProtocol(WIFLY_PROTOCOL_TCP);
  94. if (wifly.getPort() != 80)
  95. {
  96. wifly.setPort(80);
  97. wifly.save();
  98. Serial.println(F("Trocando para a porta 80, reiniciando..."));
  99. wifly.reboot(); // Reiniciar o módulo para alterar a número da porta
  100. delay(3000);
  101. }
  102. Serial.println(F("Pronto"));
  103. }
  104.  
  105. void loop()
  106. {
  107. if (wifly.available() > 0)
  108. {
  109. if (wifly.gets(buf, sizeof(buf)))
  110. {
  111. if (strncmp_P(buf, PSTR("GET / "), 6) == 0)
  112. //Verifica se é um GET na página principal - 192.168.1.9/
  113. {
  114. while (wifly.gets(buf, sizeof(buf)) > 0)
  115. {
  116. // Ignora o restante da requisição
  117. }
  118. pag_principal(); // Pula para a rotina que imprime a página principal
  119. }
  120. else if (strncmp_P(buf, PSTR("GET /A"), 6) == 0)
  121. //Verifica se é um GET na página de leituras analógicas - 192.168.1.9/A
  122. {
  123. while (wifly.gets(buf, sizeof(buf)) > 0)
  124. {
  125. // Ignora o restante da requisição
  126. }
  127. pag_analogica(); //Vai para rotina que imprime a página de leituras analógicas
  128. }
  129. else if (strncmp_P(buf, PSTR("POST"), 4) == 0) // Verifica se foi feito um POST
  130. {
  131. char username[16]; //Cria uma váriavel para armazenar o campo "Digite seu nome"
  132. if (wifly.match(F("user=")))
  133. {
  134. wifly.gets(username, sizeof(username));
  135. wifly.flushRx(); // Descarta o restante da transmissão
  136. pag_ola(username);
  137. }
  138. }
  139. else
  140. {
  141. wifly.flushRx(); // Descarta o restante da transmissão
  142. erro404(); //Envia página de erro - link não existe
  143. }
  144. }
  145. }
  146. }
  147.  
  148.  
  149. // Envia a página principal
  150. void pag_principal()
  151. {
  152. /* Envia Cabeçalho HTML*/
  153. wifly.println(F("HTTP/1.1 200 OK"));
  154. wifly.println(F("Content-Type: text/html"));
  155. wifly.println(F("Transfer-Encoding: chunked"));
  156. wifly.println();
  157.  
  158. /* Envia o corpo do email com protocolo fragmentado
  159. * Com isto o cliente sabe quando a mensagem terminou
  160. */
  161. wifly.sendChunkln(F("<html>"));
  162. wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
  163. wifly.sendChunkln(F("<BR><center></B></I></U><a href=\"http://www.google.com\"> <img src=http://www.bullx.com.br/images/agua.png></a></B></I></U> </center>"));
  164. wifly.sendChunkln(F("<p><center>Moises Wifi-Shield</center></p>"));
  165. wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.8/'\">Liga o LED no pino 10</button></center><p>"));
  166. wifly.sendChunkln(F("<p><center><form name=\"input\" action=\"/\" method=\"post\">"));
  167. wifly.sendChunkln(F("Digite seu nome"));
  168. wifly.sendChunkln(F("<input type=\"text\" name=\"user\" /></center></p>"));
  169. wifly.sendChunkln(F("<p><center><input type=\"submit\" value=\"Enviar\" /></center></p>"));
  170. wifly.sendChunkln(F("</form>"));
  171. wifly.sendChunkln(F("</html>"));
  172. wifly.sendChunkln();
  173.  
  174. ledstatus = 1^ledstatus;
  175. digitalWrite(10,ledstatus); //Inverte o estado do LED ligado ao pino 10
  176. }
  177.  
  178.  
  179. // Envia a página com as leituras analógicas
  180. void pag_analogica()
  181. {
  182. // Envia Cabeçalho HTML
  183. wifly.println(F("HTTP/1.1 200 OK"));
  184. wifly.println(F("Content-Type: text/html"));
  185. wifly.println(F("Transfer-Encoding: chunked"));
  186. wifly.println();
  187. // Enviado página com codificada
  188. wifly.sendChunkln(F("<html>"));
  189. wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
  190. wifly.sendChunkln(F("<BR><center></B></I></U><a href=\"http://www.google.com\"> <img src=http://www.bullx.com.br/images/agua.png></a></B></I></U> </center>"));
  191. wifly.sendChunkln(F("<p><center>Wifi-Shield</center></p>"));
  192. wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.8/'\">Liga o LED no pino 10</button></center><p>"));
  193.  
  194. wifly.sendChunkln(buf);
  195. wifly.sendChunkln(F("</html>"));
  196. wifly.sendChunkln();
  197.  
  198. }
  199.  
  200.  
  201. //Envia página que dá um Olá para os Garagistas
  202. void pag_ola(char *name)
  203. {
  204. // Envia Cabeçalho HTML
  205. wifly.println(F("HTTP/1.1 200 OK"));
  206. wifly.println(F("Content-Type: text/html"));
  207. wifly.println(F("Transfer-Encoding: chunked"));
  208. wifly.println();
  209. // Enviado página com codificada
  210. wifly.sendChunkln(F("<html>"));
  211. wifly.sendChunkln(F("<BR><center></B></I></U><a href=\"http://www.google.com\"> <img src=http://www.bullx.com.br/images/agua.png></a></B></I></U> </center>"));
  212. wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.9/'\">Liga o LED no pino 10</button></center><p>"));
  213.  
  214. wifly.sendChunk(F("<center><h1><p>Ol&aacute; "));
  215. wifly.sendChunk(name);
  216. wifly.sendChunkln(F("</p></h1></center>"));
  217. wifly.sendChunkln(F("</html>"));
  218. wifly.sendChunkln();
  219. }
  220.  
  221.  
  222. // Envia página de erro caso seja digitado uma URL inválida
  223. void erro404()
  224. {
  225. // Envia Cabeçalho HTML
  226. wifly.println(F("HTTP/1.1 200 OK"));
  227. wifly.println(F("Content-Type: text/html"));
  228. wifly.println(F("Transfer-Encoding: chunked"));
  229. wifly.println();
  230. // Enviado página com codificada
  231. wifly.sendChunkln(F("<html><head>"));
  232. wifly.sendChunkln(F("<title></title>"));
  233. wifly.sendChunkln(F("</head><body>"));
  234. wifly.sendChunkln(F("<h1>P&aacute;gina n&atilde;o encontrada</h1>"));
  235. wifly.sendChunkln(F("</body></html>"));
  236. wifly.sendChunkln();
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement