Led strip, LDR, PIR sensor + automatic and manual mode


#1

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);
}


#2

Hi everyone. I could change the work modes of my device (Automatic and Controled by the browser or app(Manual).

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define SSID "xxxxxxxxxx"
#define SSID_PASSWORD "xxxxxx"
#define usuario "xxxxxxxx"
#define device_id "xxxxxxxx"
#define device_credentials "xxxxxxxxxxx"

#define ledPin D1
#define PirSensor D2
//#define ledEstado D3

int SensorState, LDRVal;
boolean MODO;

ThingerESP8266 thing(usuario, device_id, device_credentials);

void setup() {
  // put your setup code here, to run once:
  thing.add_wifi(SSID, SSID_PASSWORD);
  
  pinMode(ledPin,OUTPUT);
  pinMode(PirSensor, INPUT);

  

   thing["ledPin"] << [](pson& in){
    if(in.is_empty()){
      in = (bool) digitalRead(ledPin);
    }
   else{
      digitalWrite(ledPin, in ? LOW : HIGH);
    }
    };
    thing["LED"] << analogPin(ledPin);

    thing["modo"] << inputValue(MODO);
  
   
  
}

void loop() {
  
  thing.handle();
  switch(MODO)
  {
    case 1:
    automatico();
    break;
    case 0:
    thing.handle();
    break;
    case 3:
    break;
    
  }
  

}
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);
}

I need ur help, coz I wish to app a toggle switch(a widget) for turn on/off the led, and if the led is turn on my slider widget changes.

I working with pir sensor and LDR too, in my code the value of lux depends on the current state of the environment, can I set this max value of lux through the web app?

I await your suggestions, thanks.

This is my device.


#3

Hello @Cessteel, congratulations for your project!

I suggest that your problem with the toggle can be solved using a global variable to store the value of the switch, and send this value in the resource when it’s empty. Something like this:

[...]
pson aux;
bool toggle;
   
void setup() {
  thing.add_wifi(SSID, SSID_PASSWORD);
  pinMode(switch_pin,INPUT);
 pinMode(led,OUTPUT);

 thing["sw"]<<[](pson &in){
     if(in.is_empty()){ //true when there is no data
      aux=(bool)digitalRead(switch_pin);
       in =aux; 
     }else{
       aux = in;  
       digitalWrite(led,in?HIGH:LOW);
     }
     toggle=aux;
 };
}

void loop() { 
  thing.handle();
  if(digitalRead(switch_pin)!=toggle)thing.stream(thing["sw"]);  //this will stream the resource only when the switch state changes
}

It is not possible to set any max value in the console, so you will have to filter your lux data.