rpi-power-monitor
rpi-power-monitor copied to clipboard
Refactoring into a Class
I wanted to share and see if there's any interest in further development. In order to make installing and using this library easier, I turned your code into a Class, as well as created a setup.py to allow easy installing via pip, at https://github.com/kizniche/rpi-power-monitor. It can then be installed as a package with:
python -m virtualenv ./venv
./venv/bin/python -m pip install git+https://github.com/kizniche/rpi-power-monitor.git
Then, for example:
from rpi_power_monitor import power_monitor
rpm = power_monitor.RPiPowerMonitor()
rpm.run_main()
This allows me to import it in a new input module I created for Mycodo, which uses it like this:
from rpi_power_monitor import power_monitor
phase_correction = {
'ct1': self.ct1_phase_correction,
'ct2': self.ct2_phase_correction,
'ct3': self.ct3_phase_correction,
'ct4': self.ct4_phase_correction,
'ct5': self.ct5_phase_correction,
'ct6': self.ct6_phase_correction,
}
accuracy_calibration = {
'ct1': self.ct1_accuracy_calibration,
'ct2': self.ct2_accuracy_calibration,
'ct3': self.ct3_accuracy_calibration,
'ct4': self.ct4_accuracy_calibration,
'ct5': self.ct5_accuracy_calibration,
'ct6': self.ct6_accuracy_calibration,
'AC': self.ac_accuracy_calibration,
}
self.sensor = power_monitor.RPiPowerMonitor(
grid_voltage=self.grid_voltage,
ac_transformer_output_voltage=self.transformer_voltage,
ct_phase_correction=phase_correction,
accuracy_calibration=accuracy_calibration)
board_voltage = self.sensor.get_board_voltage()
samples = self.sensor.collect_data(2000)
rebuilt_waves = self.sensor.rebuild_waves(
samples,
self.sensor.ct_phase_correction['ct1'],
self.sensor.ct_phase_correction['ct2'],
self.sensor.ct_phase_correction['ct3'],
self.sensor.ct_phase_correction['ct4'],
self.sensor.ct_phase_correction['ct5'],
self.sensor.ct_phase_correction['ct6'])
results = self.sensor.calculate_power(rebuilt_waves, board_voltage)
self.logger.info(f"Voltage: {board_voltage}")
chan = 1
for ct in range(1, 7):
self.logger.info(f"Power {chan}: {results[f'ct{ct}']['power']} W")
self.logger.info(f"Current {chan}: {results[f'ct{ct}']['current']} A")
self.logger.info(f"Power Factor {chan}: {results[f'ct{ct}']['pf']}")
chan += 3
Here's the module being configured and running in the web UI (only the first channel is enabled):
Input Page

Live Page

Dashboard Graph Widget

Hey @kizniche, that's looking awesome! I really like what you're doing here and don't have any oppositions into making a class out of it.
I see you've already accounted for the command line interaction as well, like being able to start the monitor in terminal, debug, or phase mode. That was my only real concern because of all the Wiki documentation I've put together.
I am very swamped with different projects right now and don't have too much bandwidth available to implement and test this at the moment, but I would like to dive into this further in the future.
Merged into master and released in v0.2.0! Thank you, @kizniche!