LDR as light sensor



  • Hello

    Since I'm a student and the holidays have begun, I finally have time to convert my purchases online into a domotic system.
    I succesfully set up my raspi 2 with domoticz and started working on my very first sensor.
    Now in the "build section" at light, there are true light sensors used, but I only needed an estimated value on which I can base actions.
    So my idea was to use an LDR and some calculations to convert the analog input to a luxish value.
    Now... I've edited the "light" sketch from the build section to send the value I calculated, but when I look in domoticz, at "Hardware setup", it seems like this child is sending no values. (screenshot below).
    And when I go to "setup devices", this sensor doesn't even show up. (screenshot below)

    Can anyone check my code and say what I've done wrong. I'll also add a picture of my breadboard in a moment. Don't know whether that'll be clear though.

    Screenshots:
    Setup Hardware
    Setup devices

    Breadboard:
    Setup

    My code:

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    
    #define LIGHT_SENSOR_ANALOG_PIN 0
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    unsigned long SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
    MySensor gw;
    
    DHT dht;
    float lastTemp;
    float lastHum;
    int lastLightLevel;
    boolean metric = true; 
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgLux(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    
    
    void setup()  
    { 
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Room Flor 1", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
    
      int lightLevel = (2500/((analogRead(LIGHT_SENSOR_ANALOG_PIN)*0.0048828125)-500))/10; 
      Serial.println(lightLevel);
      if (lightLevel != lastLightLevel) {
          gw.send(msgLux.set(lightLevel));
          lastLightLevel = lightLevel;
      }
    
    
      
      gw.sleep(SLEEP_TIME); //sleep a bit
    }```

  • Hardware Contributor

    0_1468322988720_MyLightSensor.ino

    Here is my version of a LDR based light sensor code.



  • @GertSanders Thank you!

    So, you are basically sending a light intensity value between 0 and 100 to domoticz.
    I'm going to leave the battery part out because my sensor is installed near a power outlet, and I can just use power from an adapter.
    I'm testing it now, and reporting back with my results!



  • @GertSanders
    Okay
    So after editing your code to work on the slightly older mysensors library I still have installed, all seems to work!
    Thanks a lot for your help!

    Screenshots:

    New code:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a humidity/temperature
     * sensor using DHT11/DHT-22 
     * http://www.mysensors.org/build/humidity
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    
    #define LIGHTLEVEL 1
    #define ledpin 13
    #define LIGHT_SENSOR_ANALOG_PIN A0
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    #define MESSAGEWAIT 500
    #define SENDRETRIES 5
    
    int LightLevel = 0;
    int PreviousLightLevel = 0;
    boolean BatOK = false;
    boolean LightOK = false;
    
    unsigned long SLEEP_TIME = 10000; // Sleep time between reads (in milliseconds)
    MySensor gw;
    
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage LightMsg(LIGHTLEVEL, V_LIGHT_LEVEL);
    
    
    
    void setup()  
    { 
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Room Flor 1", "1.0");
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      gw.present(LIGHTLEVEL, S_LIGHT_LEVEL);
    
      pinMode(ledpin, OUTPUT);
      pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT_PULLUP);
    
      LightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
      LightOK = false;
      PreviousLightLevel = 0;
      
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
    
    
      if (!LightOK)
      {
        if (LightLevel != PreviousLightLevel)
        {
          LightOK = gw.send(LightMsg.set(LightLevel));
          if (LightOK)
          {
            PreviousLightLevel = LightLevel;
          }
        }
        else
        {
          LightOK = true;
        }
      }
    
      
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    
    void blipled()
    {
      gw.sleep(100);              // wait for a second
      digitalWrite(ledpin, HIGH);   // turn the LED on (HIGH is the voltage level)
      gw.sleep(50);              // wait for a second
      digitalWrite(ledpin, LOW);    // turn the LED off by making the voltage HIGH
    }
    

  • Hardware Contributor

    @Flor-Sanders
    You should read the analog value of pin A0 in the loop as well.
    And why not use the version 2.0.0 library, it makes the sketches more readable and compact.



  • @GertSanders
    Oh right! Guessed I missed that while copying and pasting everything.
    I just remarked there was a v2.0 while writing the sketch... Might rewrite it while I'm busy 🙂



  • @GertSanders
    So I just succesfully updated the code to library 2.0.0
    Yet, now nothing shows up in domoticz. Do I also need to update something to the raspberry pi 2 (which is acting as the gateway).
    Since the serial gateway is stating to be succesfully sending information.

    New code:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * 
     * DESCRIPTION
     * Example sketch showing how to measue light level using a LM393 photo-resistor 
     * http://www.mysensors.org/build/light
     */
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    //Including nescessary libraries
    #include <SPI.h>
    #include <MySensors.h> 
    #include <DHT.h>  
    
    
    //Defining child ID's and sensor pins
    #define LIGHTLEVEL 0
    #define HUMIDITY 1
    #define TEMPERATURE 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define LIGHT_SENSOR_ANALOG_PIN A0
    DHT dht;
    
    //Defining values to store sensor information
    int LightLevel = 0;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    int lastLightLevel;
    
    //Defining sleep and wait times
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)(30 seconds)
    #define MESSAGEWAIT 500
    
    MyMessage LightMsg(LIGHTLEVEL, V_LIGHT_LEVEL);
    MyMessage msgHum(HUMIDITY, V_HUM);
    MyMessage msgTemp(TEMPERATURE, V_TEMP);
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Room Flor 1", "2.0");
      wait(MESSAGEWAIT);
    
      // Register all sensors to gateway (they will be created as child devices)
      present(LIGHTLEVEL, S_LIGHT_LEVEL);
      wait(MESSAGEWAIT);
      present(HUMIDITY, S_HUM);
      wait(MESSAGEWAIT);
      present(TEMPERATURE, S_TEMP);
    }
    
    void setup() {
       dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
    }
    
    void loop() {
      delay(dht.getMinimumSamplingPeriod());
      
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
    
      
      int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
      Serial.println(lightLevel);
      if (lightLevel != lastLightLevel) {
          send(LightMsg.set(lightLevel));
          lastLightLevel = lightLevel;
      }
      sleep(SLEEP_TIME);
    }
    
    
    

  • Hardware Contributor

    @Flor-Sanders
    No idea, there seems to be an issue with 2.0.0 and Raspi Gateway. I'm using the dev version of 2.0.0 without updating my raspi GW. Will check in the coming days on my SMS node (moet ik klaar maken voor de meetup in Breda).



  • @GertSanders
    Then I'll temporaraly switch over to a serial gateway.
    Thanks again for the info!



Suggested Topics

21
Online

11.2k
Users

11.1k
Topics

112.5k
Posts