Interfacing with Logo 8 using Modbus TCP

I am currently working on a project related with smart home and for my use-case I have chosen Siemens Logo 8 controller as a main brain device. One thing that I like very much about this Logo generation over previous, that it comes with integrated Modbus TCP functionality. This is very useful, because instead of interfacing with device using digital inputs and outputs I am able to transfer flag and numeric values through Modbus TCP connection to any device which is supporting this protocol. This helps building custom HMI with specific functionality or expand with custom peripheral devices or connecting industrial equipment through Modbus TCP.

My setup consists of Logo 8 controller together with 24V power source. Using Ethernet cable it is connected to home TP 10/100 switch which is linking Raspberry Pi LCD and my laptop.

Schematic view of my setup

For Logo I have added 7 input buttons and one analogue input which is connected to variable resistor, for now all relay outputs are not connected to anything, but their operation can be spotted by sound which is made when contact is made and that is enough for me at this point.

To interface with Logo device I am using Python library uModbus that creates connection and is communicating values between devices. With Python I am changing or reading specified memory section which is reserved on Logo, all memory mapping information can be found in Siemens Logo datasheet.

Logo! Soft Comfort program example

In Logo program above I have added digital Input M1 which is changed from Modbus TCP client (in this case Raspberry Pi) and then turning on/off the relay. Modbus TCP packets can be transferred ower ordinary Ethernet network.

Reading analogue value in Logo! Soft Comfort

Following code is taken from uModbus description page example code and adjusted for my setup. In example above I am reading analogue value from my A1 (analogue input 1), when variable resistor is turned, then changed value is reflected on Python console screen. Below Python code example.

Reading analogue values code example

import socket
from umodbus import conf
from umodbus.client import tcp
from time import sleep as wait

# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True

### Creating connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.3', 502))

### Reading Analogue value from associated Input Register
message = tcp.read_input_registers(slave_id=1, starting_address=0, quantity=1)

### Printing analogue values in Python console
for i in range(1, 100000):
    response = tcp.send_message(message, sock)
    print(response, i)
    wait(0.01)

sock.close()
print("Transfer ended!")

Setting Logo coil value code example

import socket
from umodbus import conf
from umodbus.client import tcp

# Enable values to be signed (default is False).
conf.SIGNED_VALUES = True

### Creating connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.3', 502))

message = tcp.write_multiple_coils(slave_id=1, starting_address=8256, values=[1, 0, 0, 0])

# Response depends on Modbus function code. This particular returns the
# amount of coils written, in this case it is.
response = tcp.send_message(message, sock)
print(response)
sock.close()
print("Transfer finished")

In conclusion – Modbus TCP functionality in Logo controller is very powerful thing, that expands Siemens Logo controller integration with different devices that are supporting this interface. It gives you more flexibility in choosing peripherals and one can develop own HMI solutions.

One thing that I have not added to this stage of the project was VPN connectivity to this workbench. For that I am missing router that is supporting PPTP connectivity. PPTP client connection would enable controlling this setup remotely from any part of the world, this enables monitoring different statuses of logs, capturing and storing different analogue and flag values in database.

For easier Python code transfer to Raspberry Pi I suggest setting shareable folder on Raspberry Pi that can be accessed on devices on local network.

Source