DHCP address on 2152
On the 2152 controller we have made an MQTT client which connects to an online broker.
Now this PLC is placed in a random network of a customer where we have no information about this network and IP range.
Because there are multiple PLC`s, and different customers, we cannot set an fixed IP address per project.
I read that it should be possible to set the PLC to get it`s local IP address via DHCP?
There is an option to configure a start and end IP address, but even then it must be in the same range. (not possible to set it to 0.0.0.0 to 255.255.255.255
I also don't see this DHCP option in my PLCnext engineer version.
PLCnext engineer 2023.0.1, CPU is a 2152 with FW 2023.0.0
How can i configure this?
Comments
Hello!
I think, that one of the ways of provide this task is related to general linux administration proposes.
Content of file:
#!/bin/sh
ip addr add 192.168.1.10/24 dev eth0
This steps will provide both automatic dhcp settings and additional static point to eth0 of your plc.
Best regards
Important notice!
Profinet device and Profinet controller system services mast be deactivated to prevent fatal errors during startup, that cause stop of MainProcess.
Best regards
It's also possible to make this setting from PLCnext Engineer:
Note that the settings in the "Online Parameters" tab are not transferred to the device with the PLCnext Engineer project, but are only transferred to the device when the "Write" button is pressed (the little gear wheel with a pencil).
I will suggest another alternative that I've found success with. Starting with FW2023.0, I had issues with the methods suggested by @Oleksandr Guminskiy and @Martin PLCnext Team. The gateway address acquired through DHCP was not acquired correctly. The method I use now is to call a command to the linux shell using the PBCL_SysLinuxShell from the PLCnextBase library.
Using the command "sudo dhcpcd <adapter name>" - replace <adapter name> with eth0 for AXC F 2152 or lan1 for AXC F 3152. This will acquire a second virtual IP address for the network interface, leaving the original static IP definition intact. This does require modifying the sudoers file to add permissions for the plcnext_firmware user. I add the line: "plcnext_firmware ALL=(ALL) NOPASSWD: /sbin/dhcpcd".
This may not be the simplest or supported method, but the advantage I found is I can call this command again to refresh the DHCP lease without rebooting.
Nice and flexible suggestion in various cases.
Hi @Oleksandr Guminskiy
Thank you, it seems that fw version 2023.0.0 does not accept the dhcp setting by plc next engineer.
I have changed dhcp in /etc/network/interfaces as you described.
if I understand correctly i need to create a file named 'add_ip.sh' in the folder /etc/network/if-up.d
With content of file:
-----------------------------------------------------------------------
#!/bin/sh
ip addr add 192.168.1.10/24 dev eth0
-----------------------------------------------------------------------
But what do you mean by:
Do i need to excecute this command from root user or set this command somewhere?
Hi @Rob H
Thank you,
I`m not fammiliar with the commands, what do you mean by:
This does require modifying the sudoers file to add permissions for the plcnext_firmware user. I add the line: "plcnext_firmware ALL=(ALL) NOPASSWD: /sbin/dhcpcd".
Can you give me some more information?
Hello!
Script placed at /etc/network/if-up.d will execute automatically during system startup, when eth0 successfully started.
And yes, you have to make this operation under root acc, otherwise system will decline commands.
Command "chmod +x /etc/network/if-up.d/add_ip.sh" will set special bit of file, that will allow it's execution as script file.
Hi @Martin PLCnext Team ,
Unfortunally this function didn`t work.
I have dis-abled the profinet in WBM, and set DHCP by PLCnext Engineer, online parameters and then the write button.
When PLC is restarted, again the PLC remain the static IP.
Linux commands executed via the PBCL_SysLinuxShell from the PLCnextBase library are executed with the plcnext_firmware user. To execute the dhcpcd command, the plcnext_firmware user needs rights to execute as root without providing a password. Doing so requires adding a rule to the sudoers. For help on adding users to sudoers, I am not the expert. There are resources out there for that. A short description of what worked for me:
SSH as root user. Use command
sudo visudo /etc/sudoers.d/axcf2152-sudoers
to begin editing the sudoers file. Add lineplcnext_firmware ALL=(ALL) NOPASSWD: /sbin/dhcpcd
Then after a reboot, dhcpcd should be able to be executed from the PBCL_SysLinuxShell function block.Hi Rob
I'm trying it your way but I'm a litlle stuck. I opened the sudoers file ( see image), but I don't know how to edit/save it.
Can you help me out?
The default editor for visudo is vim so you can certainly find more help on using the editor on the web. To help with your immediate problem:
To save and exit
:wq
To close without saving:
:q!
Thanks I edited the file and rebooted the controller. A few questions:
Do I have to turn off the profinet controller option in WBM?
How do i know this works/ know the dynamic IP?
Does the execute for the function block need to stay true?
And the FB gives following result:
Thanks in advance!