Python Code to set the sensor id

This was replaced with a more robust program written in C. See Page: Home Solar Project

This is some code hacked up to set the ID for the sensors. They start out at '1' so if you forget you'll have to walk it. One could clean this up to take parameters, but it's so one-shot it's hardly worth it. The sensors keep the ID once set, I don't see any way to reset it from hardware.

#!/usr/bin/env python3
#
# psensor-set.py
#
# I don't like python very much
#
import minimalmodbus
import serial

#----------------------------------------------------------------------
# PSENSOR_READ
#----------------------------------------------------------------------
def psensor_read(pwm,id):
    try:
        print("ID = {:d}\n".format(id))
        v = pwm.read_register(0,1,4)
        a1 = pwm.read_register(1,3,4)
        a2 = pwm.read_register(2,0,4)
        print ("v:{:f} a1:{:f} a2:{:f}".format(v,a1,a2))

    except IOError:
        print ("Read Failed\n")

#----------------------------------------------------------------------
# PSENSOR_WRITE
#----------------------------------------------------------------------
def psensor_write(pwm,id,new_id):
    try:
        print("ID = {:d} -> {:d}\n".format(id,new_id))
        v = pwm.read_register(0,1,4)
        a1 = pwm.read_register(1,3,4)
        a2 = pwm.read_register(2,0,4)
        print ("v:{:f} a1:{:f} a2:{:f}".format(v,a1,a2))
        pwm.write_register(2, new_id, 0, 6)

    except IOError:
        print ("Write Failed\n")

#----------------------------------------------------------------------
# PSENSOR_OPEN
#----------------------------------------------------------------------
def psensor_open(psd_id,full = False):
    psd = minimalmodbus.Instrument('/dev/ttyUSB0', psd_id, 'rtu', True)
    if(full):
        psd.serial.baudrate = 9600
        psd.serial.bytesize = 8
        psd.serial.parity = serial.PARITY_NONE
        psd.serial.stopbits = 1
        psd.serial.timeout = 0.1
    psd.mode = minimalmodbus.MODE_RTU
    print('Details of the meter are:')
    print(psd)
    return psd

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------

psensor1 = psensor_open(1,True)
psensor_read(psensor1,1)
#psensor_write(psensor1,1,11)

psensor11 = psensor_open(11,False)
psensor_read(psensor11,11)

psensor12 = psensor_open(12,False)
psensor_read(psensor12,12)
code/old_sensor_python.txt · Last modified: 2022-12-21 19:45 by keith
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0