Sleep mode/Read Vcc issue



  • Hello,

    I'm made temp/hum sensor with slim node and DHT22.
    When Mysensors/HumiditySensor sketch is loaded, the sensor in sleep mode consumes 25uA.

    I also would like to report battery level.
    But when read vcc function is included in the sketch, the power consumtions after transmission remains ~ 0.1 - 0.2mA.

    Any ideas why sleep mode is not functioning correctly with sendBatteryReport()?
    Thank you in advance.

    Please see my updated sketch:

    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>
    #include <Vcc.h>
    
    #define VCC_MIN 2.8
    #define VCC_MAX 3.3
    Vcc vcc;
    #define NODE_ID 2
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    unsigned long SLEEP_TIME = 120000; // 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);
    
    
    void setup()  
    { 
      gw.begin(NULL, NODE_ID);
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Humidity", "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);
      
      metric = gw.getConfig().isMetric;
    }
    
    void loop()      
    {  
      sendBatteryReport();
      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);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    void sendBatteryReport() {
              float p = vcc.Read_Perc(VCC_MIN, VCC_MAX, true);
              int batteryPcnt = static_cast<int>(p);
              gw.sendBatteryLevel(batteryPcnt);
    }
    
    

  • Hero Member

    To give you at least a response. I have no clue... 😉 I use a lot of low power nodes and the vcc library. My only guess is that it could have something to do with the supply voltage begin insufficient to get readings from the DHT sensor.



  • I'm not sure if it's related to DHT22, because sleep mode works fine when Vcc function is commented in the sketch.


  • Mod

    Maybe you could try to move sendBatteryReport(); from loop to setup and see if it makes a difference if vcc is only called once.



  • I dont use vcc.h i use this code at the bottom of my sketch

    long readVcc() {
    Serial.println("readVcc");
    // Read 1.1V reference against AVcc
    ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    delay(2); // Wait for Vref to settle
    ADCSRA |= _BV(ADSC); // Convert
    while (bit_is_set(ADCSRA,ADSC));
    result = ADCL;
    result |= ADCH<<8;
    result = 1126400L / result; // Back-calculate AVcc in mV
    //return result;
    //gw.begin(incomingMessage,node_id, false)
    batteryPcnt = (result - 3300) * 0.111111;
    batteryVolt = result/1000.000;
    gw.sendBatteryLevel(batteryPcnt);
    gw.send(battMsg.set(batteryVolt, 3));
    /*Serial.print("battery volt:");
    Serial.println(batteryVolt, 3);
    Serial.print("battery percent:");
    Serial.println(batteryPcnt);
    */
    }
    

    how does you vcc.h look like


  • Hardware Contributor

    @flopp
    You shouldn't need your own readVcc() code like that since you're including the Vcc library in the beginning of the sketch. Make sure you have the vcc-lib in your Arduino folder:
    https://github.com/Yveaux/Arduino_Vcc


  • Mod

    If you use the readVcc function in flopp's sketch, remove gw.begin(incomingMessage, NODE_ID, false);
    Calling gw.begin several times seems strange.



  • @mfalkvidd
    Sorry that line I don't use anymore. Code must be copied from an old sketch where I was using an LDO that I disabled and when power was back I have to run gw.begin again otherwise I could send anything.
    I have now comment that row out.
    Thanks for noticing



  • @m26872
    I don't use vcc.h I like to have it in the bottom of the sketch.
    Is it better to have it in a seperate file?
    Except that it looks cleaner?


  • Mod

    @engy: just a note: the sketch you posted has #include <Vcc.h>

    You should either include Vcc.h OR include readVcc() in your sketch. Not both.



  • This post is deleted!


  • The problem was faulty NRF24... another batch 😞
    Just powered sensor in sleep mode is consuming as expected - a few uA.
    But later, after while, power consumption is increasing.
    Commenting Vcc was just coincidence.

    Received new NRFs batch from mysensors shop, and all the problems gone.
    Thanks!


 

270
Online

7.8k
Users

8.7k
Topics

92.9k
Posts