Reading a P1 port from a dutch Smart Meter with a Raspberry Pi and Node.js (WIP)

Picture by Quang Nguyen Vinh
Multimeter

P1 Port

A P1 port is a serial port on your Smart Meter. It's connector is a Registered jack (RJ). This plugin is known from the old telephone cables.

It sends a telegram (serial data) every second or 10 seconds (depending on your meter) over 1 of the pins when you apply voltage to the correct pin (RJ consists of 6 wires and 6 corresponding pins).

What we build

The project consists of 2 parts.

  1. The hardware (cable or circuit) to connect the P1 port with a Raspberry Pi.
  2. Software to read the telegram and save it to the database.

For connecting the P1 port to the Pi you can buy a ready made cable or make your own. In this guide we will make our own. For the software we will create a Node.js app and use some npm packages to save the data to a PostgreSQL database.

What we need

The schematic

P1 schematic

Raspberry Pi

Setting up the basics

If you have a new SD card you should first install Raspbian

Enabling SSH and add new users

By adding an empty ssh file in the boot/root directory of the sd card you enable ssh so a monitor is not needed for logging in. Follow this guide to login with the default pi user and ad your own user. Also add your user to the sudo group.

Update OS

$ sudo apt update
$ sudo apt full-upgrade

Login without promting password

Uitwerken

Setting correct system time

$ sudo raspi-config

ntpdate for correct time

Setting up the Raspberry Pi for the project

Install NodeJS

With nvm (Node Version Manager)

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Logout and log back in.

$ nvm install --lts

Or for the Raspberry Pi Zero (W)

$ NVM_NODEJS_ORG_MIRROR=https://unofficial-builds.nodejs.org/download/release nvm install --lts

Getting SSL certificates

sudo apt install certbot
sudo certbot certonly --standalone

Install Nginx for reverse proxy

sudo apt install nginx

Create a configuration file for the app in /etc/nginx/conf.d/. Replace example.com in this example with your app’s domain or public IP address:

server {
listen 80;
server_name example.com;

return 301 https://example.com$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

location / {
proxy_pass http://localhost:4000/;
}
}

Add the following to /etc/nginx/nginx.conf if yout get an error about server_names_hash_bucket_size.

http {
server_names_hash_bucket_size 64;
}

Nginx Reverse proxy && SSL reverse proxy ssl

certbot

Install pm2

$ npm install pm2 -g
$ pm2 startup
$ pm2 start --time npm --name home-care-api -- run server

Install git

$ sudo apt install git

Build Prisma engines

At the time of writing prisma doesn't prebuild engines for arm devices, so you need to build them yourself. prisma-engines build instructions

$ sudo apt install libssl-dev

Install PostgreSQL

instructions

Remove shell over serial and enable hardware

$ sudo raspi-config

Allow user to access gpio pins

$ sudo usermod -a -G dialout,gpio username

And logout and login.