EMH NXT4 smart meter via smart meter binding, python solution

Hi,
anoybody succesfull atached EMH NXT4 smart meter via smart meter binding 2.5.5 ?
Installed openhab 2.5.5 on RPi4 Buster 4GB

emh.items
Number:Energy EnergyPlus “Energy+ [%.2f kWh]” { channel=“smartmeter:meter:emh:1-0_1-8-0” }
Number:Energy EnergyMinus “Energy- [%.2f kWh]” { channel=“smartmeter:meter:emh:1-0_2-8-0” }

emh.things
smartmeter:meter:emh [port=“/dev/ttyUSB1”, baudrateChangeDelay=250, refresh=180, mode=“ABC”]
Channels:
Type 1-0:1.8.0 : 1-0_1-8-0 #positive energy
Type 1-0:2.8.0 : 1-0_2-8-0 #negative energy
}

I’m using ttyUSB1 RS485-USB converter.
With python script I can read out answer from NXT4. 300Bd, 7E1,
init string: /?!<0D><0A>

Via Python script I can read out all values:

*!/usr/bin/python
import serial
import sys
import time

  • configure the serial port
    port = serial.Serial(
    port=‘/dev/ttyUSB1’,
    baudrate=300,
    parity=serial.PARITY_EVEN,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.SEVENBITS,
    timeout=2,
    xonxoff=False,
    rtscts=False,
    dsrdtr=False
    )

  • Send /?!<0D><0A>
    str0 = b’\x2f\x3f\x21\x0d\x0a’

  • write opening string to serial port
    port.write(str0)

  • loop readline all other data from port (but only with 300Bd !!!)
    for x in range(1, 119):
    str = port.readline()
    print(str)

port.close()

/dev/ttyUSB0 is used to communicate with APC UPS device.
I have also modified /etc/default/openhab2
EXTRA_JAVA_OPTS=“-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB0:/dev/ttyUSB1”
and disabled bluetooth and ttyAMA0
User openhab is in gropus tty and dialout

The problem is that the loop answers are readout correctly but only with 300Bd.
I would prefer to change the speed to 9600Bd for the rest of data.

Via Windows program, transfer beginns with 300Bd 7E1 for opening string
and proceeds next datasets with 9600Bd 7E1.

lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC
Bus 001 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub ### /dev/ttyUSB0 APCUSBD
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

python -m serial.tools.list_ports -v
/dev/ttyUSB0
desc: US232R # SUA1000 APCUPSD
hwid: USB VID:PID=0403:6001 SER=FTGVERLS LOCATION=1-1.2
/dev/ttyUSB1
desc: FT232R USB UART #EMH smart meter
hwid: USB VID:PID=0403:6001 SER=AC02545Z LOCATION=1-1.3
2 ports found

Thanks for help.

Jozef

My solution:

/!/usr/bin/python

import serial
import sys
import time
from openhab import openHAB

base_url = ‘http://192.168.x.y:8080/rest
openhab = openHAB(base_url)

/ configure the serial port (ttyUSB0 777 chown openhab group openhab)
ser = serial.Serial(
port=’/dev/ttyUSBxy’,
baudrate=300,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS,
timeout=2,
xonxoff=False,
rtscts=False,
dsrdtr=False
)

/ Send /?!<0D><0A>
ser.write(b’\x2f\x3f\x21\x0d\x0a’)
/ ser.write(b’/?!\r\n’)
/ Get answer 300Bd EMH5\@\201NXT4Y001\r\n
str = ser.readline()
/ print(str)

/ Request to transfer with 300-9600Bd
/ 300 Bd
/ ser.write(b’\x06\x30\x30\x30\x0d\x0a’)

/ 600 Bd
/ ser.write(b’\x06\x30\x31\x30\x0d\x0a’)

/ 1200 Bd
/ ser.write(b’\x06\x30\x32\x30\x0d\x0a’)

/ 2400 Bd
/ ser.write(b’\x06\x30\x33\x30\x0d\x0a’)

/ 4800 Bd
/ser.write(b’\x06\x30\x34\x30\x0d\x0a’)

/ 9600 Bd
ser.write(b’\x06\x30\x35\x30\x0d\x0a’)

time.sleep (0.3)
/ set 300, 600, 1200, 2400, 4800, or 9600
ser.baudrate = 9600
time.sleep (0.3)

/ Complete answer to one string
list = ser.readlines()
ser.close()

/ Format answer substrings
str180 = list[22].decode(encoding=‘UTF-8’)
str280 = list[46].decode(encoding=‘UTF-8’)
s180 = int((str180[6:14]).lstrip(“0”))
s280 = int((str280[6:14]).lstrip(“0”))

/ fetch all items
/ items = openhab.fetch_all_items()
/ fetch a single item

s1 = openhab.get_item(‘SumConsumption’)
s1.state=s180

s2 = openhab.get_item(‘SumOverage’)
s2.state=s280