Community
OpenEnergyMonitor

Community

Autometers DDSD285 Smart Meter Modbus RS485 interfacing and monitoring

smartmeter
modbus
rs485
Tags: #<Tag:0x00007f13f36c3348> #<Tag:0x00007f13f36c3208> #<Tag:0x00007f13f36c30c8>

(Anthony Smith) #61

Genious!!!

Code now looks like this:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import threading, time, subprocess, logging, minimalmodbus

instrument = minimalmodbus.Instrument("/dev/ttyUSB0", 1)
instrument.serial.baudrate = 9600
instrument.serial.parity = minimalmodbus.serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout  = .5
instrument.debug = False
delay = .02
#print instrument

TOTKWH  = round(instrument.read_float((32001-30001),4),4) # Total Energy KWHs
time.sleep (delay)
#Address Register  Length Parameter Name Access Data Format   Units
# (hex)  (decimal) (bytes) (string)       (R/W) (Float,16bit) A,V,KW,KWh,Hz
# 0x0010   30017      4   Voltage L1        R      Float        V
# 0x004E   30079      4   Frequency         R      Float        Hz
VOLTAGE = round(instrument.read_float((30017-30001),4),4)
time.sleep (delay)

FREQUENCY = round(instrument.read_float((30079-30001),4),4)
time.sleep (delay)

#Address Register  Length Parameter Name Access Data Format   Units
# (hex)  (decimal) (bytes) (string)       (R/W) (Float,16bit) A,V,KW,KWh,Hz
# 0x0058 30089        4   Current Total     R       Float       A
AMPS = round(instrument.read_float((30089-30001),4),4) 
time.sleep (delay)

#Address Register  Length Parameter Name Access Data Format   Units
# (hex)  (decimal) (bytes) (string)       (R/W) (Float,16bit) A,V,KW,KWh,Hz
# 0x0090   30145      4   Power L1          R       Float       KW
POWER = (round(instrument.read_float((30145-30001),4),4)*1000)
time.sleep (delay)
#BAUDRATE = instrument.read_register((31318-30001),0,4)
# SETBAUD = instrument.write_register((31318-30001),38400,0,6)
#print BAUDRATE

print('Volts  Amps Frequency Hz Power Load Watts Total KWHs\n{} {} {}        {}             {} \n'.format(VOLTA$
#print time.strftime('%Y%m%d %X ')
file = open('/home/pi/DDSD285/meter_data.txt', 'w+')
file.write(time.strftime('%Y%m%d %X '))
file.write('{} {} {} {} {}\n'.format(VOLTAGE,AMPS,FREQUENCY,POWER,TOTKWH))
file.close()

subprocess.call('/home/pi/DDSD285/send-to-emoncms.sh') # call the bash file to transmit the data

exit()

It seems to work every time now. So its a timing thing and I guess heuristic to find the right value.

Thanks again.

Now attempting to transfer this install to the new Rpi Zero W but it wont boot the SD card at present.

Cheers
Tony


(Anthony Smith) #62

Hello,

Struggling a bot with the schedule part of this. If I run it from the command line all is well it runs and updates data to the central emoncms sever every 10 seconds.

If I run it from a systemd service then it fails as below.

sudo systemctl status ddsd285.service
● ddsd285.service - DDSD285 Meter Reading Service
   Loaded: loaded (/lib/systemd/system/ddsd285.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2019-02-16 19:51:59 GMT; 5s ago
  Process: 3357 ExecStart=/usr/bin/python /home/pi/DDSD285/ddsd285-test1.py > /home/pi/DDSD285/ddsd285.log 2>&1 
 Main PID: 3357 (code=exited, status=1/FAILURE)

Feb 16 19:51:59 raspberrypi systemd[1]: Started DDSD285 Meter Reading Service.
Feb 16 19:51:59 raspberrypi python[3357]: Traceback (most recent call last):
Feb 16 19:51:59 raspberrypi python[3357]:   File "/home/pi/DDSD285/ddsd285-test1.py", line 4, in <module>
Feb 16 19:51:59 raspberrypi python[3357]:     import threading, time, subprocess, logging, minimalmodbus
Feb 16 19:51:59 raspberrypi python[3357]: ImportError: No module named minimalmodbus
Feb 16 19:51:59 raspberrypi systemd[1]: ddsd285.service: Main process exited, code=exited, status=1/FAILURE
Feb 16 19:51:59 raspberrypi systemd[1]: ddsd285.service: Unit entered failed state.
Feb 16 19:51:59 raspberrypi systemd[1]: ddsd285.service: Failed with result 'exit-code'.

Systemd service wrapper is below:

[Unit]
 Description=DDSD285 Meter Reading Service
 After=multi-user.target

 [Service]
 Type=idle
 ExecStart=/usr/bin/python2 /home/pi/DDSD285/ddsd285-test1.py > /home/pi/DDSD285/ddsd285.log 2>&1

 [Install]
 WantedBy=multi-user.target

I am sure it must be something to do with paths etc.

Any help appreciated.

Cheers
Tony


(Bill Thomson) #63

Python thinks minimalmodbus isn’t installed.

Silly question: did you install minimalmodbus? i.e. sudo pip install minimalmodbus


(Anthony Smith) #64

Yes that’s exactly how I installed it.

It is clearly installed because if I run the program from the command line all is well.

Cheers
Tony.


(Bill Thomson) #65

Sounds like this may be of some help:


(Anthony Smith) #66

Cheer Bill. I will have a look through it.

had a look. Perfect answer and problem solved in about 30 seconds.NOte to self must improve google-fu skills :slight_smile:

Add this property to [Service] section to make sure systemd run as the specified user.

User=pi

Results in all being well after reloading and restarting the service:

sudo systemctl daemon-reload
[email protected]:~ $ sudo systemctl start ddsd285.service
[email protected]:~ $ sudo systemctl status ddsd285.service
● ddsd285.service - DDSD285 Meter Reading Service
   Loaded: loaded (/lib/systemd/system/ddsd285.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-02-16 23:38:12 GMT; 7s ago
 Main PID: 3996 (python2)
   CGroup: /system.slice/ddsd285.service
           └─3996 /usr/bin/python2 /home/pi/DDSD285/ddsd285-test1.py > /home/pi/DDSD285/ddsd285.log 2>&1

Feb 16 23:38:12 raspberrypi systemd[1]: Started DDSD285 Meter Reading Service.

Tony