A powermeter data logger with Raspberry Pico W for powermeters with S0 interface. For logging into database and/or connection to FHEM home automation system
Go to file
Manuel Kamper a445655c48 fixes #4 and #5 2024-03-22 15:26:20 +01:00
LICENSE Initial commit 2023-06-09 09:47:49 +00:00
Logger.py Dateien hochladen nach „“ 2023-06-09 09:48:38 +00:00
NTP.py Dateien hochladen nach „“ 2023-06-09 09:48:38 +00:00
Networking.py Dateien hochladen nach „“ 2023-06-09 09:48:38 +00:00
README.md README.md aktualisiert 2023-12-21 19:09:02 +00:00
TimeUtils.py Dateien hochladen nach „“ 2023-06-09 09:49:13 +00:00
api.php implemented #3 2023-12-21 18:43:10 +00:00
configs.py configs.py aktualisiert 2023-12-21 16:33:07 +00:00
db.sql Dateien hochladen nach „“ 2023-06-09 10:02:22 +00:00
main.py fixes #4 and #5 2024-03-22 15:26:20 +01:00
pico-powermeter.png Dateien hochladen nach „“ 2023-06-09 20:41:39 +00:00
secrets.py Dateien hochladen nach „“ 2023-06-09 09:49:13 +00:00

README.md

pico-s0-powermeter

The main goal is, that you can track your power consumption (current and total) with a powermeter with a S0 connection (see for more details: Wikipedia S0 Schnittstelle). With this solution you also store this data in a database and retrieve the data for integration in your FHEM home automation system. You get also statistics from this data, where you can see how much your daily/weekly/monthly/yearly power consuption was. You do not need the database for the FHEM integration - if you do not need the data stored in a separate database, you can comment out the call of the functions for storing it in the database and for retrieving the statistics. Or the other way round, you can use it only to store data in the database and do not connect it with your FHEM instance.

Setup

  1. Upload all *.py files to your Raspberry Pico W
  2. Set your wifi credentials in secrets.py file. Also set an api_key.
  3. Set values in configs.py file (most important is ticks_per_kWh)
  4. Upload api.php to your internal webserver and configure your database connection
  5. Setup your Database with the file db.sql
  6. Be sure that you give your Pico W a static IP address on your router, so you know where the webservice is available and can configure it in FHEM

Hint: use openssl rand -hex 20 to generate the api_key

Hardware

If needed, you can print a case for your Raspberry Pico W: Thingiverse Pico W Box
You need a powermeter with S0 connection. I use this one: Amazon S0 powermeter
Probably you need a 5 Volt power source, I use this one for DIN-rail mount: Amazon 5V power source
You also need a 10 kOhm resistor.
Wire the Pico W as follows:
picture
Connect the green wire to S0- and the blue wire to S0+ on your powermeter.

PHP API

IMPORTANT: Please note, that the PHP API is just an example, with no focus on security or error handling. Use this example API only as a baseline for your own secure implementation!

Pico W API

You can configure the port, on which the API runs in configs.py in the 'api_port'-value. You also have to set an API-Key in secrets.py in the 'api'-value. Then you can call the API as follows:
http://servername/api_key/command[/json]

You can use the following commands:
settotal - Sets the powermeter total value (in Wh), only integer numbers
total - Returns the powermeter total value in kWh
current - Returns the current power consumption in W
ping - Check if the API is up and running
stats - Returns some stats of the device
reboot - Reboots the device
logs - See logs of the device (you can add a number like .../logs/12 to see the latest 12 log lines)

FHEM integration

This is a minimalistic integration of your powermeter in FHEM as a HTTPMOD device. You can use it as a starting point for your own custom implementation

defmod Powermeter HTTPMOD 0
attr Powermeter event-on-change-reading .*
attr Powermeter extractAllJSON 1
attr Powermeter get01Name ReadPowermeterStats
attr Powermeter get01Poll 1
attr Powermeter get01URL http://powermeter-hostname-or-ip/api_key/stats/json
attr Powermeter icon stromzaehler_icon
attr Powermeter stateFormat Aktuell: current W<br/>Zähler: Powermeter_Powermeter_count__kWh_ kWh<br/>Heute: Powermeter_Consumption_Today__kWh_ kWh<br/>Woche: Powermeter_Consumption_this_week__kWh_ kWh
attr Powermeter userReadings current {ReadingsVal("Powermeter","Powermeter_Current_power_consumption__W_","-")}