Jumper5558
Posts: 2
Joined: Fri Nov 20, 2020 3:25 am

Monitoring voltage on pi with C++

Fri Nov 20, 2020 3:31 am

Hi,

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!

DarkElvenAngel
Posts: 1251
Joined: Tue Mar 20, 2018 9:53 pm

Re: Monitoring voltage on pi with C++

Fri Nov 20, 2020 11:33 pm

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.

Jumper5558
Posts: 2
Joined: Fri Nov 20, 2020 3:25 am

Re: Monitoring voltage on pi with C++

Tue Nov 24, 2020 4:08 pm

Hi,

Here is the link to the scripts Github.

https://github.com/alchemypower/uptime2.0

Thank you so much!!

DarkElvenAngel
Posts: 1251
Joined: Tue Mar 20, 2018 9:53 pm

Re: Monitoring voltage on pi with C++

Wed Nov 25, 2020 12:39 am

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 https://gitlab.com/DarkElvenAngel/argon ... rgononed.c I don't use any external libraries so this makes my code more portable but a little bit more work.

Return to “C/C++”