first battery powered DHT lux



  • hi guys so im trying to build my first sensor from scratch, its using the easyPCB @sundberg84 (CHEERS)!

    im having issues combining it all for the DHT

    i keep getting dht was not declared

    so its a battery sensor with a DHT 22 and a photo resistor for lux

    here is my code

    anyone with advice would be greatly appreciated

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 3
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define LIGHT_SENSOR_ANALOG_PIN A1
    
    unsigned long SLEEP_TIME = 600000; // Sleep time between reads (in milliseconds)
    
    #define SKETCH_NAME "loungeroom living sensor #1"        // Change to a fancy name you like
    #define SKETCH_VERSION "1"                    // Your version
    
    float lastTemp;
    float lastHum;
    boolean metric = true;
    int LightLevel = 0;
    int lastLightLevel;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage LightMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    void setup()  
    { 
     analogReference(INTERNAL);             // For battery sensing
    
      delay(500); // Allow time for radio if power used as reset
      
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      metric = getConfig().isMetric;
    }
    
    void presentation()  
    { 
      // Send the Sketch Version Information to the Gateway
     // Send the Sketch Version Information to the Gateway
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_LIGHTLEVEL, S_LIGHT_LEVEL);
    }
    
    void loop()      
    {  
      delay(500); // Allow time for radio if power used as reset
      delay(dht.getMinimumSamplingPeriod());
     
      // Fetch temperatures from DHT sensor
      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(temprature);
      
      
      // Fetch humidity from DHT sensor
      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;
    
    }
    
      batM();
      sleep(SLEEP_TIME); //sleep a bit
    
    }
    
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    

  • Hardware Contributor

    @markjgabb you need to install the external dht22 library and you forgot to declare dht. (DHT dht;)

    See my sketch https://github.com/sundberg84/MySensors2.0.0/blob/master/NorrHumAs/NorrHumAs.ino



  • @sundberg84

    i copied your github one directly....

    but when i try to verify i get the following error
    exit status 1
    no matching function for call to 'DHT::DHT()'


  • Hardware Contributor

    @markjgabb did you install the library?



  • i did install DHT library...but mine seems to be diffrent, if i manually include it i get dht_U.h



  • dht sensor library by adafruit installed version 1.3.0



  • @sundberg84 copied a dht library from another computer i used last time i did mysensors work...now ive got that base sketch working....now to add the photo resistor



  • @sundberg84 champion and here is completed sketch which compiles without errors

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 14
    #define MY_PARENT_NODE_ID 100
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT.h>  
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_LIGHT 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    #define LIGHT_SENSOR_ANALOG_PIN A1
    unsigned long SLEEP_TIME = 120000; // Sleep time between reads (in milliseconds)
    
    #define SKETCH_NAME "loungeroom living sensor #1"        // Change to a fancy name you like
    #define SKETCH_VERSION "1"                    // Your version
    
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    int LightLevel = 0;
    int lastLightLevel;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage LightMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    void setup()  
    { 
     analogReference(INTERNAL);             // For battery sensing
    
      delay(500); // Allow time for radio if power used as reset
      
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      metric = getConfig().isMetric;
    }
    
    void presentation()  
    { 
      // Send the Sketch Version Information to the Gateway
     // Send the Sketch Version Information to the Gateway
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()      
    {  
      delay(500); // Allow time for radio if power used as reset
      delay(dht.getMinimumSamplingPeriod());
     
      // Fetch temperatures from DHT sensor
      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);
      }
      
      // Fetch humidity from DHT sensor
      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;
      }
          
      batM();
      sleep(SLEEP_TIME); //sleep a bit
    }
    
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    

  • Hardware Contributor

    @markjgabb - nice to hear!
    I always use the librarys in the ext. MySensors folder: https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries since they are known to work with the sketches in the build section.

    Also note that you have fixed the ID and parent node now... you might want to remove those lines
    #define MY_NODE_ID 14
    #define MY_PARENT_NODE_ID 100

    atleast the parent_node_id because that is a repeater in my setup... so unless you have a repeater woth 100 as id this will not work.
    If you have flashed this to the node, it might be a good idea to clear the eeprom before flashing the new sketch without the parent_node_id feature.



  • @sundberg84 nah just prrof of build at the moment...ill fix them with my proper node ID's when it's build time....waiting on step up's at the moment..... they seem to take a while when you find them cheaper


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 5
  • 6
  • 2
  • 1

11
Online

11.4k
Users

11.1k
Topics

112.7k
Posts