Hilscher / node-red-contrib-s7comm

A Node-RED node to communicate with Siemens S7 PLCs.
MIT License
34 stars 11 forks source link

Dynamic ip address #12

Open Henkkas52 opened 2 years ago

Henkkas52 commented 2 years ago

Is it possible to configure the ip address dynamically, if so, how do I do that?

gb96 commented 1 year ago

If the PLC receives its ip address via DHCP or is otherwise dynamic, you can enter a hostname instead of the ip and it works.

Note that field validation will complain because the hostname won't match the expected IPv4 address format. Likewise an IPv6 address will also fail validation checks, but from my testing the PLC connection still succeeds.

Henkkas52 commented 1 year ago

@gb96 , thank you for reacting to my question. I think this could be a solution, but how and where do I declare the host name?

All communications with my PLC run well, but I also have an other small issue which you might have the answer to. When I start node-red (on my RPI 3b+) I get error about wrong OS, see the logging here

Starting as a systemd service. 27 Oct 13:12:48 - [info] Welcome to Node-RED

27 Oct 13:12:48 - [info] Node-RED version: v3.0.2 27 Oct 13:12:48 - [info] Node.js version: v16.17.1 27 Oct 13:12:48 - [info] Linux 5.10.103-v7+ arm LE 27 Oct 13:12:50 - [info] Loading palette nodes 27 Oct 13:12:53 - [s7comm-Error] - Installation of Module net-keepalive failed because we might be on the wrong OS. OS=linux 27 Oct 13:12:53 - [s7comm-Info] - Debug configuration for logLevelNodeS7:{"debug":0,"silent":true} 27 Oct 13:12:53 - [s7comm-Info] - Debug configuration for logLevelNodeRED:{"debug":2,"silent":true} 27 Oct 13:12:55 - [info] Dashboard version 3.2.0 started at /ui 27 Oct 13:12:56 - [info] Settings file : /home/pi/.node-red/settings.js 27 Oct 13:12:56 - [info] Context store : 'default' [module=localfilesystem] 27 Oct 13:12:56 - [info] User directory : /home/pi/.node-red 27 Oct 13:12:56 - [warn] Projects disabled : editorTheme.projects.enabled=false 27 Oct 13:12:56 - [warn] Flows file name not set. Generating name using hostname. 27 Oct 13:12:56 - [info] Flows file : /home/pi/.node-red/flows_pipi.json 27 Oct 13:12:57 - [info] Starting flows 27 Oct 13:12:59 - [info] Started flows

gb96 commented 1 year ago

If your network has a DNS service running with the correct hostname->address already available, you can just enter the hostname into the IP-Address field of the s7-comm config node within Node-RED. Validation fails and you get warnings when you click the Deploy button, however the s7 comm still works with the hostname instead of IP address.

Without DNS the simplest way to achieve this is to edit the system file /etc/hosts (or under most versions of Windows it is located at C:\Windows\System32\drivers\etc\hosts )

Content of the hosts file is plain text with one entry per line. Each entry has an address followed by one or more hostnames or aliases. The '#' character is for line-comments: the rest of a line following any # is ignored.

Example hosts file:

# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
192.168.0.4       plc        # a plc address with a hostname of plc

Regarding the Node-RED start-up error involving node-red-contrib-s7comm and failed installation of net-keepalive, this appears to be a common error. I also had this error but only after updating to use recent versions of nodejs and Node-RED. The error disappeared after I manually installed version 3.0.0 of net-keepalive via command-line ( npm i net-keepalive@3.0.0 ).

Henkkas52 commented 1 year ago

Regarding the Node-RED start-up error involving node-red-contrib-s7comm and failed installation of net-keepalive, this appears to be a common error. I also had this error but only after updating to use recent versions of nodejs and Node-RED. The error disappeared after I manually installed version 3.0.0 of net-keepalive via command-line ( npm i net-keepalive@3.0.0 ).``

Indeed this was the solution. So one small matter down.

Regarding the hostname you suggested, indeed this works, but Node-red keeps giving "red flags". So I don't think this is a pretty solution.

Isn't it possible some how (like with the modbus-node) to inject the ip-address with a message? Example:

msg.payload = { 'connectorType': 'TCP', 'tcpHost': ip_address, 'tcpPort': portnr, 'unitId': 1 }