diff --git a/example/logger/README.md b/example/logger/README.md index c59495b..c926ae7 100644 --- a/example/logger/README.md +++ b/example/logger/README.md @@ -1,46 +1,140 @@ -# Power Consumption Logger Example +# Odroid PowerMate Logger and Plotter -Based on this script, you can monitor power consumption and implement graph plotting. +This directory contains two Python scripts to log power data from an Odroid PowerMate device and visualize it. -## How to Run the Script +1. `logger.py`: Connects to the device's web server, authenticates, and logs real-time power data from its WebSocket to a CSV file. +2. `csv_2_plot.py`: Reads the generated CSV file and creates a plot image of the power, voltage, and current data over time. -### Install Python Virtual Environment +## Prerequisites -```shell +### 1. Clone this example +```bash git clone https://github.com/hardkernel/odroid-powermate.git cd odroid-powermate/example/logger ``` -```shell -sudo apt install virtualenv -virtualenv venv -source venv/bin/activate +### 2. Python and Virtual Environment + +It is highly recommended to use a Python virtual environment to manage project dependencies and avoid conflicts with other projects. + +Ensure you have Python 3 installed. + +1. **Create a virtual environment:** + Open your terminal in this directory and run: + ```bash + python3 -m venv venv + ``` + This will create a `venv` directory containing the Python interpreter and libraries. + +2. **Activate the virtual environment:** + * **On Windows:** + ```powershell + .\venv\Scripts\activate + ``` + * **On macOS and Linux:** + ```bash + source venv/bin/activate + ``` + Your terminal prompt should now show `(venv)` at the beginning, indicating that the virtual environment is active. + +### 3. Install Required Libraries + +With the virtual environment activated, install the necessary Python packages: + +```bash +pip3 install requests websockets protobuf pandas matplotlib python-dateutil ``` -### Install require package +### 4. Protobuf Generated File -```shell -pip install grpcio-tools requests websockets protobuf pandas matplotlib +The `logger.py` script uses Google Protocol Buffers (Protobuf) to decode real-time data from the WebSocket. This requires a Python file, `status_pb2.py`, which is generated from a Protobuf definition file (`status.proto`). + +**How to Generate `status_pb2.py`:** + +1. **Install Protobuf Compiler Tools:** + You need the `grpcio-tools` package, which includes the `protoc` compiler and Python plugins. You can install it via pip: + ```bash + pip3 install grpcio-tools + ``` + +2. **Locate the `.proto` file:** + Ensure you have the `status.proto` file in the current directory. This file defines the structure of the data messages. + +3. **Run the Compiler:** + Execute the following command in your terminal. This command tells `protoc` to look for `status.proto` in the directory (`-I../../proto`) and generate the Python output file (`--python_out=.`) in the same place. + ```bash + python3 -m grpc_tools.protoc -I../../proto --python_out=. status.proto + ``` +After running this command, the `status_pb2.py` file will be created, and `logger.py` will be able to use it. + +## Usage + +The process is a two-step workflow: first log the data, then plot it. + +### Step 1: Log Power Data with `logger.py` + +Run `logger.py` to connect to your Odroid Smart Power device and save the data to a CSV file. + +**Syntax:** +```bash +python3 logger.py -u -p -o ``` -### Build `status_pb2.py` +**Arguments:** +* `host`: The IP address or hostname of the Odroid Smart Power device (e.g., `192.168.1.50`). +* `-u`, `--username`: The username for logging in. +* `-p`, `--password`: The password for logging in. +* `-o`, `--output`: The path to save the output CSV file. This is required if you want to generate a plot. -```shell -python -m grpc_tools.protoc -I ../../proto --python_out=. status.proto +**Example:** + +This command will log in and save the power data to `power_log.csv`. + +```bash +python3 logger.py 192.168.1.50 -u admin -p mypassword -o power_log.csv ``` -### Execute script +The script will continue to log data until you stop it with `Ctrl+C`. -#### Power consumption collection -```shell -# python3 logger.py -u -o -p
-python3 logger.py -u admin -p password -o test.csv 192.168.30.5 +### Step 2: Generate a Plot with `csv_2_plot.py` + +Once you have a CSV log file, you can use `csv_2_plot.py` to create a visual graph. +You can also use the csv file recorded from PowerMate Web. + +**Syntax:** +```bash +python3 csv_2_plot.py [options] ``` -#### Plot data +**Arguments:** +* `input_csv`: The path to the CSV file generated by `logger.py`. +* `output_image`: The path to save the output plot image (e.g., `plot.png`). -```shell -python3 csv_2_plot.py test.csv plot.png [--type power voltage current] [--source vin main usb] +**Optional Arguments:** +* `-t`, `--type`: Specify which plots to generate. Choices are `power`, `voltage`, `current`. Default is all three. +* `-s`, `--source`: Specify which power sources to include. Choices are `vin`, `main`, `usb`. Default is all three. + +**Example 1: Default Plot** + +This command reads `power_log.csv` and generates a plot containing power, voltage, and current for all sources, saving it as `power_graph.png`. + +```bash +python3 csv_2_plot.py power_log.csv power_graph.png ``` -![plot.png](plot.png) \ No newline at end of file +**Example 2: Custom Plot** + +This command generates a plot showing only the **power** and **current** for the **MAIN** and **USB** sources. + +```bash +# main, usb power consumption +python csv_2_plot.py power_log.csv custom_plot.png --type power --source main usb +``` + +## Example Output + +Running the plot script will generate an image file similar to this: + +![plot.png](img/plot.png) + +The 5-unit scale is highlighted with a blue dotted line, and the 10-unit scale is highlighted with a red dotted line. \ No newline at end of file diff --git a/example/logger/img/plot.png b/example/logger/img/plot.png new file mode 100644 index 0000000..1a6331f Binary files /dev/null and b/example/logger/img/plot.png differ diff --git a/example/logger/plot.png b/example/logger/plot.png deleted file mode 100644 index 916615b..0000000 Binary files a/example/logger/plot.png and /dev/null differ