Battery Sensor / Voltage Monitor



  • Guys,

    This sketch compiled but doesn't work. I wondering if some one could have a look at it and see what I have done wrong. Cheers!

    Serial Logs

    sensor started, id 1
    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
    send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=13,st=ok:Battery Meter
    send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 1-1-0-0 s=20,c=0,t=38,pt=0,l=5,st=ok:1.4.1
    1023
    Battery Voltage: 3.44 V
    Battery percent: 102 %
    send: 1-1-0-0 s=255,c=3,t=0,pt=1,l=1,st=ok:102

    // This is an example that demonstrates how to report the battery level for a sensor
    // Instructions for measuring battery capacity on A0 are available in the follwoing forum
    // thread: http://forum.micasaverde.com/index.php/topic,20078.0.html
    
    #include <SPI.h>
    #include <MySensor.h>
    
    #define CHILD_ID_BATT 20
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    
    MySensor gw;
    
    MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
    unsigned long SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
    int oldBatteryPcnt = 0;
    
    void setup()  
    {
       // use the 1.1 V internal reference
       analogReference(INTERNAL);
       gw.begin();
    
       // Send the sketch version information to the gateway and Controller
       gw.sendSketchInfo("Battery Meter", "1.0");
       
      gw.present(CHILD_ID_BATT, V_VOLTAGE);
    }
    
    void loop()
    {
       // get the battery Voltage
       int sensorValue = analogRead(BATTERY_SENSE_PIN);
       Serial.println(sensorValue);
       
       // 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
       float batteryV  = sensorValue * 0.003363075;
       int batteryPcnt = sensorValue / 10;
    
       Serial.print("Battery Voltage: ");
       Serial.print(batteryV);
       Serial.println(" V");
    
       Serial.print("Battery percent: ");
       Serial.print(batteryPcnt);
       Serial.println(" %");
    
       if (oldBatteryPcnt != batteryPcnt) {
         // Power up radio after sleep
         gw.sendBatteryLevel(batteryPcnt);
         oldBatteryPcnt = batteryPcnt;
       }
       gw.sleep(SLEEP_TIME);
    }
    

  • Hero Member

    @jeylites From what I see the sketch is working fine except that it is reporting a 102% battery level 🙂 So it could be that your controller is not interpreting the message or that you are expecting the Voltage to show up as a sensor. To do the last you have to send it with your the 'msgBattery' message. Which controller are you using?



  • Everything looks fine.

    this is send to the gw send: 1-1-0-0 s=255,c=3,t=0,pt=1,l=1,st=ok:102

    Maybe the controller doesn't accept higher than 100.

    Your voltage is higher than 3.363V so thats why its 102%.

    add this just after int batteryPcnt - sensorValue / 10;

    if(batteryPcnt > 100)
    {
       batteryPcnt = 100;
    }
    

    or

    batteryPcnt = min(batteryPcnt, 100);
    


  • @ AWI

    good to hear from you 🙂 Yes, I will like it to show up as a sensor, I'm using Vera Edge.

    @Sweebee

    I'm going to make the change and see if it works. Will report back guys!



  • It actually comes up as Arduino Door... very odd. Still can't get voltage or battery level to show up.



  • After restarting the node, I do see the battery % on Arduino Node . Question is, how do you make it to show up as a sensor. It will be great to have it show voltage too.


  • Hero Member

    @jeylites You are presenting the sensor as V_VOLTAGE (value=38). The sensor should present itself with a S_.... message. eg S_POWER (value=13) to be recognized correctly. Have a look at the API/ Serial spec


  • Admin

    S_VOLTAGE has not been implemented on vera (someone needs to create the device-files).

    But I don't think you need to present anything at all. The Battery level is added to the Node-device which is automatically created.



  • @hek That explains why it's not working

    Here is a screen shot of my setup.Screen Shot 2015-05-12 at 12.31.41 AM.png


  • Admin

    Sidedote: Hmm.. the gui looks awful on UI7. Why doesn't it break the text into lines?



  • @hek
    I couldn't agree more. I saw somewhere in My Sensor someone posted a way to make that go away but I can't seem to find it.


  • Admin

    You are running the UI7 branch right?
    https://github.com/mysensors/Vera/tree/UI7



  • @hek Yes sir!

    I added all these files below into Luup Screen Shot 2015-05-12 at 12.46.27 AM.png



  • @hek
    I did not know what this does so I did not add....Screen Shot 2015-05-12 at 12.50.03 AM.png


  • Admin

    @jeylites said:

    I did not know what this does so I did not add....

    No they should not be added.

    If someone has the time to adjust the UI7 GUI a bit I would appreciate it. There are a lot of whitespace above the icon. I hope that space is available to use by the device. If not, is sucks.



  • @hek out of curiosity, I was checking out the sketch for Sensebender and saw that it had a Child Id for Battery Sensor and Voltage sensor. How does this work as a Child id?

    Since Voltage is not available on Vera at this time , I'm assuming I will not be able to use this feature... correct?


  • Admin

    I think it is supported in Domoticz. But we better ping @tbowmo for a better explanation. It will simply be ignored in Vera.


  • Admin

    @jeylites @hek

    That battery ChildId is a leftover, from some earlier monitoring that I did, to monitor batteryvoltage decrease. Back then I used pidome as controler.

    For a long time it has only reported battery percentage with the following routine

    /********************************************
     *
     * Sends battery information (battery percentage)
     *
     * Parameters
     * - force : Forces transmission of a value
     *
     *******************************************/
    void sendBattLevel(bool force)
    {
      if (force) lastBattery = -1;
      long vcc = readVcc();
      if (vcc != lastBattery) {
        lastBattery = vcc;
        // Calculate percentage
    
        vcc = vcc - 1900; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at
        
        long percent = vcc / 14.0;
        gw.sendBatteryLevel(percent);
      }
    }
    

    There are a couple of variable references back in the code at github, for the old battery voltage childId. These where removed last night, while I did some cleanup of the code. The changes are not pushed to github yet, as I haven't tested it yet.



  • How can I present the value in Voltage (for example 3.06 V)? I don't like the "Battery monitor".

    I have got the multiple temperature meter (DHT + Dallas) working very well. Would be nice to have battery level in volts also.

    Cheers!



  • @stamag That depends on the kind of controller employed.

    There is no plugin for Vera according to @hek .

    S_VOLTAGE has not been implemented on vera (someone needs to create the device-files).

    But I don't think you need to present anything at all. The Battery level is added to the Node-device which is automatically created.


  • Hero Member

    @stamag I don't know exactly which sensors are supported in vera but you could possibly 'trick' the voltage on a custom V_VAR type.



  • This post is deleted!


  • If I'm measuring a 5.2 volt battery, what value should R1 and R2 be?

    If the measuring voltages goes beyond the set voltage will that destroy the Arduino's input (A0)?



  • @jeylites You may change R1 and R2 and you don't destroy (A0).

    An other way is to change the multiplicator (0.003363075) in the formula:
    float batteryV = sensorValue * 0.003363075;


Log in to reply
 

Suggested Topics

  • 4
  • 5
  • 14
  • 2
  • 274
  • 2

14
Online

11.2k
Users

11.1k
Topics

112.5k
Posts