MQTT and batterylevel



  • 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);
     }


  • Sorry for bumping, but i'm lost... 😞
    Help?



  • This post is deleted!


  • 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);" )



  • @mbj

    Like this?

      // Initialize temperature message
     MyMessage msg(0,V_TEMP);
     MyMessage msgvolt(1,V_VOLTAGE);


  • 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!



  • 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 πŸ™‚



  • Big thanks @mbj
    I'm more of a beginner than you. Trust me. πŸ™‚

    I'll do some experiments using you pointers.



  • @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? πŸ˜‰



  • @gadu Glad to be of any help, your turn next πŸ™‚



  • 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...



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


  • Hero Member

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



  • @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....



  • Thanks alot @gadu . I will give this a try and get back to you! πŸ‘



  • @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);
    


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

    Thanks in advance!!


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts