Skip to content

Receive and process messages from Kaifa smart electricity meter

Notifications You must be signed in to change notification settings

HWal/RPi_HAN_Receive_Web_Relay_Output

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MAIN README

HAN reader with relay control for Raspberry Pi 3 B+

Brief description

This project for the Raspberry Pi has the following functions:

  • Read and decode data from the HAN-port on Kaifa (MA304H3E 3-phase) smart electricity meter. It also reads data from Kaifa 1-phase meter, but this has not been tested.
  • Present live data from the meter on a traditional webpage, based on Apache2 webserver.
  • Control two output relays.
  • View and download el-spotprices for Norway/Bergen area in EUR and NOK currency.
  • Send email to a specified address when average of three Wh values within one month exceeds a limit set by the user.
  • Analyze log files on a Windows laptop.

Hardware

  • One Raspberry Pi 3 B+ with Raspbian (Buster) installed
  • 5V power supply and USB cable
  • TSS721 Module Board M-BUS To TTL converter (AliExpress)
  • Two 3.3V relay boards that will drain less than 12mA from RPi gpio output
  • Some connecting wire
  • A laptop, in my project one that is running Windows 10

Preparation of RPi while powered up and connectected directly to a screen (TV)

Activate VNC: sudo raspi-config -> Interfacing Options -> VNC -> Enable VNC server
Activate SSH: sudo raspi-config -> Interfacing Options -> SSH -> Enable SSH server

Open a terminal window, note the IP number of the RPi returned from the command: hostname -I

Prepare a laptop for remote control of the RPi

Download VNC viewer: https://www.realvnc.com/en/connect/download/viewer/
Download putty.exe and pscp.exe: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
You should now be able to control RPi from the laptop on your local network, both via the GUI (VNC viewer) and terminal window (putty.exe). Also you can copy files between RPi and the laptop with pscp.exe. To connect, use the IP number noted above.

From now on a separate screen should rarely be necessary. The RPi will be working in "Headless mode".

Continue to prepare RPi in Headless mode

Preferably, give RPi a static IP address, see: https://raspberrypi.stackexchange.com/questions/37920/how-do-i-set-up-networking-wifi-static-ip-address/74428#74428

Install Apache2 webserver: sudo apt-get update , sudo apt-get upgrade , sudo apt-get install apache2 -y
Make user pi the owner of /var/www/html: sudo chown pi: -R html
Install PHP for the relay control script: sudo apt-get install php libapache2-mod-php -y

Make a "clean" UART port on the RPi 3. For reference, see: https://www.raspberrypi.org/documentation/configuration/uart.md and https://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3-4/
We want these pins to communicate with the M-BUS To TTL converter:
Physical pin 8 = gpio 14 = Tx
Physical pin 10 = gpio 15 = Rx

sudo apt-get update , sudo apt-get upgrade

Disable console and activate serial: sudo raspi-config -> Interfacing Options -> Serial
Login shell accessible over serial: No
Serial port hardware enabled: Yes
The system asks for a reboot: Yes

We can stop and deactivate the getty service, as long as the console is not used:
sudo systemctl stop [email protected]
sudo systemctl disable [email protected]

We don't need to use the Bluetooth modem at this time.
sudo systemctl disable hciuart // Disables the Bluetooth modem

We want to swap the serial ports, please see the relevant lines in the file /boot/overlays/README.
sudo nano /boot/config.txt, and add at the bottom: dtoverlay=pi3-disable-bt
sudo reboot

Now ttyAMA0 / PL011 / UART0 is connected to gpio 14 / 15 which are the physical pins 8 / 10

Make some directories on the Raspberry Pi (Command - Owner - Group - View - Change - Access):
mkdir /home/pi/Cpp_AMS - pi - pi - Anyone - Only owner - Anyone
mkdir /home/pi/Python_AMS - pi - pi - Anyone - Only owner - Anyone
mkdir /var/meter_log - root - root - Anyone - Anyone - Anyone
mkdir /var/www/html/data - pi - pi - Anyone - Only owner - Anyone
mkdir /var/www/html/img - pi - pi - Anyone - Only owner - Anyone
mkdir /media/pi/xxxxxxxxx/meter - pi - pi - Anyone - Only owner - Anyone (for usb stick)
mkdir /media/pi/ABCDEFGHI/prices - pi - pi - Anyone - Only owner - Anyone

