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