How to get battery volt / percentage message send to mqtt?
-
@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
-
@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
-
@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``` -
@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? -
the second one is useless. It is odd that it misses the analog reads. What AVR boards version are you using?
-
Try a simple example sketch that reads the analog pin just to make sure the hardware is OK, or try another pro mini