Permissions and ownership shown above is like I have it on my RPi. Data security has not been considered so far.

Now copy the files from github into the corresponding directories on RPi listed above.

Edit a cronjob as your pi user, with crontab -e, with the following content:

05 00 * * * /usr/bin/python3 /home/pi/Python_AMS/copyprices_1.py
10 00 * * * /home/pi/Cpp_AMS/copyFiles_meter
00 15 * * * /usr/bin/python3 /home/pi/Python_AMS/spotprices.py
55 23 * * * /usr/bin/python3 /home/pi/Python_AMS/copyprices_2.py

Goto folder Cpp_AMS and compile the source code with: g++ -W readAMSxx.cpp. Then start reading messages by typing: ./a.out

NOTE: After upgrading from RPi Operating system stretch to buster I experienced problems while reading the meter. readAMS66.cpp has now been adapted to the newer buster release, and latest version is now readAMS77.cpp. In case of problems it is suggested that you try both program versions to determine what works best for you.

Goto folder Python_AMS and start the notification app with: python3 maxpowermonitor.py

When you see the two programs working in their respective terminal windows, you may open the website. From the laptop, when connected to the same local network, open the browser and enter the IP address of the Raspberry pi. The website is very simple, and should be self explanatory. Test that values are being updated in the "View current data from the meter" page.

Note: I have a water meter as well, which has an entry in the webpage. Details about this is currently not covered in this project.

For presentation of log data on laptop, please see the Readme file in the python folder.

To connect to the website from the outside world, you should open port 80 in your firewall. Beware the risk of getting the RPi hacked by outside users. You should therefore password protect the website. Information about how to do this is found on the internet.

LIST OF FILES
/home/pi/Cpp_AMS/readAMS66.cpp C++ source code
/home/pi/Cpp_AMS/readAMS77.cpp C++ source code
/home/pi/Cpp_AMS/copyFiles_meter bash script
/home/pi/Cpp_AMS/a.out executable, reads meter
/home/pi/Python_AMS/copyprices_1.py
/home/pi/Python_AMS/copyprices_1.py
/home/pi/Python_AMS/spotprices.py
/home/pi/Python_AMS/maxpowermonitor.py
/var/www/html/currtime.php
/var/www/html/gpio.php
/var/www/html/notificationlimit.php
/var/www/html/schematic.html
/var/www/html/schematic.jpg
/var/www/html/spotprices_EUR_today.php
/var/www/html/spotprices_EUR_today.html
/var/www/html/spotprices_EUR_tomorrow.php
/var/www/html/spotprices_EUR_tomorrow.html
/var/www/html/spotprices_NOK_today.php
/var/www/html/spotprices_NOK_today.html
/var/www/html/spotprices_NOK_tomorrow.php
/var/www/html/spotprices_NOK_tomorrow.html
/var/www/html/relaycontrol.php
/var/www/html/threemaxes.php
/var/www/html/threemaxes.html
/var/www/html/amsdata.html
/var/www/html/currpower.php
/var/www/html/currlog.php
/var/www/html/index.html
/var/www/html/img/green_0.jpg
/var/www/html/img/green_1.jpg
/var/www/html/img/red_0.jpg
/var/www/html/img/red_1.jpg
/var/www/html/data/notificationlimit.data
/var/www/html/data/threemaxes.data
/var/www/html/data/prices_EUR_today.data
/var/www/html/data/prices_NOK_today.data
/var/www/html/data/prices_EUR_tomorrow.xml
/var/www/html/data/prices_EUR_tomorrow.data
/var/www/html/data/currencyconversions.xml
/var/www/html/data/prices_NOK_tomorrow.data
/var/www/html/data/currentlog.data
/var/www/html/data/currentactivepower.data
/var/www/html/data/currenttime.data
/var/meter_log/20xx-yy-zz.txt Prepare meter log files for permanent storage on usb stick
/media/pi/D8AF-261F/meter/20xx-yy-zz.txt
/media/pi/D8AF-261F/prices/20xxyyzz_EUR.data
/media/pi/D8AF-261F/prices/20xxyyzz_NOK.data

About

Receive and process messages from Kaifa smart electricity meter

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published