├── controle.ino ├── led.ino └── temperatura.ino /controle.ino: -------------------------------------------------------------------------------- 1 | #include // Biblioteca IRemote 2 | int RECV_PIN = 15; // Arduino pino D11 conectado no Receptor IR 3 | IRrecv irrecv(RECV_PIN); // criando a instância 4 | decode_results results; // declarando os resultados 5 | bool LED5, LED6, LED7 = false; // estado dos LEDs 6 | int atraso = 250; // atraso após ligar LED 7 | 8 | void setup() 9 | { 10 | Serial.begin(9600); // Monitor velocidade 9600 bps 11 | pinMode(19, OUTPUT); // LED vermelho no pino D07 12 | pinMode(18, OUTPUT); // LED azul no pino D06 13 | pinMode(4, OUTPUT); // LED verde no pino D05 14 | irrecv.enableIRIn(); // Inicializa a recepção de códigos 15 | } 16 | 17 | void loop() 18 | { 19 | results.value = 0; // zera os registradores 20 | if (irrecv.decode(&results)) // se algum código for recebido 21 | { 22 | Serial.println(results.value, HEX); // imprime o HEX Code 23 | irrecv.resume(); // reinicializa o receptor 24 | } 25 | if (results.value == 0xFFB04F) // pressione tecla 3 para controlar LED vermelho (D07) 26 | { 27 | LED7 = !LED7; // alterna o estado do LED D07 28 | digitalWrite(19, LED7); // acende ou apaga LED vermelho (D07) 29 | delay(atraso); // atraso de 250 ms 30 | } 31 | 32 | if (results.value == 0xFF18E7) // pressione tecla 2 para controlar LED azul (D06) 33 | { 34 | LED6 = !LED6; // alterna o estado do LED D06 35 | digitalWrite(18, LED6); // acende ou apaga LED vermelho (D07) 36 | delay(atraso); // atraso de 250 ms 37 | } 38 | 39 | if (results.value == 0xFF30CF) // pressione tecla 1 para controlar LED verde (D05) 40 | { 41 | LED5 = !LED5; // alterna o estado do LED D05 42 | digitalWrite(4, LED5); // acende ou apaga LED verde (D05) 43 | delay(atraso); // atraso de 250 ms 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /led.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Acionamento de lampada com MQTT Dash 4 | //Por: Fábio Souza 5 | //Data: 12/02/18 6 | 7 | #include 8 | //#include 9 | #include 10 | 11 | #define DEBUG 12 | 13 | #define L1 15 //pino de saida para acionamento da Lampada L1 14 | 15 | //informações da rede WIFI 16 | const char* ssid = "AndroidAP1336"; //SSID da rede WIFI 17 | const char* password = "snjt8055"; //senha da rede wifi 18 | 19 | //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT 20 | const char* mqttServer = "192.168.43.251"; //server 21 | const char* mqttUser = "user"; //user 22 | const char* mqttPassword = "user"; //password 23 | const int mqttPort = 1883; //port 24 | const char* mqttTopicSub ="led/L1"; //tópico que sera assinado 25 | 26 | 27 | WiFiClient espClient; 28 | PubSubClient client(espClient); 29 | 30 | void setup() { 31 | 32 | Serial.begin(115200); 33 | pinMode(L1, OUTPUT); 34 | 35 | WiFi.begin(ssid, password); 36 | 37 | while (WiFi.status() != WL_CONNECTED) { 38 | delay(500); 39 | #ifdef DEBUG 40 | Serial.println("Conectando ao WiFi.."); 41 | #endif 42 | } 43 | #ifdef DEBUG 44 | Serial.println("Conectado na rede WiFi"); 45 | #endif 46 | 47 | client.setServer(mqttServer, mqttPort); 48 | client.setCallback(callback); 49 | 50 | while (!client.connected()) { 51 | #ifdef DEBUG 52 | Serial.println("Conectando ao Broker MQTT..."); 53 | #endif 54 | 55 | if (client.connect("ESP8266Client", mqttUser, mqttPassword )) { 56 | #ifdef DEBUG 57 | Serial.println("Conectado"); 58 | #endif 59 | 60 | } else { 61 | #ifdef DEBUG 62 | Serial.print("falha estado "); 63 | Serial.print(client.state()); 64 | #endif 65 | delay(2000); 66 | 67 | } 68 | } 69 | 70 | //subscreve no tópico 71 | client.subscribe(mqttTopicSub); 72 | 73 | } 74 | 75 | void callback(char* topic, byte* payload, unsigned int length) { 76 | 77 | //armazena msg recebida em uma sring 78 | payload[length] = '\0'; 79 | String strMSG = String((char*)payload); 80 | 81 | #ifdef DEBUG 82 | Serial.print("Mensagem chegou do tópico: "); 83 | Serial.println(topic); 84 | Serial.print("Mensagem:"); 85 | Serial.print(strMSG); 86 | Serial.println(); 87 | Serial.println("-----------------------"); 88 | #endif 89 | 90 | //aciona saída conforme msg recebida 91 | if (strMSG == "1"){ //se msg "1" 92 | digitalWrite(L1, LOW); //coloca saída em LOW para ligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 93 | }else if (strMSG == "0"){ //se msg "0" 94 | digitalWrite(L1, HIGH); //coloca saída em HIGH para desligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 95 | } 96 | 97 | } 98 | 99 | //função pra reconectar ao servido MQTT 100 | void reconect() { 101 | //Enquanto estiver desconectado 102 | while (!client.connected()) { 103 | #ifdef DEBUG 104 | Serial.print("Tentando conectar ao servidor MQTT"); 105 | #endif 106 | 107 | bool conectado = strlen(mqttUser) > 0 ? 108 | client.connect("ESP8266Client", mqttUser, mqttPassword) : 109 | client.connect("ESP8266Client"); 110 | 111 | if(conectado) { 112 | #ifdef DEBUG 113 | Serial.println("Conectado!"); 114 | #endif 115 | //subscreve no tópico 116 | client.subscribe(mqttTopicSub, 1); //nivel de qualidade: QoS 1 117 | } else { 118 | #ifdef DEBUG 119 | Serial.println("Falha durante a conexão.Code: "); 120 | Serial.println( String(client.state()).c_str()); 121 | Serial.println("Tentando novamente em 10 s"); 122 | #endif 123 | //Aguarda 10 segundos 124 | delay(10000); 125 | } 126 | } 127 | } 128 | 129 | void loop() { 130 | if (!client.connected()) { 131 | reconect(); 132 | } 133 | client.loop(); 134 | } 135 | -------------------------------------------------------------------------------- /temperatura.ino: -------------------------------------------------------------------------------- 1 | //Acionamento de lampada com MQTT Dash 2 | //Por: Fábio Souza 3 | //Data: 19/03/18 4 | 5 | #include 6 | #include 7 | #include "DHT.h" 8 | 9 | #define INTERVALO_ENVIO 10000 10 | 11 | #define DHTPIN 4 // o sensor dht11 foi conectado ao pino 2( D4 do node MCU) 12 | #define DHTTYPE DHT22 13 | 14 | DHT dht(DHTPIN, DHTTYPE); 15 | 16 | #define DEBUG 17 | 18 | #define L1 4 //pino de saida para acionamento da Lampada L1 19 | 20 | //informações da rede WIFI 21 | const char* ssid = "AndroidAP"; //SSID da rede WIFI 22 | const char* password = "kizi3184"; //senha da rede wifi 23 | 24 | //informações do broker MQTT - Verifique as informações geradas pelo CloudMQTT 25 | const char* mqttServer = "m16.cloudmqtt.com"; //server 26 | const char* mqttUser = "shfldxvc"; //user 27 | const char* mqttPassword = "SVLnPuDrNmlb"; //password 28 | const int mqttPort = 19759; //port 29 | const char* mqttTopicSub ="weather/L1"; //tópico que sera assinado 30 | 31 | int ultimoEnvioMQTT = 0; 32 | 33 | WiFiClient espClient; 34 | PubSubClient client(espClient); 35 | 36 | void setup() { 37 | 38 | Serial.begin(115200); 39 | pinMode(L1, OUTPUT); 40 | 41 | WiFi.begin(ssid, password); 42 | 43 | while (WiFi.status() != WL_CONNECTED) { 44 | delay(500); 45 | #ifdef DEBUG 46 | Serial.println("Conectando ao WiFi.."); 47 | #endif 48 | } 49 | #ifdef DEBUG 50 | Serial.println("Conectado na rede WiFi"); 51 | #endif 52 | 53 | client.setServer(mqttServer, mqttPort); 54 | client.setCallback(callback); 55 | 56 | while (!client.connected()) { 57 | #ifdef DEBUG 58 | Serial.println("Conectando ao Broker MQTT..."); 59 | #endif 60 | 61 | if (client.connect("ESP8266Client", mqttUser, mqttPassword )) { 62 | #ifdef DEBUG 63 | Serial.println("Conectado"); 64 | #endif 65 | 66 | } else { 67 | #ifdef DEBUG 68 | Serial.print("falha estado "); 69 | Serial.print(client.state()); 70 | #endif 71 | delay(2000); 72 | 73 | } 74 | } 75 | 76 | //subscreve no tópico 77 | client.subscribe(mqttTopicSub); 78 | 79 | dht.begin(); 80 | 81 | } 82 | 83 | void callback(char* topic, byte* payload, unsigned int length) { 84 | 85 | //armazena msg recebida em uma sring 86 | payload[length] = '\0'; 87 | String strMSG = String((char*)payload); 88 | 89 | #ifdef DEBUG 90 | Serial.print("Mensagem chegou do tópico: "); 91 | Serial.println(topic); 92 | Serial.print("Mensagem:"); 93 | Serial.print(strMSG); 94 | Serial.println(); 95 | Serial.println("-----------------------"); 96 | #endif 97 | 98 | //aciona saída conforme msg recebida 99 | if (strMSG == "1"){ //se msg "1" 100 | digitalWrite(L1, LOW); //coloca saída em LOW para ligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 101 | }else if (strMSG == "0"){ //se msg "0" 102 | digitalWrite(L1, HIGH); //coloca saída em HIGH para desligar a Lampada - > o módulo RELE usado tem acionamento invertido. Se necessário ajuste para o seu modulo 103 | } 104 | 105 | } 106 | 107 | //função pra reconectar ao servido MQTT 108 | void reconect() { 109 | //Enquanto estiver desconectado 110 | while (!client.connected()) { 111 | #ifdef DEBUG 112 | Serial.print("Tentando conectar ao servidor MQTT"); 113 | #endif 114 | 115 | bool conectado = strlen(mqttUser) > 0 ? 116 | client.connect("ESP8266Client", mqttUser, mqttPassword) : 117 | client.connect("ESP8266Client"); 118 | 119 | if(conectado) { 120 | #ifdef DEBUG 121 | Serial.println("Conectado!"); 122 | #endif 123 | //subscreve no tópico 124 | client.subscribe(mqttTopicSub, 1); //nivel de qualidade: QoS 1 125 | } else { 126 | #ifdef DEBUG 127 | Serial.println("Falha durante a conexão.Code: "); 128 | Serial.println( String(client.state()).c_str()); 129 | Serial.println("Tentando novamente em 10 s"); 130 | #endif 131 | //Aguarda 10 segundos 132 | delay(10000); 133 | } 134 | } 135 | } 136 | 137 | void loop() { 138 | if (!client.connected()) { 139 | reconect(); 140 | } 141 | 142 | //envia a cada X segundos 143 | if ((millis() - ultimoEnvioMQTT) > INTERVALO_ENVIO) 144 | { 145 | enviaDHT(); 146 | ultimoEnvioMQTT = millis(); 147 | } 148 | 149 | client.loop(); 150 | } 151 | 152 | //função para leitura do DHT11 153 | void enviaDHT(){ 154 | 155 | char MsgUmidadeMQTT[10]; 156 | char MsgTemperaturaMQTT[10]; 157 | 158 | float umidade = dht.readHumidity(); 159 | float temperatura = dht.readTemperature(); 160 | 161 | 162 | if (isnan(temperatura) || isnan(umidade)) 163 | { 164 | #ifdef DEBUG 165 | Serial.println("Falha na leitura do dht11..."); 166 | #endif 167 | } 168 | else 169 | { 170 | #ifdef DEBUG 171 | Serial.print("Umidade: "); 172 | Serial.print(umidade); 173 | Serial.print(" \n"); //quebra de linha 174 | Serial.print("Temperatura: "); 175 | Serial.print(temperatura); 176 | Serial.println(" °C"); 177 | #endif 178 | 179 | sprintf(MsgUmidadeMQTT,"%f",umidade); 180 | client.publish("casa/umidade", MsgUmidadeMQTT); 181 | sprintf(MsgTemperaturaMQTT,"%f",temperatura); 182 | client.publish("casa/temperatura", MsgTemperaturaMQTT); 183 | } 184 | } 185 | --------------------------------------------------------------------------------