Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------- Consegui depois de 4 anos ------------------
- //----------- Deus me ensinou sem ele não conseguiram nunca ---------
- //----------- Code do lbg ------------------------------------------
- /*
- * WiFiShield
- * Baseado no exemplo httpserver da biblioteca WiFlyHQmaster
- */
- #undef PROGMEM
- #define PROGMEM __attribute__(( section(".progmem.data") ))
- #undef PSTR
- #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
- #include "Arduino.h"
- #include <WiFlyHQ.h>
- #include <SoftwareSerial.h>
- SoftwareSerial wifiSerial(2,3);
- WiFly wifly;
- void pag_principal();
- void pag_analogica();
- void pag_ola(char *name);
- void erro404();
- char buf[80];
- boolean ledstatus = false;
- // Altere as duas variáveis abaixo de acordo com sua rede Wifi
- const char rede[] = "INTEL";
- const char senha[] = "12345678";
- void setup()
- {
- pinMode(11, OUTPUT);
- pinMode(10, OUTPUT);
- Serial.begin(115200);
- Serial.println(F("Inicializando"));
- Serial.print(F("Memoria livre: "));
- Serial.println(wifly.getFreeMemory(),DEC);
- wifiSerial.begin(9600);
- if (!wifly.begin(&wifiSerial, &Serial)) {
- Serial.println(F("Falha ao inicializar o wifly"));
- wifly.terminal();
- }
- //Se não estiver associado a uma rede configura a rede no módulo
- if (!wifly.isAssociated())
- {
- Serial.println(F("Conectando..."));
- wifly.setSSID(rede);
- wifly.setPassphrase(senha);
- wifly.enableDHCP();
- wifly.save();
- if (wifly.join())
- {
- Serial.println(F("Conectado a Rede"));
- }
- else
- {
- Serial.println(F("Falha ao conectar na rede"));
- wifly.terminal();
- }
- }
- else
- {
- Serial.println(F("Pronto para entrar na rede"));
- }
- wifly.setBroadcastInterval(0); // Desliga UPD broadcast
- Serial.print(F("MAC: "));
- Serial.println(wifly.getMAC(buf, sizeof(buf)));
- Serial.print(F("IP: "));
- Serial.println(wifly.getIP(buf, sizeof(buf)));
- wifly.setDeviceID("ShieldWiFi");
- if (wifly.isConnected())
- {
- Serial.println(F("Fechando outras conexões ativas"));
- wifly.close();
- }
- wifly.setProtocol(WIFLY_PROTOCOL_TCP);
- if (wifly.getPort() != 80)
- {
- wifly.setPort(80);
- wifly.save();
- Serial.println(F("Trocando para a porta 80, reiniciando..."));
- wifly.reboot(); // Reiniciar o módulo para alterar a número da porta
- delay(3000);
- }
- Serial.println(F("Pronto"));
- }
- void loop()
- {
- if (wifly.available() > 0)
- {
- if (wifly.gets(buf, sizeof(buf)))
- {
- if (strncmp_P(buf, PSTR("GET / "), 6) == 0)
- //Verifica se é um GET na página principal - 192.168.1.9/
- {
- while (wifly.gets(buf, sizeof(buf)) > 0)
- {
- // Ignora o restante da requisição
- }
- pag_principal(); // Pula para a rotina que imprime a página principal
- }
- else if (strncmp_P(buf, PSTR("GET /A"), 6) == 0)
- //Verifica se é um GET na página de leituras analógicas - 192.168.1.9/A
- {
- while (wifly.gets(buf, sizeof(buf)) > 0)
- {
- // Ignora o restante da requisição
- }
- pag_analogica(); //Vai para rotina que imprime a página de leituras analógicas
- }
- else if (strncmp_P(buf, PSTR("POST"), 4) == 0) // Verifica se foi feito um POST
- {
- char username[16]; //Cria uma váriavel para armazenar o campo "Digite seu nome"
- if (wifly.match(F("user=")))
- {
- wifly.gets(username, sizeof(username));
- wifly.flushRx(); // Descarta o restante da transmissão
- pag_ola(username);
- }
- }
- else
- {
- wifly.flushRx(); // Descarta o restante da transmissão
- erro404(); //Envia página de erro - link não existe
- }
- }
- }
- }
- // Envia a página principal
- void pag_principal()
- {
- /* Envia Cabeçalho HTML*/
- wifly.println(F("HTTP/1.1 200 OK"));
- wifly.println(F("Content-Type: text/html"));
- wifly.println(F("Transfer-Encoding: chunked"));
- wifly.println();
- /* Envia o corpo do email com protocolo fragmentado
- * Com isto o cliente sabe quando a mensagem terminou
- */
- wifly.sendChunkln(F("<html>"));
- wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
- 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>"));
- wifly.sendChunkln(F("<p><center>Moises Wifi-Shield</center></p>"));
- wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.8/'\">Liga o LED no pino 10</button></center><p>"));
- wifly.sendChunkln(F("<p><center><form name=\"input\" action=\"/\" method=\"post\">"));
- wifly.sendChunkln(F("Digite seu nome"));
- wifly.sendChunkln(F("<input type=\"text\" name=\"user\" /></center></p>"));
- wifly.sendChunkln(F("<p><center><input type=\"submit\" value=\"Enviar\" /></center></p>"));
- wifly.sendChunkln(F("</form>"));
- wifly.sendChunkln(F("</html>"));
- wifly.sendChunkln();
- ledstatus = 1^ledstatus;
- digitalWrite(10,ledstatus); //Inverte o estado do LED ligado ao pino 10
- }
- // Envia a página com as leituras analógicas
- void pag_analogica()
- {
- // Envia Cabeçalho HTML
- wifly.println(F("HTTP/1.1 200 OK"));
- wifly.println(F("Content-Type: text/html"));
- wifly.println(F("Transfer-Encoding: chunked"));
- wifly.println();
- // Enviado página com codificada
- wifly.sendChunkln(F("<html>"));
- wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
- 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>"));
- wifly.sendChunkln(F("<p><center>Wifi-Shield</center></p>"));
- wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.8/'\">Liga o LED no pino 10</button></center><p>"));
- wifly.sendChunkln(buf);
- wifly.sendChunkln(F("</html>"));
- wifly.sendChunkln();
- }
- //Envia página que dá um Olá para os Garagistas
- void pag_ola(char *name)
- {
- // Envia Cabeçalho HTML
- wifly.println(F("HTTP/1.1 200 OK"));
- wifly.println(F("Content-Type: text/html"));
- wifly.println(F("Transfer-Encoding: chunked"));
- wifly.println();
- // Enviado página com codificada
- wifly.sendChunkln(F("<html>"));
- 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>"));
- wifly.sendChunkln(F("<p><center><button onclick=\"window.location.href='http://192.168.1.9/'\">Liga o LED no pino 10</button></center><p>"));
- wifly.sendChunk(F("<center><h1><p>Olá "));
- wifly.sendChunk(name);
- wifly.sendChunkln(F("</p></h1></center>"));
- wifly.sendChunkln(F("</html>"));
- wifly.sendChunkln();
- }
- // Envia página de erro caso seja digitado uma URL inválida
- void erro404()
- {
- // Envia Cabeçalho HTML
- wifly.println(F("HTTP/1.1 200 OK"));
- wifly.println(F("Content-Type: text/html"));
- wifly.println(F("Transfer-Encoding: chunked"));
- wifly.println();
- // Enviado página com codificada
- wifly.sendChunkln(F("<html><head>"));
- wifly.sendChunkln(F("<title></title>"));
- wifly.sendChunkln(F("</head><body>"));
- wifly.sendChunkln(F("<h1>Página não encontrada</h1>"));
- wifly.sendChunkln(F("</body></html>"));
- wifly.sendChunkln();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement