Another “Swiss Army Knife” for PLC Networks : Raspberry Pi 3

network

Say bye bye to the laptop.  It is annoying when we need to troubleshoot or to replace Allen Bradley ControlLogix CPUs or modules  and we don’t know what  the model and revision is. Another case is when we have to do verification on Endress Hauser Promag 400 or other Field devices  and we forget where the laptop is or  with some mysterious reasons, it is not available at that moment. 

Thanks to Raspberry Pi 3 , now we can turn it into a WIFI router which can be accessed with our regular cellphones. To do this what we need is a Raspberry Pi 3,an ethernet cable to connect it to the network, a cellphone with browser capability and average Linux skills.

First, We need to identify the gateway IP address, and write it down. Secondly find a static IP address for Raspberry Pi 3  which is not used on our PLC network. Let says that the gateway IP number  is 192.168.1.1 and the unused static IP number is 192.168.1.50.  Keep these numbers for configuration and verification later.

Keep in the mind that the following procedure is for Raspberry Pi 3 and I am sure it might work for some  older  versions with slightly modification. I got the script from several sources online and I did modification to work on PLC network which use static IP addresses.

1.Install hostapd, udhcpd and iptables

Install the necessary tools
 
  1. sudo apt-get update
  2. sudo apt-get install hostapd udhcpd -y
  3. sudo apt-get install iptables -y

 

2. Create back up for these files, in case of things go wrong.

backup
 
  1. sudo touch /etc/network/interfaces.bak
  2. sudo cp /etc/network/interfaces /etc/network/interfaces.bak
  3. sudo touch /etc/dhcpcd.bak
  4. sudo cp /etc/dhcpcd.conf /etc/dhcpcd.bak

 

3.Build a following  script (mywifi.sh).

Basically this script is to create several configuration (conf) files and will load them into default ones. DHCP (Dynamic Host Configuration Protocol)  configuration will give away the dynamic  IP addresses between 192.168.42.2 and 192.168.42.20 to your phones or other devices to access wlan0 on Raspberry pi 3 (raspi3)  meanwhile give a static IP address for raspi3 192.168.42.1  . For older version , it is necessary to setup configuration on /etc/network/interfaces for interfacing with network (wlan0,eth0, etc )  but on new  Raspi3, the network configuration located on /etc/dhcpcd.conf . HostAPD configuration will create hotspot and APN ( Access Point Name)  so when all things works you should see “WifiPLC” on your phone WIFI settings. The next is NAT (Network Address Translation ) which works as a translator between external router that connect to eth0 and wifi guests who access  wlan0.    :

WIFIhot
 
  1. #! /bin/bash
  2.  #1. Configure DHCP----------------------------------------------
  3. x=tem.tem
  4. touch $x
  5. sudo rm -rf /etc/default/udhcpd
  6. #sudo mkdir /etc/default
  7. sudo touch /etc/default/udhcpd
  8. echo "start 192.168.42.2 " >>   $x
  9. echo "end 192.168.42.20" >> $x
  10. echo "interface wlan0" >> $x
  11. echo "remaining yes" >> $x
  12. echo "opt dns 8.8.8.8 4.2.2.2" >> $x
  13. echo "opt subnet 255.255.255.0" >> $x
  14. echo "opt router 192.168.42.1" >> $x
  15. echo "opt lease 864000" >> $x
  16. sudo mv  $x /etc/udhcpd.conf
  17. touch $x
  18. echo  "# Comment the following line to enable" >> $x
  19. echo "#DHCPD_ENABLED=\"no\"" >> $x
  20. echo "# Options to pass to busybo>> $x
  21. echo "DHCPD_OPTS=\"-S\"" >> $x
  22. sudo mv $x  /etc/default/udhcpd
  23.         #give the Pi a static IP address
  24. sudo ifconfig wlan0 192.168.42.1
  25. #2.------------------------------SETUP AP (Access Point) Interface -------------------------------
  26. touch  $x
  27. echo "source-directory /etc/network/interfaces.d" >> $x
  28. echo "auto lo" >> $x
  29. echo "iface lo inet loopback" >> $x
  30. echo "" >> $x
  31. echo "auto eth0" >> $x
  32. echo "iface eth0 inet dhcp" >> $x
  33. echo "" >> $x
  34. echo "iface wlan0 inet static" >> $x
  35. echo "    address 192.168.42.1" >> $x
  36. echo "    netmask 255.255.255.0" >> $x
  37. echo "    wireless-power off" >> $x
  38. echo "" >> $x
  39. echo "iface default inet dhcp" >> $x
  40. echo "up iptables-restore < /etc/iptables.ipv4.nat" >> $x
  41. sudo chmod --reference=/etc/network/interfaces $x
  42. sudo chown --reference=/etc/network/interfaces $x
  43. sudo mv $x  /etc/network/interfaces.ap
  44. sudo chmod --reference /etc/network/interfaces.bk /etc/network/interfaces.ap
  45. #3. Configure HostAPD------------------------------------------------
  46. touch $x
  47. echo "interface=wlan0" >> $x
  48. echo "driver=nl80211" >> $x
  49. echo "ssid=WifiPLC" >> $x
  50. echo "hw_mode=g" >> $x
  51. echo "channel=6" >> $x
  52. echo "macaddr_acl=0" >> $x
  53. echo "auth_algs=1" >> $x
  54. echo "ignore_broadcast_ssid=0" >> $x
  55. echo  "wpa=0" >> $x
  56. echo "wpa_passphrase=My_Passphrase" >> $x
  57. echo "wpa_key_mgmt=WPA-PSK" >> $x
  58. echo "wpa_pairwise=TKIP" >> $x
  59. echo "rsn_pairwise=CCMP" >> $x
  60. echo "ieee80211n=1"   >> $x
  61. echo "wmm_enabled=1" >> $x
  62. echo "ht_capab=[HT40][SHORT-GI-20][DSSS_CCK_40]" >> $x
  63. sudo mv  $x  /etc/hostapd/hostapd.conf
  64. #4. Configure NAT--------------------------------------------
  65. touch $x
  66. echo "DAEMON_CONF=\"/etc/hostapd/hostapd.conf\"" >> $x
  67. sudo mv $x /etc/default/hostapd
  68. touch $x
  69. sudo sh -c "echo 1 >> /proc/sys/net/ipv4/ip_forward"
  70. sudo echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
  71. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  72. sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  73. sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
  74. sudo sh -c "iptables-save >> /etc/iptables.ipv4.nat"
  75. #------------------------------5. Fire it up! R----------------------------------------
  76. sudo service hostapd start
  77. sudo service udhcpd start
  78. #-----------------------------6.get the hotspot to start on boot----------------------
  79. sudo update-rc.d hostapd enable
  80. sudo update-rc.d udhcpd enable
  81. #------------------------Create wifiConnect.py------------------------------------
  82. sudo apt-get install dnsmasq -y
  83. sudo service dnsmasq start
  84. sudo update-rc.d dnsmasq enable
  85. sudo apt-get install udhcpc -y

 

4.Create AP.sh script

AP.sh
 
  1. sudo nano AP.sh
ap.sh script
 
  1. #! /bin/bash
  2. #sudo cp  /etc/init.d/hostapd.bak /etc/init.d/hostapd
  3. #sudo cp /etc/init.d/udhcpd.bak /etc/init.d/udhcpd
  4. sudo /etc/init.d/hostapd stop
  5. sudo /etc/init.d/udhcpd stop
  6. sudo ifdown wlan0
  7. sudo ifconfig wlan0 down
  8. sudo rm -rf /etc/network/interfaces
  9. sudo cp /etc/network/interfaces.ap /etc/network/interfaces
  10. sudo ifconfig wlan0 up
  11. sudo ifup wlan0
  12. sudo service hostapd start
  13. sudo service udhcpd start
  14. sudo service hostapd status

 

Run mywifi.sh and AP.sh

run wifi and AP
 
  1. sudo ./mywifi.sh
  2. sudo ./AP.sh

 

If there is no errors , you should able to see WifiPLC on your phone. Login and when ask password, type “My_Passphrase” and it will save and connect it to your phone. To test it, connect eth0 (ethernet port) on Raspi3 to DHCP router that connect to Internet. The router will assign a dynamic IP address . Now you are able to connect to Internet. Type “www.google.com” on your phone browser to verify it.

The last step. Eth0  still has a dynamic IP address and will not communicate with PLC static IP address network. to get a static IP address add the following to the very bottom of /etc/dhcpcd.conf file. Remember, you already wrote the gateway IP address and unused IP address on the network.

dhcpcd.conf
 
  1. # static IP setup
  2. interface eth0
  3. static ip_address=192.168.1.50/24
  4. static routers=192.168.1.1
  5. static domain_name_servers=192.168.1.1

 

Save it as  /etc/dhcpcd.conf.

Reboot your Raspi3, connect  the ethernet port to PLC switch or router. Now if you have CPUs  or ethernet  modules  which have  “192.168.1.xx ” , type those number on your browser. Let says “192.168.1.33”. Most new Field devices or PLCs  have HTTP protocol capability on port 80/8080, so you will  see something like this  :

diag

You can find MAC number using “arp’ command. Also you might install “nmap” utility which can do many things in the network such as scanning particular IP address block, checking open/closed ports etc.

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
*
*