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. Development
  3. How to get battery volt / percentage message send to mqtt?

How to get battery volt / percentage message send to mqtt?

Scheduled Pinned Locked Moved Development
15 Posts 3 Posters 2.8k Views 3 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.
  • gohanG Offline
    gohanG Offline
    gohan
    Mod
    wrote on last edited by
    #2

    @mr_sensor said in How to get battery volt / percentage message send to mqtt?:

    int oldBatteryPcnt = 0;

    I think this needs to be a global variable, not inside loop, otherwise you set it to zero on each loop. I don't have my code with me at the moment to compare, but I think there is an error on the voltage and percentage calculation

    Y 1 Reply Last reply
    0
    • gohanG gohan

      @mr_sensor said in How to get battery volt / percentage message send to mqtt?:

      int oldBatteryPcnt = 0;

      I think this needs to be a global variable, not inside loop, otherwise you set it to zero on each loop. I don't have my code with me at the moment to compare, but I think there is an error on the voltage and percentage calculation

      Y Offline
      Y Offline
      Yveaux
      Mod
      wrote on last edited by
      #3

      @gohan said in How to get battery volt / percentage message send to mqtt?:

      I think this needs to be a global variable

      Or prefix it with the 'static' keyword to indicate its value should persist over multiple runs.

      http://yveaux.blogspot.nl

      M 1 Reply Last reply
      0
      • Y Yveaux

        @gohan said in How to get battery volt / percentage message send to mqtt?:

        I think this needs to be a global variable

        Or prefix it with the 'static' keyword to indicate its value should persist over multiple runs.

        M Offline
        M Offline
        Mr_sensor
        wrote on last edited by
        #4

        @yveaux changed the position of int oldBatteryPcnt = 0; outside of the loop and added the static prefix static int oldBatteryPcnt = 0;
        Still not giving me the right result the first message seem ok but than it only has 0 ?

        Child ID 2Battery Voltage: 0.43 V
        Battery Percent: 12 %
        2289 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:12
        2297 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
        2304 TSF:TDI:TSL
        2306 MCO:SLP:WUP=-1
        2308 TSF:TRI:TSB
        2363 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.6
        T: 19.56
        2396 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:39.7
        H: 39.69
        0
        Child ID 2Battery Voltage: 0.00 V
        Battery Percent: 0 %
        2408 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:0
        2414 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
        2420 TSF:TDI:TSL
        2422 MCO:SLP:WUP=-1
        2426 TSF:TRI:TSB
        2482 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.6
        T: 19.57
        2512 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:39.5
        H: 39.52
        0
        Child ID 2Battery Voltage: 0.00 V
        Battery Percent: 0 %
        2523 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
        2531 TSF:TDI:TSL```
        1 Reply Last reply
        0
        • gohanG Offline
          gohanG Offline
          gohan
          Mod
          wrote on last edited by
          #5

          it looks as the analogread keeps returning zero

          M 1 Reply Last reply
          0
          • gohanG gohan

            it looks as the analogread keeps returning zero

            M Offline
            M Offline
            Mr_sensor
            wrote on last edited by
            #6

            @gohan Tried to find out what causing that but can not figure it out. I'm bot a real coderer so its trial and error :) and trying to understand form other sketches / posts. Any idea where to look for?

            1 Reply Last reply
            0
            • gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #7

              try adding a delay(500) before the analogread

              M 1 Reply Last reply
              0
              • gohanG gohan

                try adding a delay(500) before the analogread

                M Offline
                M Offline
                Mr_sensor
                wrote on last edited by
                #8

                @gohan said in How to get battery volt / percentage message send to mqtt?:

                delay(500)

                Hi, tried that (already tried it with wait). Now I added it here:

                delay(500);
                    int sensorValue = analogRead(BATTERY_SENSE_PIN);
                

                Also tried it here:

                 if (oldBatteryPcnt != batteryPcnt) {
                        // Power up radio after sleep
                        delay(500);       
                        sendBatteryLevel(batteryPcnt);
                        oldBatteryPcnt = batteryPcnt;
                    } 
                                                                                                                                                                                      
                  sleep(SLEEP_TIME); //sleep a bit  ```
                
                Both did not help still ending up with 0 . 
                So what should be the best place the right place to put the delay?
                1 Reply Last reply
                0
                • gohanG Offline
                  gohanG Offline
                  gohan
                  Mod
                  wrote on last edited by
                  #9

                  the second one is useless. It is odd that it misses the analog reads. What AVR boards version are you using?

                  M 1 Reply Last reply
                  0
                  • gohanG gohan

                    the second one is useless. It is odd that it misses the analog reads. What AVR boards version are you using?

                    M Offline
                    M Offline
                    Mr_sensor
                    wrote on last edited by
                    #10

                    @gohan I'm using the EasyPCB board with an arduino pro mini 3,3v

                    1 Reply Last reply
                    0
                    • gohanG Offline
                      gohanG Offline
                      gohan
                      Mod
                      wrote on last edited by
                      #11

                      I mean in Arduino IDE library manager

                      M 1 Reply Last reply
                      0
                      • gohanG gohan

                        I mean in Arduino IDE library manager

                        M Offline
                        M Offline
                        Mr_sensor
                        wrote on last edited by
                        #12

                        @gohan

                        Arduino Pro or Pro Mini
                        Programmer AVR ISP
                        ATmega328P (3,3v 8mhz)

                        M 1 Reply Last reply
                        0
                        • M Mr_sensor

                          @gohan

                          Arduino Pro or Pro Mini
                          Programmer AVR ISP
                          ATmega328P (3,3v 8mhz)

                          M Offline
                          M Offline
                          Mr_sensor
                          wrote on last edited by
                          #13

                          Still not getting it working! Any thoughts on this?

                          1 Reply Last reply
                          0
                          • gohanG Offline
                            gohanG Offline
                            gohan
                            Mod
                            wrote on last edited by
                            #14

                            Try a simple example sketch that reads the analog pin just to make sure the hardware is OK, or try another pro mini

                            M 1 Reply Last reply
                            0
                            • gohanG gohan

                              Try a simple example sketch that reads the analog pin just to make sure the hardware is OK, or try another pro mini

                              M Offline
                              M Offline
                              Mr_sensor
                              wrote on last edited by
                              #15

                              @gohan Ok will try that see if I made a fault somewhere

                              1 Reply Last reply
                              0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              17

                              Online

                              12.0k

                              Users

                              11.2k

                              Topics

                              113.4k

                              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