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



  • I Build a temp / hum sensor with the easyPcb board and Am able to get the temp and hum send to the Mqtt broker.
    Next thing is to get the battery voltage and % send to the Mqtt broker as well.
    Now that is giving me some problems. I merged the two scripts together, and tried to learn form other topics. But I'M not getting is right dough. This is my script right now:
    Also the output of the serial monitor.
    I have the feeling I am nearly there 🙂 Any help would be great!

    [code]
    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0: Henrik EKblad
     * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
     * 
     * DESCRIPTION
     * This sketch provides an example of how to implement a humidity/temperature
     * sensor using a DHT11/DHT-22.
     *  
     * For more information, please visit:
     * http://www.mysensors.org/build/humidity
     * 
     HTU21D Humidity Sensor
     Hardware Connections (Breakoutboard to Arduino):
     -VCC = 3.3V
     -GND = GND
     -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
     -SCL = A5 (use inline 330 ohm resistor if your board is 5V)
     */
    
     
    #define MY_NODE_ID 2
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_RS485
     
    #include <SparkFunHTU21D.h>
    #include <MySensors.h>  
    #include <Wire.h>
    #include <SPI.h>
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    //static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_BATTERY 2
    //#define CHILD_ID_VOLT 2
    #define CHILD_ID_PRCNT 3
    
    float lastTemp;
    float lastHum;
    
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    boolean metric = true;
    
    
    void presentation()  
    { 
     // Send the Sketch Version Information to the Gateway                                                                                                                     
      sendSketchInfo("Humidity", "2.0");      
       
     // Register all sensors to gw (they will be created as child devices)                                                                                                     
      present(CHILD_ID_HUM, S_HUM);                                                                                                                                          
      present(CHILD_ID_TEMP, S_TEMP);
       present(CHILD_ID_BATTERY, S_MULTIMETER);                                                                                                                                         
      //metric = getControllerConfig().isMetric;
    
    
       
    }
    
                                                                                                                                                      
    unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)                                                                                             
                                                                                                                                                                            
                                                                                                                                                                   
                                                                                                                                                                            
    //Create an instance of the object                                                                                                                                          
    HTU21D myHumidity;                                                                                                                                                         
    
                                                                                                                                                                                                                                                                                                                                 
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);                                                                                                                                      
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);                                                                                                                                   
    MyMessage msgBattery(CHILD_ID_BATTERY, V_VOLTAGE);
    //MyMessage msgBattery(CHILD_ID_BATTERY, V_PRCNT);
    
    
    void setup() {
        myHumidity.begin();
    
         // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
        analogReference(INTERNAL1V1);
    #else
        analogReference(INTERNAL);
    #endif
    } 
                                                                                                                                                                                                                                                                                                                                                     
    void loop()                                                                                                                                                                 
    {                                                                                                                                                                           
      float temperature = myHumidity.readTemperature();                                                                                                                         
      //if (!metric) {                                                                                                                                                            
      //    temperature = (temperature * 1.8) + 32.0;                                                                                                                             
      //}                                                                                                                                                                         
      send(msgTemp.set(temperature, 1));
      Serial.print("T: ");
      Serial.println(temperature);                                                                                                                                     
    
      float humidity = myHumidity.readHumidity();                                                                                                                               
      send(msgHum.set(humidity, 1));                                                                                                                                         
      Serial.print("H: ");
      Serial.println(humidity);
    
    
       // get the battery Voltage
       int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    
    //SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
    int oldBatteryPcnt = 0;
    
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
    
    #ifdef MY_DEBUG
        Serial.println(sensorValue);
    #endif
    
        // 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
    
        int batteryPcnt = sensorValue / 10;
    
    #ifdef MY_DEBUG
        float batteryV  = sensorValue * 0.003363075;
        Serial.print("Child ID ");
        Serial.print(CHILD_ID_BATTERY);
        Serial.print("Battery Voltage: ");
        Serial.print(batteryV);
        Serial.println(" V");
    
        Serial.print("Battery Percent: ");
        Serial.print(batteryPcnt);
        Serial.println(" %");
    #endif
    
        if (oldBatteryPcnt != batteryPcnt) {
            // Power up radio after sleep
            sendBatteryLevel(batteryPcnt);
            oldBatteryPcnt = batteryPcnt;
        } 
    
      
    
                                                                                                                                                                                
      sleep(SLEEP_TIME); //sleep a bit  
                                                                                                                                           
    }
    
    
    
    [/code]```
    
    

    16 MCO:BGN:INIT NODE,CP=RNNNA---,VER=2.2.0
    26 TSM:INIT
    28 TSF:WUR:MS=0
    34 TSM:INIT:TSP OK
    36 TSM:INIT:STATID=2
    38 TSF:SID:OK,ID=2
    40 TSM:FPAR
    43 TSM:FPAR:STATP=0
    45 TSM:ID
    47 TSM:ID:OK
    47 TSM:UPL
    51 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    59 TSF:MSG:READ,0-0-2,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    65 TSF:MSG:PONG RECV,HP=1
    69 TSM:UPL:OK
    69 TSM:READY:ID=2,PAR=0,DIS=1
    75 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    81 TSF:MSG:READ,0-0-2,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    90 TSF:MSG:SEND,2-2-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.2.0
    100 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2111 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=11,pt=0,l=8,sg=0,ft=0,st=OK:Humidity
    2119 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:2.0
    2129 TSF:MSG:SEND,2-2-0-0,s=0,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2138 TSF:MSG:SEND,2-2-0-0,s=1,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
    2146 TSF:MSG:SEND,2-2-0-0,s=2,c=0,t=30,pt=0,l=0,sg=0,ft=0,st=OK:
    2154 MCO:REG:REQ
    2158 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2179 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2185 MCO:PIM:NODE REG=1
    2187 MCO:BGN:STP
    2189 MCO:BGN:INIT OK,TSP=1
    2244 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.2
    T: 19.20
    2277 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:42.7
    H: 42.68
    127
    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.1
    T: 19.14
    2396 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:42.7
    H: 42.71
    0
    Child ID 2Battery Voltage: 0.00 V
    Battery Percent: 0 %
    2406 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2414 TSF:TDI:TSL
    2416 MCO:SLP:WUP=-1
    2418 TSF:TRI:TSB
    2473 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:20.3
    T: 20.33
    2506 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:55.9
    H: 55.90
    0
    Child ID 2Battery Voltage: 0.00 V
    Battery Percent: 0 %
    2516 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2525 TSF:TDI:TSL
    2527 MCO:SLP:WUP=-1
    2529 TSF:TRI:TSB
    2584 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.59
    2617 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:45.1
    H: 45.06
    0
    Child ID 2Battery Voltage: 0.00 V
    Battery Percent: 0 %
    2627 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2635 TSF:TDI:TSL
    2637 MCO:SLP:WUP=-1
    2639 TSF:TRI:TSB
    2695 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.4
    T: 19.39
    2727 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:43.9
    H: 43.87
    0
    Child ID 2Battery Voltage: 0.00 V
    Battery Percent: 0 %
    2738 MCO:SLP:MS=60000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2746 TSF:TDI:TSL
    2748 MCO:SLP:WUP=-1
    2750 TSF:TRI:TSB
    2807 TSF:MSG:SEND,2-2-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:19.3
    T: 19.30
    2838 TSF:MSG:SEND,2-2-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:43.5
    H: 43.48
    0


  • Mod

    @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


  • Mod

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



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

  • Mod

    it looks as the analogread keeps returning zero



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


  • Mod

    try adding a delay(500) before the analogread



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

  • Mod

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



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


  • Mod

    I mean in Arduino IDE library manager



  • @gohan

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



  • Still not getting it working! Any thoughts on this?


  • Mod

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



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


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 6
  • 2
  • 5
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts