Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. reporting battery to domoticZ

reporting battery to domoticZ

Scheduled Pinned Locked Moved Troubleshooting
47 Posts 5 Posters 13.3k Views 6 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • fhenrycoF Offline
    fhenrycoF Offline
    fhenryco
    wrote on last edited by fhenryco
    #7

    this morning , i get 102% , much closer to the actual value ... assuming that the ADC activity did not discharge my battery at an unprecedented level (i had this sketch working for two monthes with very little discharge of the battery before adding this battery monitoring functionality). I will know for sure if the battery level does not drop further in the coming days...i'm concerned that the delay 500 might produce non negligible battery consumption

    sundberg84S 1 Reply Last reply
    0
    • fhenrycoF fhenryco

      this morning , i get 102% , much closer to the actual value ... assuming that the ADC activity did not discharge my battery at an unprecedented level (i had this sketch working for two monthes with very little discharge of the battery before adding this battery monitoring functionality). I will know for sure if the battery level does not drop further in the coming days...i'm concerned that the delay 500 might produce non negligible battery consumption

      sundberg84S Offline
      sundberg84S Offline
      sundberg84
      Hardware Contributor
      wrote on last edited by
      #8

      @fhenryco Its hard to reach a 100% correct value on the chinese clones. I get a 5% off probably all the time. Because of that I have added an average so I collect three values every 20min into an array and send the average every hour.

      Controller: Proxmox VM - Home Assistant
      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

      1 Reply Last reply
      0
      • fhenrycoF Offline
        fhenrycoF Offline
        fhenryco
        wrote on last edited by fhenryco
        #9

        ok, thanks again, something else i v been wondering about is why domoticz will show the temp + humidity sensor as a single sensor even though there are separate instructions
        for the presentation :
        present(CHILD_ID_HUM, S_HUM);
        present(CHILD_ID_TEMP, S_TEMP);
        and for the messages themselves
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        send(msgTemp.set(temperature, 1));
        send(msgHum.set(humidity, 1));
        what should be modified if i want domoticZ to treat the two sensors separately ? (this is for Yana, a speech recognition software that has a plugin for domoticZ, but apparantly is not able to handle several values for a given sensor).

        is it the sendSketchInfo("TemperatureAndHumidity", "1.1"); that needs to be replaced by two sendSketchInfo, one for temp and one for humidity ?

        fhenrycoF 1 Reply Last reply
        0
        • fhenrycoF fhenryco

          ok, thanks again, something else i v been wondering about is why domoticz will show the temp + humidity sensor as a single sensor even though there are separate instructions
          for the presentation :
          present(CHILD_ID_HUM, S_HUM);
          present(CHILD_ID_TEMP, S_TEMP);
          and for the messages themselves
          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
          send(msgTemp.set(temperature, 1));
          send(msgHum.set(humidity, 1));
          what should be modified if i want domoticZ to treat the two sensors separately ? (this is for Yana, a speech recognition software that has a plugin for domoticZ, but apparantly is not able to handle several values for a given sensor).

          is it the sendSketchInfo("TemperatureAndHumidity", "1.1"); that needs to be replaced by two sendSketchInfo, one for temp and one for humidity ?

          fhenrycoF Offline
          fhenrycoF Offline
          fhenryco
          wrote on last edited by fhenryco
          #10

          @fhenryco apparantly something like send(msgTemp.setSensor(CHILD_ID_TEMP).set(temperature,1));
          send(msgHum.setSensor(CHILD_ID_HUM).set(humidity,1));
          should do the job ! going to try that
          but i'm surprised this is not done by default since msgHum and msgTemp are defined separately for each sensor Id

          fhenrycoF 1 Reply Last reply
          0
          • fhenrycoF fhenryco

            @fhenryco apparantly something like send(msgTemp.setSensor(CHILD_ID_TEMP).set(temperature,1));
            send(msgHum.setSensor(CHILD_ID_HUM).set(humidity,1));
            should do the job ! going to try that
            but i'm surprised this is not done by default since msgHum and msgTemp are defined separately for each sensor Id

            fhenrycoF Offline
            fhenrycoF Offline
            fhenryco
            wrote on last edited by fhenryco
            #11

            @fhenryco it did nothing different ... still have a single sensor temp+hum don't understand why ...

            sundberg84S 1 Reply Last reply
            0
            • fhenrycoF fhenryco

              @fhenryco it did nothing different ... still have a single sensor temp+hum don't understand why ...

              sundberg84S Offline
              sundberg84S Offline
              sundberg84
              Hardware Contributor
              wrote on last edited by sundberg84
              #12

              @fhenryco - Yes, and there is a trick to seperate them... and i if I remembe right @AWI is the one to ask.

              Edit, found his post here: https://forum.mysensors.org/topic/5132/ds18b20-ans-sht31-d-show-up-as-combined-sensors-on-domoticz/13

              Controller: Proxmox VM - Home Assistant
              MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
              MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
              RFLink GW - Arduino Mega + RFLink Shield, 433mhz

              1 Reply Last reply
              0
              • fhenrycoF Offline
                fhenrycoF Offline
                fhenryco
                wrote on last edited by fhenryco
                #13

                using V_text and S_info is a workaround to avoid grouping

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rene046
                  wrote on last edited by
                  #14

                  i would like to ask something to ,

                  Now i have the battery level showing up in Domoticz what can i do to log the voltage in volts, should i add something inside the sketch or something in domoticz..

                  fhenrycoF AWIA 2 Replies Last reply
                  0
                  • R Rene046

                    i would like to ask something to ,

                    Now i have the battery level showing up in Domoticz what can i do to log the voltage in volts, should i add something inside the sketch or something in domoticz..

                    fhenrycoF Offline
                    fhenrycoF Offline
                    fhenryco
                    wrote on last edited by
                    #15

                    @Rene046
                    This is the instruction in your sketch which allows to see the battery level of devices in domoticZ
                    sendBatteryLevel(batteryPcnt);
                    But as you noticed , this is not reported in the logs. One possibility to see it in the Log is to as well send this batteryPcnt information with a send(msgbatt.set(batteryPcnt ,1);
                    provided you have defined
                    MyMessage msgbatt(CHILD_ID_BATT, V_TEXT);

                    In other words you send the battery info as if it was the info of an additional sensor sending text type info
                    which you also need to present in the presentation void with

                    present(CHILD_ID_BATT, S_INFO);
                    For instance if you already have two sensors in you sketch let's say temp and hum with ID =0 and 1 respectively you would give the battery level the next unused ID
                    CHILD_ID_BATT=2
                    There might be a simpler and more natural way but i don't know it .

                    1 Reply Last reply
                    0
                    • R Rene046

                      i would like to ask something to ,

                      Now i have the battery level showing up in Domoticz what can i do to log the voltage in volts, should i add something inside the sketch or something in domoticz..

                      AWIA Offline
                      AWIA Offline
                      AWI
                      Hero Member
                      wrote on last edited by
                      #16

                      @Rene046 The "royal" way to report battery level is to define it as S_MULTIMETER and use it to report voltage. Domoticz will take the voltage and can graph it. i.e.

                      definitions: MyMessage voltageMsg(VOLTAGE_CHILD_ID, V_VOLTAGE);
                      in presentation: present(VOLTAGE_CHILD_ID, S_MULTIMETER, "Battery level" );
                      sending: send(voltageMsg.set(lastVoltage,2)); //send battery in Volt 2 decimal places

                      1 Reply Last reply
                      1
                      • R Offline
                        R Offline
                        Rene046
                        wrote on last edited by
                        #17

                        @ fhenryco

                        Cant get it to work, if you have time could help with my sketch.

                        // Enable debug prints to serial monitor
                        #define MY_DEBUG 
                        
                        // Enable and select radio type attached
                        #define MY_RADIO_NRF24
                        //#define MY_RADIO_RFM69
                        
                        
                        #include <SPI.h>
                        #include <MySensors.h>
                        #include <DHT.h>   
                        
                        
                        
                        // Enable debug prints
                        #define MY_DEBUG
                        
                        #define CHILD_ID_HUM 0
                        #define CHILD_ID_TEMP 1
                        #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                        #define CHILD_ID_BATTERY 4
                        unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                        
                        #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                        #define SKETCH_VERSION "2.1"                    // Your version
                        
                        DHT dht;
                        float lastTemp;
                        float lastHum;
                        boolean metric = true; 
                        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                        MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                        //MyMessage msgbatt(CHILD_ID_BATTERY, V_TEXT);
                        
                        //=========================
                        // 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.0041055718475073313782991202346  
                        #define VMIN 3.6                                  //  Vmin (radio Min Volt)=1.9V (564v)
                        #define VMAX 4.2                                  //  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 = getControllerConfig().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_BATTERY, S_INFO);
                          //present(CHILD_ID_BATTERY, S_CUSTOM);
                          present(CHILD_ID_HUM, S_HUM);
                          present(CHILD_ID_TEMP, S_TEMP);
                        }
                        
                        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;
                            if (!metric) {
                              temperature = dht.toFahrenheit(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);
                          }
                          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);
                               msgbatt.set(batteryPcnt ,1);
                               batLoop = 0;
                              }
                             else 
                             {
                             batLoop++;
                             }
                        }```
                        
                        My sketch is not working 100% my voltage is not precise, giving me even 221 in percentage while  lipo is at this moment 3.7 volt (simulated with a variable power supply)
                        
                        im running it on a nano on 1 lipo cell and a dc dc powerup to 5 volt measuring the lipo voltage with a  divider 1M/470K
                        .
                        Hope to add a soil moisture sensor and baro sensor in future.
                        
                        p.s. any one with some better idea sketch its ver welcome. im just started to experiment with Mysensor
                        R fhenrycoF 2 Replies Last reply
                        0
                        • R Rene046

                          @ fhenryco

                          Cant get it to work, if you have time could help with my sketch.

                          // Enable debug prints to serial monitor
                          #define MY_DEBUG 
                          
                          // Enable and select radio type attached
                          #define MY_RADIO_NRF24
                          //#define MY_RADIO_RFM69
                          
                          
                          #include <SPI.h>
                          #include <MySensors.h>
                          #include <DHT.h>   
                          
                          
                          
                          // Enable debug prints
                          #define MY_DEBUG
                          
                          #define CHILD_ID_HUM 0
                          #define CHILD_ID_TEMP 1
                          #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                          #define CHILD_ID_BATTERY 4
                          unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                          
                          #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                          #define SKETCH_VERSION "2.1"                    // Your version
                          
                          DHT dht;
                          float lastTemp;
                          float lastHum;
                          boolean metric = true; 
                          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                          MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                          //MyMessage msgbatt(CHILD_ID_BATTERY, V_TEXT);
                          
                          //=========================
                          // 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.0041055718475073313782991202346  
                          #define VMIN 3.6                                  //  Vmin (radio Min Volt)=1.9V (564v)
                          #define VMAX 4.2                                  //  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 = getControllerConfig().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_BATTERY, S_INFO);
                            //present(CHILD_ID_BATTERY, S_CUSTOM);
                            present(CHILD_ID_HUM, S_HUM);
                            present(CHILD_ID_TEMP, S_TEMP);
                          }
                          
                          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;
                              if (!metric) {
                                temperature = dht.toFahrenheit(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);
                            }
                            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);
                                 msgbatt.set(batteryPcnt ,1);
                                 batLoop = 0;
                                }
                               else 
                               {
                               batLoop++;
                               }
                          }```
                          
                          My sketch is not working 100% my voltage is not precise, giving me even 221 in percentage while  lipo is at this moment 3.7 volt (simulated with a variable power supply)
                          
                          im running it on a nano on 1 lipo cell and a dc dc powerup to 5 volt measuring the lipo voltage with a  divider 1M/470K
                          .
                          Hope to add a soil moisture sensor and baro sensor in future.
                          
                          p.s. any one with some better idea sketch its ver welcome. im just started to experiment with Mysensor
                          R Offline
                          R Offline
                          Rene046
                          wrote on last edited by
                          #18

                          O i forget to tell im running the latest gateway from Mysensor.

                          1 Reply Last reply
                          0
                          • R Rene046

                            @ fhenryco

                            Cant get it to work, if you have time could help with my sketch.

                            // Enable debug prints to serial monitor
                            #define MY_DEBUG 
                            
                            // Enable and select radio type attached
                            #define MY_RADIO_NRF24
                            //#define MY_RADIO_RFM69
                            
                            
                            #include <SPI.h>
                            #include <MySensors.h>
                            #include <DHT.h>   
                            
                            
                            
                            // Enable debug prints
                            #define MY_DEBUG
                            
                            #define CHILD_ID_HUM 0
                            #define CHILD_ID_TEMP 1
                            #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                            #define CHILD_ID_BATTERY 4
                            unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                            
                            #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                            #define SKETCH_VERSION "2.1"                    // Your version
                            
                            DHT dht;
                            float lastTemp;
                            float lastHum;
                            boolean metric = true; 
                            MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                            MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                            MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                            //MyMessage msgbatt(CHILD_ID_BATTERY, V_TEXT);
                            
                            //=========================
                            // 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.0041055718475073313782991202346  
                            #define VMIN 3.6                                  //  Vmin (radio Min Volt)=1.9V (564v)
                            #define VMAX 4.2                                  //  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 = getControllerConfig().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_BATTERY, S_INFO);
                              //present(CHILD_ID_BATTERY, S_CUSTOM);
                              present(CHILD_ID_HUM, S_HUM);
                              present(CHILD_ID_TEMP, S_TEMP);
                            }
                            
                            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;
                                if (!metric) {
                                  temperature = dht.toFahrenheit(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);
                              }
                              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);
                                   msgbatt.set(batteryPcnt ,1);
                                   batLoop = 0;
                                  }
                                 else 
                                 {
                                 batLoop++;
                                 }
                            }```
                            
                            My sketch is not working 100% my voltage is not precise, giving me even 221 in percentage while  lipo is at this moment 3.7 volt (simulated with a variable power supply)
                            
                            im running it on a nano on 1 lipo cell and a dc dc powerup to 5 volt measuring the lipo voltage with a  divider 1M/470K
                            .
                            Hope to add a soil moisture sensor and baro sensor in future.
                            
                            p.s. any one with some better idea sketch its ver welcome. im just started to experiment with Mysensor
                            fhenrycoF Offline
                            fhenrycoF Offline
                            fhenryco
                            wrote on last edited by
                            #19

                            @Rene046
                            not
                            msgbatt.set(batteryPcnt ,1);
                            but
                            send(msgbatt.set(batteryPcnt ,1));

                            also may be better to use S_MULTIMETER and V_VOLTAGE (rather than S_INFO and V_TEXT) as adviced by AWI and also 2 decimals hence
                            send(msgbatt.set(batteryPcnt ,2));

                            1 Reply Last reply
                            1
                            • R Offline
                              R Offline
                              Rene046
                              wrote on last edited by
                              #20

                              thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
                              .

                              // Enable debug prints to serial monitor
                              #define MY_DEBUG 
                              
                              // Enable and select radio type attached
                              #define MY_RADIO_NRF24
                              //#define MY_RADIO_RFM69
                              
                              #include <SPI.h>
                              #include <MySensors.h>
                              #include <DHT.h>   
                              
                              // Enable debug prints
                              #define MY_DEBUG
                              
                              #define CHILD_ID_HUM 0
                              #define CHILD_ID_TEMP 1
                              #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                              #define CHILD_ID_BATTERY 4
                              unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                              
                              #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                              #define SKETCH_VERSION "2.1"                    // Your version
                              
                              DHT dht;
                              float lastTemp;
                              float lastHum;
                              boolean metric = true; 
                              MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                              MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                              MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                              
                              //=========================
                              // 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.0041055718475073313782991202346  // 4.20/1023 volt
                              #define VMIN 3.6                                  //  Vmin = 3.6 lipo empty
                              #define VMAX 4.2                                  //  Vmax = 4.2 volt lipo full
                              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 = getControllerConfig().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_BATTERY, S_MULTIMETER);
                                present(CHILD_ID_HUM, S_HUM);
                                present(CHILD_ID_TEMP, S_TEMP);
                              }
                              
                              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;
                                  if (!metric) {
                                    temperature = dht.toFahrenheit(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);
                                }
                                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);
                                     send(msgbatt.set(batteryPcnt ,2));
                                     batLoop = 0;
                                    }
                                   else 
                                   {
                                   batLoop++;
                                   }
                              }```
                              R AWIA Arnold ŠlepetisA 3 Replies Last reply
                              0
                              • R Rene046

                                thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
                                .

                                // Enable debug prints to serial monitor
                                #define MY_DEBUG 
                                
                                // Enable and select radio type attached
                                #define MY_RADIO_NRF24
                                //#define MY_RADIO_RFM69
                                
                                #include <SPI.h>
                                #include <MySensors.h>
                                #include <DHT.h>   
                                
                                // Enable debug prints
                                #define MY_DEBUG
                                
                                #define CHILD_ID_HUM 0
                                #define CHILD_ID_TEMP 1
                                #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                                #define CHILD_ID_BATTERY 4
                                unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                                
                                #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                                #define SKETCH_VERSION "2.1"                    // Your version
                                
                                DHT dht;
                                float lastTemp;
                                float lastHum;
                                boolean metric = true; 
                                MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                                
                                //=========================
                                // 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.0041055718475073313782991202346  // 4.20/1023 volt
                                #define VMIN 3.6                                  //  Vmin = 3.6 lipo empty
                                #define VMAX 4.2                                  //  Vmax = 4.2 volt lipo full
                                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 = getControllerConfig().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_BATTERY, S_MULTIMETER);
                                  present(CHILD_ID_HUM, S_HUM);
                                  present(CHILD_ID_TEMP, S_TEMP);
                                }
                                
                                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;
                                    if (!metric) {
                                      temperature = dht.toFahrenheit(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);
                                  }
                                  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);
                                       send(msgbatt.set(batteryPcnt ,2));
                                       batLoop = 0;
                                      }
                                     else 
                                     {
                                     batLoop++;
                                     }
                                }```
                                R Offline
                                R Offline
                                Rene046
                                wrote on last edited by
                                #21

                                And my serial monitor is going wild .lol
                                .

                                41815 MCO:SLP:WUP=-1
                                Battery percent: 95 %
                                Battery Average (Send): -12 %
                                45359 !TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=NACK:244
                                45403 !TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=1,st=NACK:-12.00
                                45410 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
                                45415 MCO:SLP:TPD
                                45417 MCO:SLP:WUP=-1
                                47962 !TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=2,st=NACK:20.0
                                T: 20.00
                                48007 !TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=3,st=NACK:49.6
                                H: 49.60
                                Battery percent: -49 %
                                49014 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
                                49020 MCO:SLP:TPD
                                49022 MCO:SLP:WUP=-1
                                51567 !TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=4,st=NACK:49.2
                                H: 49.20
                                Battery percent: -26 %
                                52575 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
                                52582 MCO:SLP:TPD
                                
                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  Rene046
                                  wrote on last edited by
                                  #22

                                  And it seems like instead of real voltage i seen percentage in the log

                                  1 Reply Last reply
                                  0
                                  • R Rene046

                                    thx i got a voltage log only giving me now -44.000 volt and 212 prct at battery level while measuring 3.7 volt battery and voltage on A0 1.3 volts
                                    .

                                    // Enable debug prints to serial monitor
                                    #define MY_DEBUG 
                                    
                                    // Enable and select radio type attached
                                    #define MY_RADIO_NRF24
                                    //#define MY_RADIO_RFM69
                                    
                                    #include <SPI.h>
                                    #include <MySensors.h>
                                    #include <DHT.h>   
                                    
                                    // Enable debug prints
                                    #define MY_DEBUG
                                    
                                    #define CHILD_ID_HUM 0
                                    #define CHILD_ID_TEMP 1
                                    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
                                    #define CHILD_ID_BATTERY 4
                                    unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
                                    
                                    #define SKETCH_NAME "Temp_Hum_Batt"        // Change to a fancy name you like
                                    #define SKETCH_VERSION "2.1"                    // Your version
                                    
                                    DHT dht;
                                    float lastTemp;
                                    float lastHum;
                                    boolean metric = true; 
                                    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                    MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                                    
                                    //=========================
                                    // 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.0041055718475073313782991202346  // 4.20/1023 volt
                                    #define VMIN 3.6                                  //  Vmin = 3.6 lipo empty
                                    #define VMAX 4.2                                  //  Vmax = 4.2 volt lipo full
                                    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 = getControllerConfig().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_BATTERY, S_MULTIMETER);
                                      present(CHILD_ID_HUM, S_HUM);
                                      present(CHILD_ID_TEMP, S_TEMP);
                                    }
                                    
                                    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;
                                        if (!metric) {
                                          temperature = dht.toFahrenheit(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);
                                      }
                                      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);
                                           send(msgbatt.set(batteryPcnt ,2));
                                           batLoop = 0;
                                          }
                                         else 
                                         {
                                         batLoop++;
                                         }
                                    }```
                                    AWIA Offline
                                    AWIA Offline
                                    AWI
                                    Hero Member
                                    wrote on last edited by
                                    #23

                                    @Rene046 if you send V_VOLTAGE you should use the voltage level not the percentage.

                                    R 1 Reply Last reply
                                    0
                                    • AWIA AWI

                                      @Rene046 if you send V_VOLTAGE you should use the voltage level not the percentage.

                                      R Offline
                                      R Offline
                                      Rene046
                                      wrote on last edited by
                                      #24

                                      @AWI

                                      hi could you explain how..

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Rene046
                                        wrote on last edited by
                                        #25

                                        I still cant get it to work, No voltage reading or logging.
                                        I there nobody trying to build a equal sensor.

                                        maybe someone who could rework my sketch. or having a working sketch where i could delete some sensors from.

                                        kind regards and greetings from the Netherlands.

                                        AWIA 1 Reply Last reply
                                        0
                                        • R Rene046

                                          I still cant get it to work, No voltage reading or logging.
                                          I there nobody trying to build a equal sensor.

                                          maybe someone who could rework my sketch. or having a working sketch where i could delete some sensors from.

                                          kind regards and greetings from the Netherlands.

                                          AWIA Offline
                                          AWIA Offline
                                          AWI
                                          Hero Member
                                          wrote on last edited by
                                          #26

                                          @Rene046 I would be happy to help you but need some information on your circuit:

                                          • How are you powering your Arduino? Through an (onboard) regulator or directly from battery?
                                          • The internal reference can only be used up to 1.1V, you seem to be having a voltage divider which has a resulting voltage of > 1.1 V ?
                                          • I do not understand your averaging algorithm, why are you not taking an average of the latest x readings?

                                          My suggestion would be to use a different external reference (output of the dc-dc boost converter/ Vcc?) to measure the voltage.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          13

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular