This commit is contained in:
Nikolay Lazarov 2024-03-24 19:20:22 +02:00
commit bb0975d091
4 changed files with 203 additions and 0 deletions

16
README.md Executable file
View File

@ -0,0 +1,16 @@
# HS110-Grafana
Power monitoring of the HS100/110 smart plugs via Grafana
Library used for the control of the smartplug - https://github.com/GadgetReactor/pyHS100
How to install:
Clone the repo to /opt/pyHS100 and run install.sh
git clone https://github.com/Amadeaus-dll/HS110-Grafana.git /opt/pyHS100
chmod u+x /opt/pyHS100/install.sh
The script automatically (if requested) installs InfluxDB and Grafana and starts the export script for the smart plug. The only thing that needs to be created is the graph in grafana.
Script is tested on Debian 9

44
influxdbexporter.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python3
from pyHS100 import SmartPlug
from influxdb import InfluxDBClient
from time import sleep
plug = SmartPlug("IP")
client = InfluxDBClient(database="dbase")
while True:
power = plug.get_emeter_realtime()['power']
client.write_points([{
"measurement": "power",
"fields": {
"value": power
}
}])
sleep(1)
voltage = plug.get_emeter_realtime()['voltage']
client.write_points([{
"measurement": "voltage",
"fields": {
"value": voltage
}
}])
sleep(1)
current = plug.get_emeter_realtime()['current']
client.write_points([{
"measurement": "current",
"fields": {
"value": current
}
}])
sleep(1)
total = plug.get_emeter_realtime()['total']
client.write_points([{
"measurement": "total",
"fields": {
"value": total
}
}])
sleep(1)

13
influxdbexporter.service Executable file
View File

@ -0,0 +1,13 @@
[Unit]
Description=Python Exporter for InfluxDB
[Service]
Type=simple
ExecStart=/usr/bin/python3 /opt/pyHS100/influxdbexporter.py
WorkingDirectory=/opt/pyHS100/
Restart=always
RestartSec=2
User=exporter
[Install]
WantedBy=sysinit.target

130
install.sh Executable file
View File

@ -0,0 +1,130 @@
#!/bin/bash
#Install basic packages
apt-get update
apt-get install apt-transport-https curl wget -y
#Ask the user if he wants to install influxdb.
echo -ne "Do you want to install influxdb [yes/no]: "; read responce
case $responce in
[yY] | [yY][eE][sS] | "" ) influxdb=yes ;;
[nN] | [nO][oO] ) influxdb=no ;;
esac
echo ""
if [[ ${influxdb} == "yes" ]]; then
curl -sL https://repos.influxdata.com/influxdb.key | apt-key add -
echo "#Influxdb repository" | tee -a /etc/apt/sources.list.d/influxdb.list
echo "deb https://repos.influxdata.com/debian stretch stable" | tee -a /etc/apt/sources.list.d/influxdb.list
apt-get update
apt-get install influxdb python3 python3-pip -y
sleep 2
pip3 install influxdb
sleep 2
systemctl daemon-reload
systemctl start influxd
sleep 2
echo ""
fi
#Ask the user if he wants to install grafana.
echo -ne "Do you want to install Grafana (amd64) (recommended for the most Systems) [yes/no]: "; read responce
case $responce in
[yY] | [yY][Ee][Ss] | "" ) grafana-amd64=yes ;;
[nN] | [nN][oO] ) grafana-amd64=no ;;
esac
echo ""
if [[ ${grafana-amd64} == "yes" ]]; then
curl https://packagecloud.io/gpg.key | apt-key add -
echo '#Grafana repository' | tee -a /etc/apt/sources.list.d/grafana.list
echo 'deb https://packagecloud.io/grafana/stable/debian/ stretch main' | tee -a /etc/apt/sources.list.d/grafana.list
apt-get update
apt-get install grafana -y
sleep 2
systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server.service
sleep 2
echo ""
fi
echo -ne "Do you want to install Grafana (armhf) (recommended for armhf based Systems like a Raspberry Pi) [yes/no]: "; read responce
case $responce in
[yY] | [yY][Ee][Ss] | "" ) grafana-armhf=yes ;;
[nN] | [nN][oO] ) grafana-armhf=no ;;
esac
echo ""
if [[ ${grafana-armhf} == "yes" ]]; then
cd /tmp
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.2.4_armhf.deb
dpkg -i grafana_5.2.4_armhf.deb
sleep 2
systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server.service
sleep 2
echo ""
fi
echo -ne "Do you want to install Grafana (arm64) (recommended for ARM64 based Systems) [yes/no]: "; read responce
case $responce in
[yY] | [yY][Ee][Ss] | "" ) grafana-arm64=yes ;;
[nN] | [nN][oO] ) grafana-arm64=no ;;
esac
echo ""
if [[ ${grafana-arm64} == "yes" ]]; then
cd /tmp
wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.2.4_arm64.deb
dpkg -i grafana_5.2.4_arm64.deb
sleep 2
systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server.service
sleep 2
echo ""
fi
#Variables
while [[ $db == "" ]]
do
read -p "Enter the desired database name you want to CREATE in influxdb: " db
echo "Enter a valid database name"
done
while [[ $retention == "" ]]
do
read -p "Enter the retention policy you wish to have for the data. Example - 30d: " retention
echo "Enter a valid retention policy value"
done
while [[ $policyname == "" || $policyname =~ [0-9] ]]
do
read -p "Enter the policy name (do not enter numbers in the name): " policyname
echo "Enter a valid policy name"
done
#Create the influx database depending on variables
echo "Creating Influxdb database $db ."
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE $db"
echo "Setting up retention policy on $db ."
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE RETENTION POLICY "$policyname" on "$db" DURATION $retention replication 1 DEFAULT"
#Ask for the smartplug IP
read -p "Enter the IP of your smartplug: " IP
#Adjust script
sed -i "s/IP/$IP/g" /opt/pyHS100/influxdbexporter.py
sed -i "s/dbase/$db/g" /opt/pyHS100/influxdbexporter.py
#Import the service and add the user
mv /opt/pyHS100/influxdbexporter.service /etc/systemd/system/
useradd -M -d /opt/pyHS100 -s /bin/false exporter
chown exporter:nogroup /opt/pyHS100 -R
systemctl daemon-reload
systemctl enable influxdbexporter.service
sleep 2
systemctl start influxdbexporter.service