ESP-12 & RGB LED STRIP & Pir sensor & LDR

221 views
Skip to first unread message

Adrián Sanz Rodrigo

unread,
Jul 2, 2016, 7:08:20 AM7/2/16
to souliss
Hi! I want that when motion is detected and a limit of low lumonisty is reached, the rgb strip turns on (no matter which color) , but I can't even turn on the strip when motion is detected. Here's my code: thank you

/*-Wiring: ADC pin from ESP have a range from 0 to 1v, so we need to use a voltage divider to down the range from 3.3v to 1, I tried with 220ohm and 100ohm resistors (1.031v) and works well.
-Scheme:  (Sorry by this, I explain better if needed)
3.3v --- 220ohm ---- LDR ---- 100 ohm ----- GND
                             LDR ----  -  ADC ----- 10kohm ------ GND

/**************************************************************************
    Souliss - RGB LED Strip control from an ESP8266
    
    This is a RGB LED Strip Driver/Controller driven by the Souliss Framework.  
    
    Load this code on ESP8266 board using the porting of the Arduino core
    for this platform.
  
  The 3 output control 3 IRLZ44N Mosfet.
  In the actual configuration the channel are configured in this way:
  Red   channel = GPIO14
  Green channel = GPIO5
  Blue  channel = GPIO4
  
  Code is not yet optimised for any energy saving and will always be active.
  DHCP is used to get the IP address. Use serial output (76800) to get the actual IP adress.
        
*/
                             
// Configure the framework
#include "bconf/MCU_ESP8266.h"              // Load the code directly on the ESP8266
#include "conf/Gateway.h"                   // The main node is the Gateway, we have just one node
#include "conf/IPBroadcast.h"

// Define the WiFi name and password
#define WIFICONF_INSKETCH
#define WiFi_SSID               "xxx"
#define WiFi_Password           "xx" 
#define Gateway_address 0x6501              
#define Peer_address    0x6504
#define myvNet_subnet   0xFF00 

// Include framework code and libraries
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include "Souliss.h"

// This identify the number of the LED logic
// **** Define here the right pin for your ESP module **** 
#define  OUTPUTPIN     BUILTIN_LED

#define LEDCONTROL          0               // This is the memory slot for the logic that handle the light
#define LEDRED              1               // This is the memory slot for the logic that handle the light
#define LEDGREEN            2               // This is the memory slot for the logic that handle the light
#define LEDBLUE             3               // This is the memory slot for the logic that handle the light
#define LDR                 4

//PINES
#define SensorLDR_pin   A0
#define PIR_PIN         12

// Light calibration data
// out[] holds the values wanted in lux/10
#define sizeofarray 9   // Number of items on out and in arrays
static const unsigned int out[] = { 7, 30, 45, 65, 150, 300, 450, 2100, 13000};  // x10  //ULTIMO VALOR REFERENCIA
static const unsigned int in[]  = { 100, 350, 430, 500, 680, 780, 950, 1005, 1024 };  // 0 - 1024

#define Debug Serial        //Change to Serial1 if you want to use the GPIO2 to TX 

void setup()
{   
    Initialize();
    
    Debug.begin(115200);

    // Connect to the WiFi network and get an address from DHCP
    GetIPAddress();      

    SetAddress(0xAB03, 0xFF00, 0xAB01);
   

    // This node will serve all the others in the network providing an address
    Set_T54(LDR);
    Set_T16(LEDCONTROL);
    pinMode(4, OUTPUT);                 // Blue LED output
    pinMode(5, OUTPUT);                 // Green LED output
    pinMode(14, OUTPUT);                // Red LED output
    pinMode(PIR_PIN, INPUT);
    START_PeerJoin();
}

// Following variable are used to save souliss set LED value. Souliss use 8 bit PWM output (0-255),
// whereas ESP8266 have 10 bit PWM output (0-1023). The variable are used to make the conversion.
int ledBlue = 0;
int ledGreen = 0;
int ledRed = 0;

