Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://labdegaragem.com/forum/topics/alguem-me-ajuda-ajuntar-esses-dois-projeto-por-favor
- IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
- An IR detector/demodulator must be connected to the input RECV_PIN.
- Version 0.1 July, 2009
- Copyright 2009 Ken Shirriff
- http://arcfn.com
- #include <IRremote.h>
- #include <SerialRelay.h>
- int RECV_PIN = 11;
- const byte NumModules = 1;
- SerialRelay relays(8,9,NumModules);
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- void setup()
- {
- Serial.begin(9600);
- irrecv.enableIRIn(); // Start the receiver
- }
- void loop()
- {
- if (irrecv.decode(&results)) {
- Serial.println(results.value, DEC);
- if(results.value == 3041579055){
- relays.SetRelay(1, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(1, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- if(results.value == 3041546415){
- relays.SetRelay(2, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(2, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- if(results.value == 3041532645){
- relays.SetRelay(3, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(3, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- irrecv.resume(); // Receive the next value
- }
- }
- */
- //ROBOCORE TECNOLOGIA - Rele Serial Ethernet v.1.0
- //Código para utilizar o Módulo Relé Serial através de rede ethernet
- //O código abaixo requer o seguinte hardware:
- //01 x BlackBoard:
- //https://www.robocore.net/modules.php?name=GR_LojaVirtual&prod=530
- //01 x Arduino Shield - Ethernet:
- //https://www.robocore.net/modules.php?name=GR_LojaVirtual&prod=96
- //01 x Cartão de Memória MicroSD
- //https://www.robocore.net/modules.php?name=GR_LojaVirtual&prod=294
- //01 x Pacote com 10 Jumper Premium de 10 cm M/F
- //https://www.robocore.net/modules.php?name=GR_LojaVirtual&prod=365
- //até 10 x Módulos Rele Serial:
- //https://www.robocore.net/modules.php?name=GR_LojaVirtual&prod=663
- //É necessário instalar as seguintes bibliotecas:
- //Serial Relay:
- //https://github.com/RoboCore/SerialRelay
- //Para instalar a bliblioteca basta acessar o menu "Sketch > Include Library > Add .ZIP Library" na IDE do Arduino e selecionar o Arquivo SerialRelay.zip contido no mesmo ZIP deste arquivo.
- //Ethernet_v2
- //https://github.com/RoboCore/Ethernet
- //Para instalar a bliblioteca basta acessar o menu "Sketch > Include Library > Add .ZIP Library" na IDE do Arduino e selecionar o Arquivo Ethernet_v2_1.6.5.zip contido no mesmo ZIP deste arquivo.
- //Salve o arquivo index.htm (que veio junto com esse arquivo) em um cartão micro SD e o coloque no shield Ethernet.
- #include <SPI.h>
- #include <SD.h>
- #include <Ethernet_v2.h>
- #include <SerialRelay.h>
- #include <IRremote.h>
- int RECV_PIN = 11;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- //Define o Mac Address da placa de rede. Essa informação pode ser encontrada em uma etiqueta colada embaixo da mesma.
- byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xC6, 0x65 };
- //Define o IP da placa. Caso necessário altere o mesmo para se adequar a sua rede.
- IPAddress ip(192, 168, 0, 99);
- EthernetServer server(8484);
- File webFile;
- //na linha de baixo está sendo definido as portas em que o módulo relé serial está conectado.
- SerialRelay relays(8, 9, 3); // (pino de data, pino de clock, quantidade de módulos)
- #define REQ_BUF_SZ 60
- char HTTP_req[REQ_BUF_SZ] = {0};
- char req_index = 0;
- int estado_botao[41];
- char * pch;
- //---------------------------------------
- void setup()
- {
- //ESTADO INICIAL DOS BOTOES 0 -> desligado, 1 -> ligado:
- estado_botao[1] = 0;
- estado_botao[2] = 0;
- estado_botao[3] = 0;
- estado_botao[4] = 0;
- estado_botao[5] = 0;
- estado_botao[6] = 0;
- estado_botao[7] = 0;
- estado_botao[8] = 0;
- estado_botao[9] = 0;
- estado_botao[10] = 0;
- estado_botao[11] = 0;
- estado_botao[12] = 0;
- estado_botao[13] = 0;
- estado_botao[14] = 0;
- estado_botao[15] = 0;
- estado_botao[16] = 0;
- estado_botao[17] = 0;
- estado_botao[18] = 0;
- estado_botao[19] = 0;
- estado_botao[20] = 0;
- estado_botao[21] = 0;
- estado_botao[22] = 0;
- estado_botao[23] = 0;
- estado_botao[24] = 0;
- estado_botao[25] = 0;
- estado_botao[26] = 0;
- estado_botao[27] = 0;
- estado_botao[28] = 0;
- estado_botao[29] = 0;
- estado_botao[30] = 0;
- estado_botao[31] = 0;
- estado_botao[32] = 0;
- estado_botao[33] = 0;
- estado_botao[34] = 0;
- estado_botao[35] = 0;
- estado_botao[36] = 0;
- estado_botao[37] = 0;
- estado_botao[38] = 0;
- estado_botao[39] = 0;
- estado_botao[40] = 0;
- Serial.begin(9600);
- Serial.println("Inicializando cartao microSD...");
- if (!SD.begin(4))
- {
- Serial.println("ERRO - inicializacao do cartao falhou!");
- return;
- }
- Serial.println("SUCESSO - cartao microSD inicializado.");
- if (!SD.exists("index.htm"))
- {
- Serial.println("ERRO - index.htm nao foi encontrado!");
- return;
- }
- Serial.println("SUCESSO - Encontrado arquivo index.htm.");
- Ethernet.begin(mac, ip);
- server.begin();
- }
- //---------------------------------------
- void loop()
- {
- RemotoControl();
- EthernetClient client = server.available(); //verifica se existe alguém querendo se conectar
- if (client) // existe cliente?
- {
- boolean currentLineIsBlank = true;
- while (client.connected())
- {
- if (client.available()) // Existe informacao vinda do cliente
- {
- char c = client.read(); // Le cada byte enviado pelo cliente, ou seja, cada caracter.
- // Por padrao, o ultimo caracter enviado pelo cliente (nosso navegador) é em branco e termina com \n
- // Dessa forma conseguimos saber se o cliente acabou de enviar informacoes para o servidor (Arduino)
- if (req_index < (REQ_BUF_SZ - 1))
- {
- HTTP_req[req_index] = c; // salva os caracteres das solicitacoes do browser
- req_index++;
- }
- if (c == '\n' && currentLineIsBlank)
- {
- // envia para o cliente o protocolo padrao de sucesso HTTP
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close");
- client.println();
- //caso a request seja pela ação de um botão:
- if (StrContains(HTTP_req, "ajax_botao"))
- {
- for (int i = 1 ; i <= 40 ; i++)
- {
- //Serial.println(HTTP_req);
- char botao[] = "botaoxx";
- if (i < 10)
- {
- botao[5] = '0';
- botao[6] = '0' + i;
- } else {
- botao[5] = '0' + (i / 10);
- botao[6] = '0' + (i % 10);
- }
- //Serial.println(botao);
- if (StrContains(HTTP_req, botao))
- {
- SetBotao(i, client);
- }
- }
- }
- else {
- // grava no arquivo webFile a página que temos no microSD
- webFile = SD.open("index.htm");
- if (webFile) {
- while (webFile.available())
- {
- client.write(webFile.read()); // envia para o cliente a página - nessa linha de fato o Arduino imprime no browser a página
- }
- webFile.close();
- }
- }
- Serial.println(HTTP_req); //para debug, verifica no monitor serial a requisição
- req_index = 0; //reseta o index do buffer e a variável que armazena as requisições
- StrClear(HTTP_req, REQ_BUF_SZ);
- break;
- }
- // toda linha de texto recebida do cliente termina com \r\n
- if (c == '\n')
- {
- //verifica se acabou a linha, já que \n é o ultimo caracter
- currentLineIsBlank = true;
- }
- else if (c != '\r')
- {
- // o cliente ainda está enviando informações
- currentLineIsBlank = false;
- }
- }
- }
- delay(1); // dá um tempo para o browser receber os dados
- client.stop(); // fecha a conexão
- }
- }
- //---------------------------------------
- void RemotoControl()
- {
- if (irrecv.decode(&results)) {
- Serial.println(results.value, DEC);
- if (results.value == 3041579055)
- {
- relays.SetRelay(1, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(1, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- if (results.value == 3041546415)
- {
- relays.SetRelay(2, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(2, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- if (results.value == 3041532645)
- {
- relays.SetRelay(3, SERIAL_RELAY_ON, 1);
- delay(500);
- relays.SetRelay(3, SERIAL_RELAY_OFF, 1);
- delay(500);
- }
- irrecv.resume(); // Receive the next value
- }
- }
- //---------------------------------------
- void SetBotao(int botao, EthernetClient client)
- {
- int modulo = (botao - 1) / 4 + 1;
- int rele = botao % 4;
- if (rele == 0) {
- rele = 4;
- }
- Serial.print("modulo:");
- Serial.print(modulo);
- Serial.print(" rele:");
- Serial.print(rele);
- Serial.print(" - ");
- if (estado_botao[botao] == 0) {
- relays.SetRelay(rele, SERIAL_RELAY_ON, modulo);
- delay(500);
- estado_botao[botao] = 1;
- //client.print("1");
- Serial.println("1");
- relays.SetRelay(rele, SERIAL_RELAY_OFF, modulo);
- delay(500);
- estado_botao[botao] = 0;
- //client.print("0");
- Serial.println("0");
- }
- client.print("0|");
- for (int i = 1 ; i <= 40 ; i++)
- {
- client.print(estado_botao[i]);
- client.print("|");
- }
- }
- //---------------------------------------
- // funcao para limpar arrays (no nosso caso, as variaveis que armazenam requests)
- void StrClear(char *str, char length)
- {
- for (int i = 0; i < length; i++)
- {
- str[i] = 0;
- }
- }
- //---------------------------------------
- // funcao que procura pela string SFIND em STR
- // retorna 1 se a string for encontrada
- // retorna 0 se a setring não for encontrada
- char StrContains(char *str, char* sfind)
- {
- char found = 0;
- char index = 0;
- char len;
- len = strlen(str);
- if (strlen(sfind) > len)
- {
- return 0;
- }
- while (index < len)
- {
- if (str[index] == sfind[found])
- {
- found++;
- if (strlen(sfind) == found)
- {
- return 1;
- }
- }
- else
- {
- found = 0;
- }
- index++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement