esp32 lights on and off, interner and IR

vars161
Posts: 1
Joined: Sat Apr 28, 2018 1:49 pm

esp32 lights on and off, interner and IR

Postby vars161 » Sat Apr 28, 2018 1:57 pm

good afternoon
I'm new to programming arduino \ esp32 and needed help, please
I'm doing a project to turn lights on and off my home with esp32 and relay.
I wanted to be able to control by the internet and also by infrared, I already have the two separate codes to work, however I wanted to join the two codes in an esp32, I already tried to join the two but the reluctant ones are turning it off and on non stop.
The other question is, how do I set the esp32 ip?



#include <IRremote.h>
int RECV_PIN = 23; // Diz que o receptor IR esta conectado no pino 23
IRrecv irrecv(RECV_PIN);
decode_results results;

#include <WiFi.h>

//Configuración de red
const char* ssid = “casa”;
const char* password = “xxxxxx”;

WiFiServer server(80); //Servidor web

const int LED1 = 18; //Pines de cada LED
const int LED2 = 19;

//Variables Cliente
char linebuf[80];
int charcount=0;

void setup()
{
Serial.begin(115200); // Abre a porta serial.
pinMode(18, OUTPUT); // Define o pino 18 como saida.
irrecv.enableIRIn(); // Inicia o receptor IR.

}

{
//Inicializamos el puerto sere
Serial.begin(115200);

//Configuramos los pines como salida.
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);

Serial.printf(“Conectando a: %s\n”, ssid);

WiFi.begin(ssid, password);

// Intentamos que se conecte a la red wifi
while(WiFi.status() != WL_CONNECTED) {
Serial.println(“Conectando…”);
delay(2000);
}

Serial.print(“Conectado. “);
Serial.print(” Dirección IP del módulo: “);
Serial.println(WiFi.localIP());

server.begin(); //Iniciamos el servidor web.
}

void loop() {
// Esperamos a que se conecte un cliente
WiFiClient client = server.available();
if (client) {
Serial.println(“Nuevo Cliente”);
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// Una petición http termina con línea en blanco.
boolean currentLineIsBlank = true;

while (client.connected()) { //Mientras que el cliente está conectado
if (client.available()) { //Si se recibe algun dato del cliente.
char c = client.read();
Serial.write(c);
//Lee caracter por caracter la petición http
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// si se recibe un caracter de nueva línea (/n) y la siguiente línea
// es una líne en blanco entonces la petición http ha terminado y podemos
// responder al cliente

if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println();
client.println(“<!DOCTYPE HTML><html><head>”);
client.println(“<body style=\”background-color:#0066aa;\”>”);
client.println(“<meta name=\”viewport\” content=\”width=device-width, initial-scale=1\”></head>”);
client.println(“<h1 style=\”width:500px;height:20px;color:#ffffff;\”>Tutorial Web Server ESP32 </h1>”);
client.println(“<p style=\”color:#ffffff\”>LED #1 <button>ON</button> <button>OFF</button></p>”);
client.println(“<p style=\”color:#ffffff\”>LED #2 <button>ON</button> <button>OFF</button></p>”);
client.println(“</html>”);
break;
}

if (c == ‘\n’) {
// Empieza una nueva línea
currentLineIsBlank = true;
if (strstr(linebuf,”GET /on1″) > 0){
Serial.println(“LED 1 ON”);
digitalWrite(LED1, HIGH);
}
else if (strstr(linebuf,”GET /off1″) > 0){
Serial.println(“LED 1 OFF”);
digitalWrite(LED1, LOW);
}
else if (strstr(linebuf,”GET /on2″) > 0){
Serial.println(“LED 2 ON”);
digitalWrite(LED2, HIGH);
}
else if (strstr(linebuf,”GET /off2″) > 0){
Serial.println(“LED 2 OFF”);
digitalWrite(LED2, LOW);
}
// Empieza una nueva línea
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
} else if (c != ‘\r’) {
// Todavía queda un caracter en la línea actual
currentLineIsBlank = false;
}
}
}

delay(1); // Esperamos un poco para que el navegador reciba los datos.

// Cerramos la conexión con el cliente
client.stop();
Serial.println(“Cliente desconectado”);
}

void loop(){
if (irrecv.decode(&results)) {
Serial.println(results.value, DEC); // Envia o codigo do botão pressionado para a porta serial.
irrecv.resume();
}
if (results.value == 16724175){ digitalWrite(18,HIGH);} // Se o receptor IR receber o codigo “70” ira Acionar o Relé.
delay (100);
if (results.value == 16718055){ digitalWrite(18,LOW);} // Se o receptor IR receber o codigo “60” ira Desligar o Relé.

}}

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32 lights on and off, interner and IR

Postby ESP_Sprite » Sun Apr 29, 2018 3:35 am

Moved to the Arduino subforum.

Who is online

Users browsing this forum: No registered users and 57 guests