void loop()
    EXECUTEFAST() {                     
        UPDATEFAST(); 
        FAST_10ms() {   // We process the logic and relevant input and output every 10 milliseconds
            Logic_T16(LEDCONTROL);
            ledBlue = mOutput(LEDBLUE);
            ledGreen = mOutput(LEDGREEN);
            ledRed = mOutput(LEDRED);
            if (ledBlue != 0) { //Avoid direct write, due to the multiplication 0 value would result in 4. 
              analogWrite(4, (ledBlue+1)*4); //multiply each value by 4, ESP work with value from 0-1024
            } else {
              analogWrite(4, 0); 
            }
            if (ledGreen != 0) {
              analogWrite(5, (ledGreen+1)*4); 
            } else {
              analogWrite(5, 0); 
            }
            if (ledRed != 0) {
              analogWrite(14, (ledRed+1)*4); 
            } else {
              analogWrite(14, 0); 
            }

             if(digitalRead(PIR_PIN) == HIGH && mOutputasFloat(LDR) <= LDR_THRESOLD) {
                mInput(LEDCONTROL) == Souliss_T1n_OnCmd;
                mOutput(LEDCONTROL) == Souliss_T1n_OnCoil;
            }    
            ProcessCommunication();
        } 
          
        
        FAST_2110ms()
        {
           Logic_T54(LDR);
        } 
        
        FAST_7110ms()
        { 
              float ldr_read = get_lux(in, out, sizeofarray)/100.0;  //ORIGINAL
              if (ldr_read == 0) ldr_read = 0.01;
              Souliss_ImportAnalog(memory_map, LDR, &ldr_read);
        }
        
        // Here we handle here the communication with Android
         FAST_PeerComms();  // Se comunica por ser Peer                                          
    }

    EXECUTESLOW() { 
        UPDATESLOW();

        SLOW_10s() {                
            // Timer associated to the LED logic control
            Timer_T16(LEDCONTROL); 
      // Test phase output to ensure device is running
      Serial.println("Device is still running ");     
        }     

    }
    
     START_PeerJoin(); 
}    
    
   
//////////////////////////////////////////////////////////////////////////////
// Calculate lux based on rawADC reading from LDR returns value in lux/10
//////////////////////////////////////////////////////////////////////////////
int get_lux(const unsigned int* _in, const unsigned int* _out, byte size)
{
 // take care the value is within range
 // val = constrain(val, _in[0], _in[size-1]);

 int val = analogRead(A0);
 Debug.print("AnalogRead: ");
 Debug.println(val);
 if (val <= _in[0]) return _out[0];
 if (val >= _in[size-1]) return _out[size-1];

 // search right interval
 byte pos = 1;  // _in[0] allready tested
 while(val > _in[pos]) pos++;

 // this will handle all exact "points" in the _in array
 if (val == _in[pos]) return _out[pos];

 // interpolate in the right segment for the rest
 return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}

Di Maio, Dario

unread,
Jul 2, 2016, 7:41:57 AM7/2/16
to sou...@googlegroups.com
Hi Adrian,

does your code never enters in the below IF statement?

if(digitalRead(PIR_PIN) == HIGH && mOutputasFloat(LDR) <= LDR_THRESOLD) {
                mInput(LEDCONTROL) == Souliss_T1n_OnCmd;
                mOutput(LEDCONTROL) == Souliss_T1n_OnCoil;
 }   

Generally you don't need mOutput, but only mInput. 

You should have the USB connection as I don't see OTA. If so, add before your IF
Serial.println(digitalRead(PIR_PIN));
Seria.println(mOutputasFloat(LDR));

Likely you never enter in.

Dario.


Adrián Sanz Rodrigo

unread,
Jul 2, 2016, 7:48:03 AM7/2/16
to sou...@googlegroups.com
Ok, I'll add the OTA code in my sketch and I'll say you,

Thank you

--
You received this message because you are subscribed to a topic in the Google Groups "souliss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/souliss/BatQg6aNGvk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to souliss+u...@googlegroups.com.
To post to this group, send email to sou...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/souliss/CAKGhMP%2BUfEkHO3R7xJsXs6XetkeSsKABF59Akf7kiph98RqYYg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Di Maio, Dario

unread,
Jul 2, 2016, 7:58:13 AM7/2/16
to sou...@googlegroups.com
Why? You don't need OTA.

Are you loading the code via USB right? So, just add that line of code and you will see on the Serial Monitor the values of the two condition before they enter in the IF.

Dario.

--
You received this message because you are subscribed to the Google Groups "souliss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to souliss+u...@googlegroups.com.

To post to this group, send email to sou...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages