Page 1 of 1

Monitoring voltage on pi with C++

Posted: Fri Nov 20, 2020 3:31 am
by Jumper5558

I have the pi-uptime UPS 2.0 and I am trying to mimick their python script to shut the pi if it reaches a certain level of voltage for a low battery.

I am not sure where to start and how I can do something like this in C++.

I am a beginner, if anyone can help it would be greatly appreciated!!

Thank you!

Re: Monitoring voltage on pi with C++

Posted: Fri Nov 20, 2020 11:33 pm
by DarkElvenAngel
Interesting I did something similar with the Argon one cases and wrote a service in C to replace the Python script they provide. If you could link to the script I would be happy to look at it and offer a suggestion.

Re: Monitoring voltage on pi with C++

Posted: Tue Nov 24, 2020 4:08 pm
by Jumper5558

Here is the link to the scripts Github.

Thank you so much!!

Re: Monitoring voltage on pi with C++

Posted: Wed Nov 25, 2020 12:39 am
by DarkElvenAngel
Seems pretty straight forward,

You have a TI TLA2024 ADC connected to the i2c bus at address 0x48 this has four channels
0 V-in
1 V-out
2 V-battery
3 V across NTC, to measure Temperature

What you need to start with is getting the values from the ADC these will be raw values and you'll need to do some math on them to get a meaningful value. Everything you need in that regard is in the python script.

Once you can understand the values you can setup a loop that checks the voltages are in range and if they drop too low you can exit the loop and execute the shutdown command.

It's up to you how you want to access the i2c bus you can use a library or use linux ioctl. A library will be easier but can limit the portability of your code.

Shutdown is best called with system function call.

Take a look at other projects out there, If you want to see the Argon One daemon I wrote look here ... rgononed.c I don't use any external libraries so this makes my code more portable but a little bit more work.