Hi, an example where can I control the light intensity of led strip with a slider. I’m too using a PIR sensor and LDR with condition for the light stip turn on or turn off.LED.cpp (438 Bytes)
My device works automatically now. I used to use my own socket server to communicate device with an app, I could use work with to modes “manual” and “automatic” then If i choose mode manual in my app I could control the light intensity and state on/off through my app, if i choose automatic the device can work with the pir senso and ldr controlling the light automatically. But I would like work my device with thinger.io.
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxxx";
const char* clave = "xxxxxxxxxx";
//byte servidor[] = { 192,168,0,26 };
//byte servidor[] = { x.x.x.x};
WiFiClient cliente;
#define AUTOMATICO 0x1
#define MANUAL 0x0
byte MODO = 1;//AUTOMATICO, MANUAL
/* BEGIN */
int ledPin = D1; // LED connected to digital pin D0
int PirSensor = D2;
int SensorState;
int LDRVal;
/* END */
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, clave);
/**while(WiFi.status() != WL_CONNECTED){
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.print("IP:");
Serial.println(WiFi.localIP());
Serial.println("Iniciando Conexion con DomoServer.");
if (cliente.connect(servidor, 7070)) {
Serial.println("Conectado");
char opCode = 0xC1;
char sizeCode = 0x05;
char headCode = 0xF1;
char subCode = 0x02;
char id = 0x02;
char paquete[] = {opCode, sizeCode, headCode, subCode, id};
cliente.write_P(paquete, sizeof(paquete));
cliente.flush();
Serial.println("Envio Paquete Correctamente.");
} else {
Serial.println("Conexion fallo");
}
/* BEGIN */
pinMode(ledPin, OUTPUT);
pinMode(PirSensor, INPUT);
/* END */
}
void loop() {
if (cliente.available()) {
byte opCode = cliente.read();
byte sizeCoe = cliente.read();
byte headCode = cliente.read();
byte subCode = cliente.read();
switch(opCode){
case 0xC2:
switch(subCode){
case 0x01:
dispositivo();
break;
case 0x02:
if(MODO == MANUAL){
intensidad();
}
break;
case 0x03:
modo();
break;
}
break;
case 0xC3:
break;
}
}
if(MODO == AUTOMATICO){
automatico();
}
if (!cliente.connected()) {
Serial.println("Desconectando.");
cliente.stop();
setup();
}
}
void dispositivo(){
byte pin = cliente.read();
byte estado = cliente.read();
switch(estado){
case 0x00:
Serial.println("APAGADO.");
setFocoLed(0);
break;
case 0x01:
Serial.println("ENCENDIDO.");
setFocoLed(100);
break;
}
}
void intensidad(){
byte DEDvalor = cliente.read();
setFocoLed((float)DEDvalor);
}
void modo(){
MODO = cliente.read();
}
int contador = 20;
void automatico() {
SensorState = digitalRead(PirSensor);
Serial.print("SensorState:");
Serial.println(SensorState);
LDRVal = analogRead(A0);
Serial.print("Sensor Luz:");
Serial.println(LDRVal);
Serial.print("Conta:");
Serial.println(contador);
if(LDRVal >= 255){
setFocoLed(0);
} else if(contador <= 9 && SensorState != HIGH){
setFocoLed((float)(255 - (float)(contador * (float)28.33)));
contador++;
} else if(SensorState == HIGH){
setFocoLed(255);
contador = 1;
} else {
setFocoLed(LOW);
}
delay(1000);
}
void setFocoLed(float porcentaje){
float intensidad = (porcentaje / (float)255) * (float) 1023;
float value = 1023 - intensidad;
analogWrite(ledPin, value);
}