Hola, estoy haciendo un proyecto usando un ESP8266 1.0 y un Arduino UNO R3
el programa del ESP funciona perfectamente el encendido y apagado.
cuando recibe la instruccion via remota de un dispositivo android acciona el mecanismo ya sea encender o apagar, pero de igual forma quiero accionarlo manualmente. ahora bien
el modulo esp va con conexion tx y rx cruzados para su comunicacion
lo que se pretende hacer es que el esp accione el relay cuando en el servidor o en android reciba la url correcta (explicada en el codigo)
y que cuando se accione manualmente del apagador mande el comando por tx, rx al modulo esp y este se accione o se apague.
con arduino probe con un pulsador y si esta HIGH hace la accion para cambiar a LOW.
sin embargo con un pulsador se mantiene HIGH para cambiarlo a LOW pero al estar en constante HIGH por medio del modulo inalambrico no puede ser cambiado a LOW. no se si me explico.
es decir quiero que de ambas formas pueda encender y apagar ya sea con una aplicacion y de forma manual como cuando se usa una compuerta xor
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "wifigratis";
const char* password = "Totoro666";
ESP8266WebServer server(80);
const int output1 = 16;
const int output2 = 5;
const int output3 = 4;
const int output4 = 0;
const int output5 = 2;
const int output6 = 14;
const int output7 = 12;
const int output8 = 13;
boolean device1 = false;
boolean device2 = false;
boolean device3 = false;
boolean device4 = false;
boolean device5 = false;
boolean device6 = false;
boolean device7 = false;
boolean device8 = false;
void handleRoot() {
//digitalWrite(led, 1);
//server.send(200, "text/plain", "hello from esp8266!");
//digitalWrite(led, 0);
String cmd;
cmd += "<!DOCTYPE HTML>\r\n";
cmd += "<html>\r\n";
//cmd += "<header><title>ESP8266 Webserver</title><h1>\"ESP8266 Web Server Control\"</h1></header>";
cmd += "<head>";
cmd += "<meta http-equiv='refresh' content='5'/>";
cmd += "</head>";
if(device1){
cmd +=("
Device1 : APAGADO");
}
else{
cmd +=("
Device1 : ENCENDIDO");
}
if(device2){
cmd +=("
Device2 : APAGADO");
}
else{
cmd +=("
Device2 : ENCENDIDO");
}
if(device3){
cmd +=("
Device3 : APAGADO");
}
else{
cmd +=("
Device3 : ENCENDIDO");
}
if(device4){
cmd +=("
Device4 : APAGADO");
}
else{
cmd +=("
Device4 : ENCENDIDO");
}
if(device5){
cmd +=("
Device5 : APAGADO");
}
else{
cmd +=("
Device5 : ENCENDIDO");
}
if(device6){
cmd +=("
Device6 : APAGADO");
}
else{
cmd +=("
Device6 : ENCENDIDO");
}
if(device7){
cmd +=("
Device7 : APAGADO");
}
else{
cmd +=("
Device7 : ENCENDIDO");
}
if(device8){
cmd +=("
Device8 : APAGADO");
}
else{
cmd +=("
Device8 : ENCENDIDO");
}
cmd += "<html>\r\n";
server.send(200, "text/html", cmd);
}
void handleNotFound(){
//digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
//digitalWrite(led, 0);
}
void setup(void){
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
pinMode(output6, OUTPUT);
pinMode(output7, OUTPUT);
pinMode(output8, OUTPUT);
digitalWrite(output1, LOW);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
//Serial.begin(SSD1306
Serial.println(" ESP8266");
Serial.println("Control");
Serial.println("Version 0.1");
delay(2000);
Serial.println("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(ssid);
Serial.println(WiFi.localIP());
//Serial.setCursor(0,36); Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/status1=1", [](){
server.send(200, "text/plain", "device1 = APAGADO");
digitalWrite(output1, HIGH);
device1 = true;
Serial.println('a');
});
server.on("/status1=0", [](){
server.send(200, "text/plain", "device1 = ENCENDIDO");
digitalWrite(output1, LOW);
device1 = false;
Serial.println('b');
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
if(Serial.available()>0){
int datos = Serial.read();
if(datos == 'a'){
server.send(200, "text/plain", "device1 = APAGADO");
digitalWrite(output1, HIGH);
device1 = true;
Serial.println("ok"+datos);
}
if(datos == 'b'){
server.send(200, "text/plain", "device1 = ENCENDIDO");
digitalWrite(output1, LOW);
device1 = false;
Serial.println("ok"+datos);
}
}
}
ese codigo es del ESP 8266 y en el servidor y en android va perfecto pero quiero combiar arduino el codigo de este es
int boton1 = 2;
int foco1 = 0;
int salida = 0;
int estado = 0;
void setup() {
Serial.begin(115200);
Serial.println("Hola mundo por tecnopak");
delay(1000);
digitalWrite(boton1, INPUT);
}
void loop() {
//enviar datos
if (Serial.available() > 0) {
char datos2 = Serial.read();
estado = digitalRead(boton1);
if (datos2 == 'b') {
foco1 = 0;
Serial.println("Esta encendido el foco");
if(estado==HIGH){
Serial.println("a");
delay(1000);
}
}
if (datos2 == 'a') {
foco1 = 1;
Serial.println("esta apagado el foco");
if(estado==HIGH){
Serial.println("b");
delay(1000);
}
}
}
}
el diagrama improvisado que hize es
basicamente el esp controla la conexion y el relay
arduino debe controlar unicamente el interruptor manual que es un switch simple de casa.