/********************************************************************************** ************************************BIBLIOTECAS************************************ **********************************************************************************/ #include #include #include int rele1 = A1; // Define rele1 para a porta A0. String readString = String(30); String StatusRele; /********************************************************************************** ********************************FIM BIBLIOTECAS************************************ **********************************************************************************/ /********************************************************************************** *************************ROTINAS USUARIO E SENHA*********************************** ***********************************************************************************/ boolean validar_usuario(char * linebuf) { /* nesse exemplo o usuario e senha estão definidos dentro do código fonte. mas o usuário e senha poderiam ser autenticados de diversas maneiras, lendo os dados de um servidor web, arquivo texto, etc, bastando apenas atribuir o valor lido para a variável usuario_senha. */ char usuario_senha[] = "admin:admin"; int t = strlen(usuario_senha); int tamanhoEnc = (((t-1) / 3) + 1) * 4; //tamanho da string codificada char out[tamanhoEnc]; base64_encode(out, usuario_senha, t+1 ); //---desconta é usado pra eliminar os caracteres '=' int desconta = 0; if ((t%3) == 1) { desconta = 2; } if ((t%3) == 2) { desconta = 1; } char out2[tamanhoEnc-desconta]; byte i; for (i=0; i<(tamanhoEnc-desconta);i++){ out2[i] = out[i]; } out2[i] = '\0'; return ( strstr(linebuf, out2)>0 ); } /********************************************************************************** *************************FIM ROTINA USUARIO E SENHA******************************** ***********************************************************************************/ /********************************************************************************** ***********************************PAGINAS HTML************************************ ***********************************************************************************/ EthernetServer * server; void iniciar_ethernet(){ byte ip[4] = {192,168,86,170}; byte gateway[4] = {192,168,86,1}; byte subnet[4] = {255,255,255,0}; byte mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; int porta = 8888; server = new EthernetServer(porta); //Ethernet.begin(mac, ip, gateway, subnet); //caso necessario gateway utilizar essa linha Ethernet.begin(mac, ip); server->begin(); } void html_logoff(EthernetClient &client){ client.print(F( "HTTP/1.1 401 Authorization Required\n" "Content-Type: text/html\n" "Connnection: close\n\n" "\n" "Logoff\n" "" //IE logoff "" "

Voce nao esta mais conectado

\n")); } void html_autenticar(EthernetClient &client) { client.print(F("HTTP/1.1 401 Authorization Required\n" "WWW-Authenticate: Basic realm=\"Area Restrita\"\n" "Content-Type: text/html\n" "Connnection: close\n\n" "\n" "Error\n" "

401 Acesso nao autorizado

\n")); } void html_autenticado(EthernetClient &client){ client.println(F("HTTP/1.1 200 OK\n" "Content-Type: text/html\n" "Connection: keep-alive\n\n" "" "")); client.println(""); client.println("Deo Master"); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println("
"); client.println("Android"); client.println(" & "); client.println("Arduino
"); //=======================================================SENSOR ON OFF 1 if(digitalRead(rele1)) { StatusRele = "Desligado"; } else { StatusRele = "Ligado"; } //=======================================================BOTÃO HTML 1 client.println("
"); client.println(" "); client.println("

"); client.println(F("
Logoff")); client.println("
"); client.println(""); client.println(""); readString = ""; client.stop(); /* for (int analogChannel = 0; analogChannel < 6; analogChannel++) { int sensorReading = analogRead(analogChannel); client.print(F("analog input ")); client.print(analogChannel); client.print(F(" is ")); client.print(sensorReading); client.println(F("
")); } */ } void exec_ethernet(){ EthernetClient client = server->available(); if (client) { char linebuf[80]; memset(linebuf, 0, sizeof(linebuf)); int charCount = 0; boolean autenticado = false; boolean currentLineIsBlank = true; boolean logoff = false; while (client.connected()) { if (client.available()) { char c = client.read(); linebuf[charCount] = c; if (charCount0 ) { logoff = true; } if (strstr(linebuf, "Authorization: Basic")>0 ) { if ( validar_usuario(linebuf) ) { autenticado = true; } } memset(linebuf, 0, sizeof(linebuf)); charCount = 0; } else if (c != '\r') { currentLineIsBlank = false; // you've gotten a character on the current line } } } delay(1); // give the web browser time to receive the data client.stop(); // close the connection: } } /********************************************************************************** **************************************** FIM PAGINAS HTML************************** ***********************************************************************************/ /********************************************************************************** *************************BASE 64 CODE/DECODE DO USUARIO E SENHA******************** ***********************************************************************************/ void a3_to_a4(unsigned char * a4, unsigned char * a3); void a4_to_a3(unsigned char * a3, unsigned char * a4); unsigned char b64_lookup(char c); int base64_encode(char *output, char *input, int inputLen) { const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int i=0, j=0, encLen=0; unsigned char a3[3], a4[4]; while(inputLen--) { a3[i++] = *(input++); if(i == 3) { a3_to_a4(a4, a3); for(i=0; i<4; i++) { output[encLen++] = b64_alphabet[a4[i]]; } i = 0; } } if (i) { for(j = i; j < 3; j++) { a3[j] = '\0'; } a3_to_a4(a4, a3); for(j = 0; j < i + 1; j++) { output[encLen++] = b64_alphabet[a4[j]]; } while((i++ < 3)) { output[encLen++] = '='; } } output[encLen] = '\0'; return encLen; } /* int base64_decode(char * output, char * input, int inputLen) { int i = 0, j = 0; int decLen = 0; unsigned char a3[3]; unsigned char a4[4]; while (inputLen--) { if (*input == '=') { break; } a4[i++] = *(input++); if (i == 4) { for (i = 0; i <4; i++) { a4[i] = b64_lookup(a4[i]); } a4_to_a3(a3,a4); for (i = 0; i < 3; i++) { output[decLen++] = a3[i]; } i = 0; } } if (i) { for (j=i; j<4; j++) { a4[j] = '\0'; } for (j=0; j<4; j++) { a4[j] = b64_lookup(a4[j]);} a4_to_a3(a3,a4); for (j=0; j='a' && c <='z') return c - 71; if(c >='0' && c <='9') return c + 4; if(c == '+') return 62; if(c == '/') return 63; return -1; } /********************************************************************************** *************************FIM BASE 64 CODE/DECODE DO USUARIO E SENHA**************** ***********************************************************************************/ /********************************************************************************** **************************************** VOID / LOOP ****************************** ***********************************************************************************/ void setup() { pinMode(rele1, OUTPUT); //Define rele1 como saída. Serial.begin(9600); iniciar_ethernet(); } void loop() { EthernetClient client = server.available(); if(client) while(client.connected()) { if(client.available()) { char c = client.read(); if(readString.length() < 30) { readString += (c); } if (c == '\n') { } } } //=======================================================BOTÃO 1 if(readString.indexOf("rele1") >= 0){ digitalWrite(rele1, !digitalRead(rele1)); } exec_ethernet(); } /********************************************************************************** *************************************FIM VOID / LOOP******************************* ***********************************************************************************/