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. MQTT and batterylevel

MQTT and batterylevel

Scheduled Pinned Locked Moved Development
17 Posts 6 Posters 6.7k Views 2 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.
  • gaduG Offline
    gaduG Offline
    gadu
    wrote on last edited by
    #1

    I can't get the ProMini to publish the batterylevel.
    In the serial interface I can see that it is reporting the levels correct, but nothing reaches Mosquitto nor OpenHAB.

    My sketch looks likes this, what am I doing wrong?!
    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>

     #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
     #define MAX_ATTACHED_DS18B20 16
     unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
     OneWire oneWire(ONE_WIRE_BUS);
     DallasTemperature sensors(&oneWire);
     
     int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
     
     MySensor gw;
    
     int oldBatteryPcnt = 0;
     
     float lastTemperature[MAX_ATTACHED_DS18B20];
     int numSensors=0;
     boolean receivedConfig = false;
     boolean metric = true; 
     // Initialize temperature message
     MyMessage msg(0,V_TEMP);
     
     void setup()  
     { 
       // Startup OneWire 
       sensors.begin();
       
       // use the 1.1 V internal reference
       analogReference(INTERNAL);
     
       // Startup and initialize MySensors library. Set callback for incoming messages. 
       gw.begin(); 
    
       // Send the sketch version information to the gateway and Controller
       //gw.sendSketchInfo("Temperature Sensor", "1.0");
    
       // Fetch the number of attached temperature sensors  
       numSensors = sensors.getDeviceCount();
     
       // Present all sensors to controller
       for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
     gw.present(i, S_TEMP);
       }
     }
     
     void loop()     
     {     
       // Process incoming messages (like config from server)
       gw.process(); 
    
       // Fetch temperatures from Dallas sensors
       sensors.requestTemperatures(); 
     
       // Read temperatures and send them to controller 
       for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
      
         // Fetch and round temperature to one decimal
         float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
      
    // Only send data if temperature has changed and no error
    if (lastTemperature[i] != temperature && temperature != -127.00) {
    
      // Send in the new temperature
      gw.send(msg.setSensor(i).set(temperature,1));
      lastTemperature[i]=temperature;
    }
       }
    
        // 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);
     }
    
    1 Reply Last reply
    0
    • gaduG Offline
      gaduG Offline
      gadu
      wrote on last edited by
      #2

      Sorry for bumping, but i'm lost... :(
      Help?

      1 Reply Last reply
      0
      • MagiskeM Offline
        MagiskeM Offline
        Magiske
        wrote on last edited by Magiske
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbj
          wrote on last edited by
          #4

          Maybe I did not read your code good enough but I could not see any definition of the message type sendBatteryLevel (see an example for the definition of the temperature message in your code "MyMessage msg(0,V_TEMP);" )

          gaduG 1 Reply Last reply
          0
          • M mbj

            Maybe I did not read your code good enough but I could not see any definition of the message type sendBatteryLevel (see an example for the definition of the temperature message in your code "MyMessage msg(0,V_TEMP);" )

            gaduG Offline
            gaduG Offline
            gadu
            wrote on last edited by
            #5

            @mbj

            Like this?

              // Initialize temperature message
             MyMessage msg(0,V_TEMP);
             MyMessage msgvolt(1,V_VOLTAGE);
            
            1 Reply Last reply
            0
            • gaduG Offline
              gaduG Offline
              gadu
              wrote on last edited by
              #6

              Nope, I cant get this to work...
              I really would appreciate if someone could help me with an working example where batterylevel can be presented as a MQTT topic.

              Help!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbj
                wrote on last edited by
                #7

                Unfortunately I am a beginner with all this but I have been successful getting messages over to Openhab via the MQTT binding.

                I can guess that the "sendBatteryLevel" example from the API pages is tailored for a Vera controller or similar but is not at all understood by Openhab. I have come across something similar when other sensors send commands directed to the controller itself.

                So my assumption was that if you should send something across that Openhab understands you could test creating a message like your example msgvolt and then send the info with something like gw.send(msgvolt.set(batteryV)) or gw.send(msgvolt.set(batteryV,1)) and pick this up with an item in Openhab.

                The messages type V_VAR1 and onwards can be used for custom values so I think you can use one of these to send the battery level percentage. Only way to find out is to experiment :-)

                gaduG 1 Reply Last reply
                0
                • M mbj

                  Unfortunately I am a beginner with all this but I have been successful getting messages over to Openhab via the MQTT binding.

                  I can guess that the "sendBatteryLevel" example from the API pages is tailored for a Vera controller or similar but is not at all understood by Openhab. I have come across something similar when other sensors send commands directed to the controller itself.

                  So my assumption was that if you should send something across that Openhab understands you could test creating a message like your example msgvolt and then send the info with something like gw.send(msgvolt.set(batteryV)) or gw.send(msgvolt.set(batteryV,1)) and pick this up with an item in Openhab.

                  The messages type V_VAR1 and onwards can be used for custom values so I think you can use one of these to send the battery level percentage. Only way to find out is to experiment :-)

                  gaduG Offline
                  gaduG Offline
                  gadu
                  wrote on last edited by
                  #8

                  Big thanks @mbj
                  I'm more of a beginner than you. Trust me. :-)

                  I'll do some experiments using you pointers.

                  gaduG K 2 Replies Last reply
                  0
                  • gaduG gadu

                    Big thanks @mbj
                    I'm more of a beginner than you. Trust me. :-)

                    I'll do some experiments using you pointers.

                    gaduG Offline
                    gaduG Offline
                    gadu
                    wrote on last edited by
                    #9

                    @mbj saved my day!!

                    publish: MyMQTT/21/0/V_TEMP 23.5
                    0;0;3;0;9;send: 0-0-21-21 s=0,c=1,t=0,pt=0,l=5,st=ok:23.5r
                    0;0;3;0;9;read: 21-21-0 s=1,c=1,t=24,pt=2,l=2:87
                    publish: MyMQTT/21/1/V_VAR1 87
                    

                    And you called yourself a beginner? ;-)

                    M 1 Reply Last reply
                    0
                    • gaduG gadu

                      @mbj saved my day!!

                      publish: MyMQTT/21/0/V_TEMP 23.5
                      0;0;3;0;9;send: 0-0-21-21 s=0,c=1,t=0,pt=0,l=5,st=ok:23.5r
                      0;0;3;0;9;read: 21-21-0 s=1,c=1,t=24,pt=2,l=2:87
                      publish: MyMQTT/21/1/V_VAR1 87
                      

                      And you called yourself a beginner? ;-)

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

                      @gadu Glad to be of any help, your turn next :-)

                      1 Reply Last reply
                      0
                      • gaduG Offline
                        gaduG Offline
                        gadu
                        wrote on last edited by
                        #11

                        If anything, let me know! Dunno if I can be of any help, but i could try :-)

                        Odd thing happend though, When the Mini is connected to the FTDI the serial monitor says "Check Wires", but on battery it sends everything correct?
                        Well, that's another issue to look at later...

                        1 Reply Last reply
                        0
                        • gaduG gadu

                          Big thanks @mbj
                          I'm more of a beginner than you. Trust me. :-)

                          I'll do some experiments using you pointers.

                          K Offline
                          K Offline
                          kunall
                          wrote on last edited by
                          #12

                          @gadu Can you post your working battery level send sketch for openhab? I would really appreciate it. I'm facing the same issue. Thanks!

                          gaduG 1 Reply Last reply
                          0
                          • HeinzH Offline
                            HeinzH Offline
                            Heinz
                            Hero Member
                            wrote on last edited by
                            #13

                            ... probably gw.sendBatteryLevel(percent); helps.

                            1 Reply Last reply
                            0
                            • K kunall

                              @gadu Can you post your working battery level send sketch for openhab? I would really appreciate it. I'm facing the same issue. Thanks!

                              gaduG Offline
                              gaduG Offline
                              gadu
                              wrote on last edited by
                              #14

                              @kunall

                              First these two rows before setup..

                              int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                              int oldBatteryPcnt = 0;
                              

                              and in the loop i have these rows...

                              // 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;
                              

                              in OpenHab items i have this row...

                              Number node2_batt  "Battery [%.1f %%]" <energy> (node2,All)  {mqtt="<[MySensor:MyMQTT/21/1/V_VAR1:state:default]"}
                              

                              hope this helps....

                              K 2 Replies Last reply
                              0
                              • gaduG gadu

                                @kunall

                                First these two rows before setup..

                                int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                int oldBatteryPcnt = 0;
                                

                                and in the loop i have these rows...

                                // 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;
                                

                                in OpenHab items i have this row...

                                Number node2_batt  "Battery [%.1f %%]" <energy> (node2,All)  {mqtt="<[MySensor:MyMQTT/21/1/V_VAR1:state:default]"}
                                

                                hope this helps....

                                K Offline
                                K Offline
                                kunall
                                wrote on last edited by
                                #15

                                Thanks alot @gadu . I will give this a try and get back to you! :+1:

                                1 Reply Last reply
                                0
                                • gaduG gadu

                                  @kunall

                                  First these two rows before setup..

                                  int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                  int oldBatteryPcnt = 0;
                                  

                                  and in the loop i have these rows...

                                  // 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;
                                  

                                  in OpenHab items i have this row...

                                  Number node2_batt  "Battery [%.1f %%]" <energy> (node2,All)  {mqtt="<[MySensor:MyMQTT/21/1/V_VAR1:state:default]"}
                                  

                                  hope this helps....

                                  K Offline
                                  K Offline
                                  kunall
                                  wrote on last edited by kunall
                                  #16

                                  @gadu How did you implement V_Var1 ? and assign it to child ID 1 ?

                                  Like this? if so, what did you use for gw.present(CHILD_ID, S_XXX) ?

                                  #define CHILD_ID 1
                                  myMessage msg(CHILD_ID, V_VAR1);
                                  
                                  1 Reply Last reply
                                  0
                                  • siodS Offline
                                    siodS Offline
                                    siod
                                    wrote on last edited by
                                    #17

                                    @gadu
                                    where is the imprtoant MQTT code of your sketch? Would be so kind and share it with us?

                                    Thanks in advance!!

                                    still learning...

                                    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