The ammeter_logger library is intended to work in conjunction with the Micropython adc_amperage application running on a microcontroller. The ammeter_logger allows for logging of the ammeter data and remote initialization of the microcontroller.
- ammeter_logger – Install from PyPi
- Usage Example – CLI
- Usage Example – Importing into Code
- class AmmeterRecvSerial(serial.Serial)
- __init__(*args, **kwargs, logger=logging)
- ammeter_start(timeout=None)
- ammeter_stop()
- ammeter_report(wait=5)
- ammeter_init()
- @property ammeter_status()
- @property ammeter_initialized()
- @property ammeter_running()
- @property ammeter_ready()
- @property ammeter_config()
- @property ammeter_interval()
- @ammeter_interval.setter(value)
- @property ammeter_current()
- @property _info_str()
- _ammeter_read()
- _ammeter_parse_read_line(respones:list)
Related LearningToPi Posts | |
Git: https://github.com/LearningToPi/ammeter_logger | |
PyPi: https://pypi.org/project/ammeter-logger/ | |
Micropython adc_amperage project |
ammeter_logger
– Install from PyPi
pip3 install -U ammeter-logger
Usage Example – CLI
One use case is to use the ammeter_logger from the command line. This method allows for:
- Initialization of the microcontroller to set the 0 amp level from the sensor with no load on the device
- Logging of amperage data during a device powering on (this does require a separate system connected to the microcontroller to log the data)
- Some basic configuration and status validation
(venv) $ python3 -m ammeter_logger --help
usage: __main__.py [-h] [--get-config] [--get-status] [--skip-init] [--force-init] [--init-only] [--sample-interval SAMPLE_INTERVAL]
[--capture-time CAPTURE_TIME] [--baudrate BAUDRATE] [--log-level LOG_LEVEL]
DEVICE OUTPUT_FILE
Start the ammeter data collector. Requires the sender to be running (provided sender is Micropython for a microcontroller)
positional arguments:
DEVICE Serial device connected to the microcontroller (i.e. /dev/ttyUSB0
OUTPUT_FILE File to save captured data to
optional arguments:
-h, --help show this help message and exit
--get-config (False) Get the configuration from the microcontroller and quit
--get-status (False) Get the current status of the microcontroller and quit
--skip-init (False) Skip initializing the ammeter (not recommended!)
--force-init (False) Force init of the ammeter
--init-only (False) Only initalize the ammeter, print the config and status and quit (implies --force-init)
--sample-interval SAMPLE_INTERVAL
Set the sampling interval, overrides the config on the microcontroller
--capture-time CAPTURE_TIME
Set the max time to capture before stopping, overrides the config on the microcontroller
--baudrate BAUDRATE (115200) Set the baudrate for the serial interface
--log-level LOG_LEVEL
(INFO) Specify the logging level for the console
(venv) pi@rpi-kvm1:~/dev/ammeter_recv $ python3 -m ammeter_logger /dev/ttyUSB0 out.csv --init-only
2022-11-16 12:10:05,650 - root - INFO - AmmeterRecvSerial: /dev/ttyUSB0: 115200: 8N1: Starting backgroup ammeter read.
Waiting for ammeter to initialize. {'status': 'NOINIT', 'timeout': 0, 'noinit_pin': 'sensor1pin32'}
Waiting for ammeter to initialize. {'status': 'INITIALIZING', 'timeout': '30', 'noinit_pin': None}
Waiting for ammeter to initialize. {'status': 'INITIALIZING', 'timeout': '28', 'noinit_pin': None}
<... output omitted ...>
Waiting for ammeter to initialize. {'status': 'INITIALIZING', 'timeout': '2', 'noinit_pin': None}
Current Config: {'interval': 50, 'timeout': 30, 'init_timeout': 30, 'pins': [{'pin': 32, 'name': 'sensor1pin32', 'baseline': 2413664}]}
Current Status: {'status': 'READY', 'timeout': 0, 'noinit_pin': None}
(venv) pi@rpi-kvm1:~/dev/ammeter_recv $ python3 -m ammeter_logger /dev/ttyUSB0 out.csv --capture-time 300
(venv) pi@rpi-kvm1:~/dev/ammeter_recv $ python3 -m ammeter_logger /dev/ttyUSB0 out.csv --get-status
2022-11-16 12:03:33,238 - root - INFO - AmmeterRecvSerial: /dev/ttyUSB0: 115200: 8N1: Starting backgroup ammeter read.
Current Status: {'status': 'READY', 'timeout': 0, 'noinit_pin': None}
Usage Example – Importing into Code
The other use case is to incorporate the ammeter_logger into other code. This method allows for:
- Initialization of the microcontroller 0 amp level. NOTE: This asumes no load on the line to initialize, so be sure this is run from a different device than the one being monitored, otherwise the microcontroller will set the running amperage of the device as “0 amps” and only show change in amperage from that level.
- Logging of amperage data during other tasks (see the python load testing module for an example)
- Microcontroller status and configuration
from ammeter_logger import AmmeterRecvSerial
class AmmeterRecvSerial(serial.Serial)
__init__(*args, **kwargs, logger=logging)
Initalizes the AmmeterRecvSerial instance. AmmeterRecvSerial inherits serial.Serial and will pass through all args other than ‘logger’ which defaults to the root Python logging daemon. See logging posts for the Python logging_handler module if you are looking for a quick method to build handlers for console, file or syslog.
From the PySerial documentation page: __init__(port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None, exclusive=None)
ammeter_start(timeout=None)
Starts the sampling on the microcontroller. timeout can be used to specify a max time to capture. If no time is specified, the timeout in the microcontroller config will be used.
ammeter_stop()
Stops sampling on the microcontroller if it is currently running.
ammeter_report(wait=5)
Returns the data that has been collected from the microcontroller. If the collection is currently running, waits up to wait seconds for the collection to end.
ammeter_init()
Starts the initialization on the microcontroller. Initialization sets the 0 amp level reading from the ADC sensor. Be sure there is no load on the sensor when running the initialization.
@property ammeter_status()
Queries the microcontroller for updated status, and returns a dictionary object.
@property ammeter_initialized()
Returns True/False if the microcontroller is initialized.
@property ammeter_running()
Returns True/False if the microcontroller is currently collecting data.
@property ammeter_ready()
Returns True/False if the microcontroller is ready to start collecting (i.e. initialization has been completed).
@property ammeter_config()
Returns the current configuration on the ammeter. This includes the current initilization value (the voltage on the ADC sensor that represents 0 amps) as well as the default collection interval and collection timeout.
@property ammeter_interval()
Returns the interval between updates on the microcontroller in milliseconds.
@ammeter_interval.setter(value)
Sets the interval between updates on the microcontroller in milliseconds.
@property ammeter_current()
Sends a command to the controller to get the current amperage reading and returns it as a float. This value is a one off and is not averaged across several reads like data collected from the report.
@property _info_str()
Internal function. Used for logging purposes to return the class name and serial port info for log messages.
_ammeter_read()
Background loop that runs as a python Thread and checks for data on the serial line. DO NOT CALL DIRECTLY.
_ammeter_parse_read_line(respones:list)
Internal function to parse the data received on the serial interface.