Created
April 18, 2017 03:50
-
-
Save alvarowolfx/8a79acf3b6a32aafebd50e706239a26a to your computer and use it in GitHub Desktop.
Código utilizado na integração do Home Assistant e o acionamento direto de botões um controle remoto via ESP8266 via HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
platform: command_line | |
switches: | |
rf_sala_entrada: | |
friendly_name: "Luz Sala Entrada" | |
command_on: '/usr/bin/curl rfoutlet.local/lamp2On' | |
command_off: '/usr/bin/curl rfoutlet.local/lamp2Off' | |
rf_sala_sofa: | |
friendly_name: "Luz Sala Sofa" | |
command_on: '/usr/bin/curl rfoutlet.local/lamp1On' | |
command_off: '/usr/bin/curl rfoutlet.local/lamp1Off' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <ESP8266WebServer.h> | |
// Configurações WiFi | |
const char* ssid = "sua wifi"; | |
const char* password = "sua senha da wifi"; | |
//Porta para escutar as requisições | |
#define LISTEN_PORT 80 | |
// Portas conectadas no controle RF | |
#define LAMP1_ON_PIN D6 | |
#define LAMP1_OFF_PIN D5 | |
#define LAMP2_ON_PIN D4 | |
#define LAMP2_OFF_PIN D7 | |
// Delay entre acionamento do botão no controle | |
#define CLICK_DELAY 1000 | |
// Servidor web | |
ESP8266WebServer server(80); | |
// Funções de acionamento das lampadas, recebendo se devem ligar ou desligar | |
int lamp1(int state); | |
int lamp2(int state); | |
void setupWifi(){ | |
// Conecta na wifi | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi conectado"); | |
} | |
void setupServer(){ | |
// Start the server | |
server.begin(); | |
Serial.println("Server iniciado"); | |
// Print the IP address | |
Serial.println(WiFi.localIP()); | |
if (!MDNS.begin("rfoutlet")) { | |
Serial.println("Erro iniciando o serviço mDNS!"); | |
while(1) { | |
delay(1000); | |
} | |
MDNS.addService("http", "tcp", 80); | |
} | |
server.on("/lamp1On", [](){ | |
lamp1(HIGH); | |
server.send(200, "application/json", "{ \"message\": \"ok\" }"); | |
}); | |
server.on("/lamp1Off", [](){ | |
lamp1(LOW); | |
server.send(200, "application/json", "{ \"message\": \"ok\" }"); | |
}); | |
server.on("/lamp2On", [](){ | |
lamp2(HIGH); | |
server.send(200, "application/json", "{ \"message\": \"ok\" }"); | |
}); | |
server.on("/lamp2Off", [](){ | |
lamp2(LOW); | |
server.send(200, "application/json", "{ \"message\": \"ok\" }"); | |
}); | |
} | |
void setup(void) { | |
// Start Serial | |
Serial.begin(115200); | |
pinMode(LAMP1_ON_PIN, OUTPUT); | |
pinMode(LAMP1_OFF_PIN, OUTPUT); | |
pinMode(LAMP2_ON_PIN, OUTPUT); | |
pinMode(LAMP2_OFF_PIN, OUTPUT); | |
setupWifi(); | |
setupServer(); | |
} | |
void loop() { | |
server.handleClient(); | |
} | |
int lamp1(int state) { | |
if(state == LOW){ | |
digitalWrite(LAMP1_OFF_PIN, HIGH); | |
delay(CLICK_DELAY); | |
digitalWrite(LAMP1_OFF_PIN, LOW); | |
}else{ | |
digitalWrite(LAMP1_ON_PIN, HIGH); | |
delay(CLICK_DELAY); | |
digitalWrite(LAMP1_ON_PIN, LOW); | |
} | |
return 1; | |
} | |
int lamp2(int state) { | |
if(state == LOW){ | |
digitalWrite(LAMP2_OFF_PIN, HIGH); | |
delay(CLICK_DELAY); | |
digitalWrite(LAMP2_OFF_PIN, LOW); | |
}else{ | |
digitalWrite(LAMP2_ON_PIN, HIGH); | |
delay(CLICK_DELAY); | |
digitalWrite(LAMP2_ON_PIN, LOW); | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment