This is a fairly simple application which retrieves the current usage data from a Shelly Plug S or Shelly Plug and saves it to a MySQL DB for later analysis.
I use three Shelly Plug S. I created a Database "shellyData". For each plug I created one table:
CREATE TABLE `shellyNAME` (
`entryID` INT NOT NULL AUTO_INCREMENT,
`timestamp` DATETIME,
`powerLevel` INT,
PRIMARY KEY (`entryID`)
);
The application is built in such a way that it is easy to add/remove Plugs. To add a new plug, just add a new enviorment variable like this:
let shelly_<NUMBER> = Vec::from_iter(env::var("SHELLY_<NUMBER>").expect("SHELLY_<NUMBER> is not defined")
.split("|").map(String::from));
let shelly_<NUMBER>_ip = shelly_<NUMBER>.get(0).expect("Error when getting IP");
let shelly_<NUMBER>_table = shelly_<NUMBER>.get(1).expect("Error when getting table name");
Then, inside the loop in the main function, add the following:
add_to_db(shelly_<NUMBER>_ip, &shelly_<NUMBER>_table, &pool)
.await.expect(format!("Error when saving {}", &shelly_<NUMBER>_table).as_str());
To build from source, run:
cargo build --release
Then, start the application:
DB_HOST='DB_IP' \
DB_PASSWORD='DB_PASSWORD' \
DB_USER='DB_USERNAME' \
SAVE_INTERVAL='SAVE_INTERVAL_IN_MS' \
SHELLY_ONE='SHELLY_IP|DB_TABLE_NAME' \
SHELLY_TWO='SHELLY_IP|DB_TABLE_NAME' \
SHELLY_THREE='SHELLY_IP|DB_TABLE_NAME' \
./target/release/shelly-save-to-db
Build the Docker image with:
docker build -t shelly-save-to-db:latest .
Then, run it with:
docker run \
-e DB_HOST='DB_IP' \
-e DB_PASSWORD='DB_PASSWORD' \
-e DB_USER='DB_USERNAME' \
-e SAVE_INTERVAL='SAVE_INTERVAL_IN_MS' \
-e SHELLY_ONE='SHELLY_IP|DB_TABLE_NAME' \
-e SHELLY_TWO='SHELLY_IP|DB_TABLE_NAME' \
-e SHELLY_THREE='SHELLY_IP|DB_TABLE_NAME' \
shelly-save-to-db:latest
Or Docker compose:
name: shelly-save-to-db
services:
shelly-save-to-db:
environment:
- DB_HOST='DB_IP'
- DB_PASSWORD='DB_PASSWORD'
- DB_USER='DB_USERNAME'
- SAVE_INTERVAL='SAVE_INTERVAL_IN_MS'
- SHELLY_ONE='SHELLY_IP|DB_TABLE_NAME'
- SHELLY_TWO='SHELLY_IP|DB_TABLE_NAME'
- SHELLY_THREE='SHELLY_IP|DB_TABLE_NAME'
container_name: shelly-save-to-db
image: shelly-save-to-db:latest