Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #undef PROGMEM
- #define PROGMEM __attribute__(( section(".progmem.data") ))
- #undef PSTR
- #define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))
- #include <String.h>
- #include <Ethernet.h>
- #include "Arduino.h"
- #include <WiFlyHQ.h>
- #include <SoftwareSerial.h>
- SoftwareSerial wifiSerial(2,3);
- WiFly wifly;
- void pag_principal();
- void erro404();
- char buf[80];
- const char rede[] = "000000000000000";
- const char senha[] = "00000000000000";
- String readString;////////////////////////////////////////////////////////
- int flagLed1=0; //Flag para comando do Led 1///////////////////////////////
- void setup()
- {
- pinMode(9, 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...")); //------ Era 80 foi 80
- wifly.reboot(); // Reiniciar o módulo para alterar a número da porta
- delay(3000);
- }
- Serial.println(F("Pronto"));
- }
- void loop()
- {
- //====================================================================================================
- {
- Serial.println(readString); //Mostra no Serial Monitor o debuging
- // HTML data header
- if(readString.indexOf('?') >=0) {
- wifly.println("HTTP/1.1 Controle de LED's");
- wifly.println();
- wifly.println();
- }
- else {
- /*LED 1*/
- if(readString.indexOf("led1on") >0) //checa se o led 1 foi aceso
- {
- flagLed1=1;
- digitalWrite(9, 1); // set pin 9 high
- Serial.println("Led1 On");
- }
- if(readString.indexOf("led1off") >0) //checa se o led 1 foi apagado
- {
- flagLed1=0;
- digitalWrite(9, 0); // set pin 9 low
- Serial.println("Led1 Off");
- }
- //====================================================================================================
- if (wifly.available() > 0)
- {
- if (wifly.gets(buf, sizeof(buf)))
- {
- if (strncmp_P(buf, PSTR("GET / "), 6) == 0)
- {
- 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
- }
- {
- while (wifly.gets(buf, sizeof(buf)) > 0)
- {
- }
- }
- {
- {
- }
- }
- {
- wifly.flushRx(); // Descarta o restante da transmissão
- erro404();
- }
- }
- }
- }
- }
- }
- 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();
- //==================================================================================================================================
- //===================================================================================================================================
- wifly.sendChunkln(F("<html>"));
- wifly.sendChunkln(F("<title>Wifi-Shield</title>"));
- //==================================================================================================================================
- //Led 1 - controle de imagem
- if(flagLed1==0)
- {
- wifly.sendChunkln(F("<table border='10px' align='center'><tr><td><input type=submit value='LIGAR AREA'"));
- wifly.sendChunkln(F("style=width:130px;height:45px onclick=\"document.location='led1on'\"/></td>"));
- wifly.sendChunkln(F("<td><img alt='Apagada' src='http://imagizer.imageshack.us/v2/100x75q90/924/nSzZ50.jpg' width='80px'/></td></tr>"));
- }
- else
- {
- wifly.sendChunkln(F("<tr><td><input type=submit value='DESLIGAR SALA' style=width:130px;height:45px onclick=\"document.location='led1off'\"/> </td>"));
- wifly.sendChunkln(F("<td><img alt='Ligada' src='http://imagizer.imageshack.us/v2/100x75q90/922/vJbJI5.jpg' width='80px'/></td></tr>"));
- }
- //==================================================================================================================================
- }
- void erro404()
- {
- wifly.println(F("HTTP/1.1 200 OK"));
- wifly.println(F("Content-Type: text/html"));
- wifly.println(F("Transfer-Encoding: chunked"));
- wifly.println();
- 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