Reading Smart-meter S0-Interface using GPIO and inserting it to MySQL to Grafana
- Node.js
- Grafana Server
- MySQL Database
- npm / Node.js installation
sudo apt update
sudo apt upgrade
sudo apt install npm
- Grafana installation
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install grafana
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
- mysql database installation
sudo apt install mariadb-server
sudo mysql_secure_installation
- Set-up script
- Download Repo
git clone https://github.com/ledahosn/power_smart_meter_s0
cd power_smart_meter_s0
- Install dependencies
npm install
- Create .env file from example
cp .env.example .env
- Edit .env with your Credentials
- start script manually
node main.js [CURRENT_WATTS_ON_SMARTMETER]
- using npm package forever-service to create a service
npm install -g forever
npm install -g forever-service
sudo forever-service install power_smart_meter_s0 --script main.js
- Download Repo
- mysql database configuration
- Create DB
CREATE DATABASE smart_home;
- Use DB
USE smart_home;
- Create the table
CREATE TABLE power_usage( id int(11) NOT NULL AUTO_INCREMENT, ms_between_pulse int(11) DEFAULT NULL, current_power_consumption float DEFAULT NULL, script_timestamp datetime DEFAULT NULL, insert_timestamp datetime DEFAULT current_timestamp(), total_pulse_count int(11) DEFAULT NULL, PRIMARY KEY (id));
- Create user for grafana
CREATE USER 'grafana'@'localhost' IDENTIFIED BY 'PASS';
- Create user for node-script
CREATE USER 'nodescript'@'localhost' IDENTIFIED BY 'PASS';
- Grant read-only to grafana user
GRANT SELECT, SHOW VIEW, PROCESS, REPLICATION CLIENT ON smart_home.* TO 'grafana'@'localhost';
- Grant all to node-script
GRANT ALL PRIVILEGES ON smart_home.* TO 'nodescript'@'localhost';
- Create DB
- Grafana Panel SQL
- Current Power Usage
SELECT date_add(insert_timestamp, interval -2 hour) as 'time', current_power_consumption as 'Current Power Usage' FROM stromverbrauch ORDER BY insert_timestamp
- Total Power Usage & Cost
SELECT date_add(insert_timestamp, interval -2 hour) as 'time', total_pulse_count as 'Total Power Usage', total_pulse_count/1000*0.2 as 'Total Costs' FROM stromverbrauch ORDER BY insert_timestamp
- Daily Power Usage & Cost
SELECT date_add(STR_TO_DATE(CONCAT(SUBSTR(insert_timestamp, 1, 10), ' 00:00:00'), '%Y-%m-%d %T'), interval -2 hour) as 'time', MAX(total_pulse_count)-MIN(total_pulse_count) as 'Daily Power Usage', (MAX(total_pulse_count)-MIN(total_pulse_count))/1000*0.2 as 'Daily Costs' FROM stromverbrauch GROUP BY time
- Current Power Usage