Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modification of the EmonTCPModbusInterfacer to work with Pymodbus 1.4.0 #52

Merged
merged 8 commits into from
Dec 10, 2018

Conversation

alexandrecuer
Copy link
Contributor

Tested on a Modbus TCP master SOFREL S500

remove instruction with reference to the variable pymodbus_found
pymodbus_found was deleted on a previous commit : 
openenergymonitor@dc9be27#diff-25d902c24283ab8cfbac54dfa101ad31
With pymodbus 1.4.0 BinaryPayloadDecoder.fromRegisters does not accept argument 'endian' but arguments byteorder and wordorder
see http://pymodbus.readthedocs.io/en/v1.4.0/source/example/modbus_payload.html?highlight=BinaryPayloadDecoder.fromRegisters
@TrystanLea
Copy link
Member

Thanks @alexandrecuer, what do you think would be the best way to catch the case where pymodbus is not installed? I got the following error when testing your changes:

Traceback (most recent call last):
  File "src/emonhub.py", line 24, in <module>
    from interfacers import *
  File "/home/pi/emonhub/src/interfacers/EmonModbusTcpInterfacer.py", line 3, in <module>
    from pymodbus.constants import Endian
ImportError: No module named pymodbus.constants

If I move the imports to within init, this avoids the error on systems where an EmonModbusTcpInterfacer configuration is not present, but it might be better to do a bit more error catching than this.. any suggestions welcome

class EmonModbusTcpInterfacer(EmonHubInterfacer):

    def __init__(self, name, modbus_IP='192.168.1.10', modbus_port=0):
        """Initialize Interfacer
        com_port (string): path to COM port
        """

        from pymodbus.constants import Endian
        from pymodbus.payload import BinaryPayloadDecoder
        from pymodbus.client.sync import ModbusTcpClient as ModbusClient

@TrystanLea
Copy link
Member

PS: thankyou for removing the lines:

if I['Type'] in ('EmonModbusTcpInterfacer','EmonFroniusModbusTcpInterfacer') and not pymodbus_found
    self._log.error("Python module pymodbus not installed. unable to load modbus interfacer")

I missed these and have been trying to remove all module specific code from emonhub.py

@beaylott
Copy link
Contributor

beaylott commented Mar 6, 2018

We could test this with a Victron battery system and will be in a position to test this soon with a SolarEdge inverter. Let me know if needed.

@alexandrecuer
Copy link
Contributor Author

alexandrecuer commented Mar 6, 2018

@TrystanLea : Indeed I see.... sorry in the rush I forgot the case pymodbus was not present
Injection in the constructor as you propose is not working for me
I will try to find something working on both cases and come back to you

@beaylott : good if you can test - if you have pymodbus installed, it should work

@alexandrecuer
Copy link
Contributor Author

@TrystanLea : I've amended my pull request so that it is not producing anymore any error when pymodbus is not present...

@glynhudson
Copy link
Member

glynhudson commented Apr 24, 2018

Thansks! @TrystanLea can this be merged now? Sorry for the delay.

…for modbus devices

cf the following issue opened by pb66 in dec 2017 :
openenergymonitor#43

with the update :
- the user will not have to enter nReg, rType and datacode(s) but only
datacode(s) in the node section
- the user will not have to enter rNames and names but only names in the
node section

TODO :
reimplement the decode_string process (only used for debug purposes)
adapt the interfacers so it can encode topics with different nodes (one
node per unitId ? possibility to use the decode_string process to create
the nodename ?)
@alexandrecuer
Copy link
Contributor Author

@TrystanLea @glynhudson @pb66
I recoded the whole interfacer on order to simplify the configuration in emonhub.conf
cf the following issue opened by pb66 in dec 2017 :
#43

Configuration in emonhub.conf is realised as follow :

Sample interfacer config within emonhub.conf

 [[ModbusTCP]]     
        # this interfacer retrieves register information from modbusTCP clients 
        # retrieve register information from modbus TCP documentation for your inverter.
        Type = EmonModbusTcpInterfacer
        [[[init_settings]]]
            modbus_IP = 192.168.1.10   # ip address of client to retrieve data from
            modbus_port = 502          # Portclient listens on
        [[[runtimesettings]]]
            # List of starting registers for items listed above
            register = 40118,40092,40102,502,40285,40305,40284,40304,40086,40088,40090
            # nodeid used to match with node definition in nodes section below. Can be set to any integer value not previously used.
            nodeId = 12
            # Channel to publish data to should leave as ToEmonCMS
            pubchannels = ToEmonCMS,
            # time in seconds between checks, This is in addition to emonhub_interfacer.run() sleep time of .01
            # use this value to set the frequency of data retrieval from modbus client
            interval = 10 

Sample Node declaration

    [[12]]
        nodename = fronius
        [[[rx]]]
            # list of names of items being retrieved
            # This example retrieves the Inverter status, AC power in watts being produced, AC Lifetime KWh produced,
            # KWh produced for current day,....
            names = Inverter_status,AC_power_watts,AC_LifetimekWh,DayWh,mppt1,mppt2,Vmppt1,Vmppt2,PhVphA,PhVphB,PhVphC
            datacodes = H,f,f,Q,H,H,H,H,f,f,f
            scales = 1,0.1,0.1,1,1,1,1,1,0.1,0.1,0.1
            units = V,W,kWh,Wh,W,W,V,V,V,V,V

hope all that is compatible with emonhub policy of code....

regards
Alex

@glynhudson glynhudson merged commit 92c493c into openenergymonitor:emon-pi Dec 10, 2018
@glynhudson
Copy link
Member

Thanks, very sorry too so long to merge 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants