Using Raspberry Pi3 with Duet 2 Ethernet
-
To anyone trying to do a similar setup, i got it working. When following the link that @DocTrucker provided, the command "iptables -A PREROUTING -t nat -i <ethernet_interface> -p tcp --dport 80 -j DNAT --to-destination <duet_ip>:80" is in addition to "sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE" and comes after. I have a paneldue connected and it told me the IP address that was being provided so I used that. All is working now!
Big thanks to @DocTrucker and @incogizmo !! Stay tuned for a post in a few weeks when I share my Voron 2.1
-
@nikker yes you were right both commands are needed. awesome work glad to hear
Also just looking over your previous post, it would be best to set a static IP on your Duet, otherwise there is a possibility DHCP will eventually provide your duet a different IP address. If this happens your port forwarding will break.
-
So after some more tinkering, here are the steps used to use the Raspberry Pi 3's Wifi to connect a Duet 2 Ethernet to a network.
-
Start with 2017-07-05-raspbian-jessie-lite (I read that with stretch it may not work or be harder... I used what was recommended here https://www.raspberrypi.org/forums/viewtopic.php?t=132674
-
Get Wifi setup by placing a template wpa_supplicant.conf on the /boot as well as an empty file called "ssh" to enable the SSH server for headless setup (Details on how to do this in the link above).
-
I use Bitvise SSH to connect to the RPI3.
-
I opted to use a spare Alfa AWUS052NH high power diversity USB WiFi since I have two of them laying around. This creates a wlan0 and wlan1 but sometimes they get switched around. To keep each device with the same wlanX name I used the instruction on these link https://raspberrypi.stackexchange.com/questions/24318/how-do-i-stop-two-wireless-dongles-switching-between-wlan0-and-wlan1 I think this still only applies to Jessie and may not work with Raspbian Strech. Skip this is you are only using the RPI3's internal Wifi.
-
Next setup a static IP for eth0 interface "sudo nano /etc/network/interfaces" and paste this. Make sure to comment out #iface eth0 inet manual
#iface eth0 inet manual
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255-
update packages and install dnsmasq:
sudo apt-get update"
sudo apt-get install dnsmasq -y -
Copy and save the original dnsmasq.conf
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig -
Configure the new dnsmasq.conf
sudo nano /etc/dnsmasq.conf and paste the following:
interface=eth0 # Use interface eth0
listen-address=192.168.1.1 # Explicitly specify the address to listen on
bind-interfaces # Bind the interface to make sure we aren't sending thingselsewhere
server=8.8.8.8 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=192.168.1.50,192.168.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time-
Enable IPv4 forwarding:
sudo nano /etc/sysctl.conf and uncomment the line net.ipv4.ip_forward=1 -
Setup IP tables. I used wlan1 for the USB Wifi dongle and my Duet will be setup with a static IP of 192.168.1.50 which will be setup in config.g on the Duet 2.
sudo iptables -t nat -A POSTROUTING -o wlan1 -j MASQUERADE
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.50:80 -
Configure it to load on reboot by first saving it to a file:
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" -
Then create a 'hook' file with a line to restore the ip tables on boot and add the following line:
iptables-restore < /etc/iptables.ipv4.nat
After a reboot you should automatically connect to your wifi and, assuming it has internet access, anything plugged into your Pi's ethernet port will have internet access as well.
You may also want to change some settings using sudo raspi-config and change user pi default password, change the hostname and expand the file system.
-
-
@incogizmo I managed to break it lol... should have left it along but the IP addresses used in the first link bothered me I tried to set things up using the IP addresses above for the RPI /duet but it didnt work. I also tried to start back at step one from a new flash and repeate what I originally did but no luck. I think the issue is with the Duet 2. It is reporting an IP address but It must not be getting forwarded from the RPI3. Is there anything I should check as well in the config.g?
-
@nikker Looking at the steps you did previously dnsmasq and the dhcp setup is not required if you hard set the ip address on your Duet, remember to set the netmask and gateway however:
From the config details you posted in your previous post
M552 S1 P192.168.1.50 #set Duet IP address M553 P255.255.255.0 #set subnet mask M554 P192.168.1.1 #set gateway
Key is making sure the gateway on the Duet is set to the ip the Pi has on eth0.
What is the network address you use on your wireless network? Without getting too much into routing / subnetting, if your wireless network IP is 192.168.1.x you will have problems.Removing DHCP/dnsmasq from your setup means you can remove the following steps:
update packages and install dnsmasq:
sudo apt-get update"
sudo apt-get install dnsmasq -yCopy and save the original dnsmasq.conf
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.origConfigure the new dnsmasq.conf
sudo nano /etc/dnsmasq.conf and paste the following:interface=eth0 # Use interface eth0
listen-address=192.168.1.1 # Explicitly specify the address to listen on
bind-interfaces # Bind the interface to make sure we aren't sending thingselsewhere
server=8.8.8.8 # Forward DNS requests to Google DNS
domain-needed # Don't forward short names
bogus-priv # Never forward addresses in the non-routed address spaces.
dhcp-range=192.168.1.50,192.168.1.150,12h # Assign IP addresses between 172.24.1.50 and 172.24.1.150 with a 12 hour lease time -
@incogizmo i started again with a fresh flash and still no luck, RPI3 address on my network is 192.168.50.16 and when i enter that in chrome nothing loads. I do have another duet 2 wifi running on my network right now as well but I didnt think the MAC addresses were the same. I setup my config.g as you had above (my home network is using 192.168.50.X so there shouldnt be any issues with the RPI3's 192.168.1.X) and performed the steps above with the exception of the dnsmasq and dhcp setup.
My /etc/network/interfaces looks like this (I am not using an external wifi adapter at the moment as I am trying to keep it simple so only have wlan0 for now:
auto lo
iface lo inet loopback#iface eth0 inet manual
allow-hotplug eth0
iface eth0 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.confallow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.confmy /etc/iptables.ipv4.nat file contains this:
Generated by iptables-save v1.4.21 on Thu Dec 13 06:32:18 2018
*nat
:PREROUTING ACCEPT [2:772]
:INPUT ACCEPT [2:772]
:OUTPUT ACCEPT [3:228]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.1.50:80
-A POSTROUTING -o wlan0 -j MASQUERADE
COMMIT
Completed on Thu Dec 13 06:32:18 2018I am not sure why wlan1 keeps coming up on a fresh flash even though I have never plugged one in. Is there something i need to configure in raspi-config? If i ping 192.168.1.50 on the RPI3 I see the green light on the Duet 2 ethernet port light up but other than when I ping only the orange light is solid.
-
When you ping the Duet from the PI do you get a ping reply?
If this is working then there is a problem with the ip forwarding, in this case please send the output from the following commands
sudo iptables -L sudo iptables -L -t nat grep ip_forward /etc/sysctl.conf ifconfig
If the ping reply is not working then it is likely a network configuration issue on the ethernet side, either on the duet of the PI. If you dont get a ping reply can you send your duet config and the output from
ifconfig
on the pi? -
Yeah when I ping the Duet 2 at 192.168.1.50 from the RPI3 this is what I get until I stop it with ctrl+c:
pi@raspberrypi:~ $ ping 192.168.1.50
PING 192.168.1.50 (192.168.1.50) 56(84) bytes of data.
64 bytes from 192.168.1.50: icmp_seq=1 ttl=128 time=0.659 ms
64 bytes from 192.168.1.50: icmp_seq=2 ttl=128 time=0.289 ms
64 bytes from 192.168.1.50: icmp_seq=3 ttl=128 time=0.294 ms
64 bytes from 192.168.1.50: icmp_seq=4 ttl=128 time=0.304 ms
64 bytes from 192.168.1.50: icmp_seq=5 ttl=128 time=0.302 ms
64 bytes from 192.168.1.50: icmp_seq=6 ttl=128 time=0.281 ms
64 bytes from 192.168.1.50: icmp_seq=7 ttl=128 time=0.276 ms
64 bytes from 192.168.1.50: icmp_seq=8 ttl=128 time=0.270 ms
^C
--- 192.168.1.50 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7292ms
rtt min/avg/max/mdev = 0.270/0.334/0.659/0.124 mshere is the output from the commands you asked for,
pi@raspberrypi:~ $ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destinationChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destinationpi@raspberrypi:~ $ sudo iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- anywhere anywhere tcp dpt:http to:192.168.1.50:80Chain INPUT (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destinationChain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- anywhere anywherepi@raspberrypi:~ $ grep ip_forward /etc/sysctl.conf
net.ipv4.ip_forward=1pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:1e:44:b6
inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::deb:7778:30df:3621/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:49 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:718 (718.0 B) TX bytes:8565 (8.3 KiB)lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)wlan0 Link encap:Ethernet HWaddr b8:27:eb:4b:11:e3
inet addr:192.168.50.16 Bcast:192.168.50.255 Mask:255.255.255.0
inet6 addr: fe80::42d8:ee2d:87f2:a2b1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:467 errors:0 dropped:0 overruns:0 frame:0
TX packets:284 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:51217 (50.0 KiB) TX bytes:40944 (39.9 KiB) -
@incogizmo Im not sure why it worked the first time but I eventually got it to work using some information from this post https://serverfault.com/questions/140622/how-can-i-port-forward-with-iptables
I added
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0 --dport 80 -j DNAT --to-destination 192.168.1.50:80
sudo iptables -A FORWARD -p tcp -d 192.168.1.50 --dport 80 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPTand made sure it was saved in /etc/iptables.ipv4.nat and then gave it a reboot, a prayer and bingo. Now to bookmark the two sites and make a backup of the SD card!
-
@nikker Ahhhh sounds like the default rules were set to DENY, possibly during your first run through you set the default FORWARD rule to accept,
You possibly ran something likeiptables -P FORWARD ACCEPT
or the ACCEPT rule you provided above.Anyway really good news hearing its all setup and running reliably.
-
This seems needlessly complicated, expensive, and troublesome.
Why not just get a wireless bridge device, it's not much more than the cost of the micro-SD card for the Pi, let alone the cost of the Pi itself.
Plug in the wireless bridge, plug the Duet Ethernet into it. Configure the wireless network once, and boom.
Personally, I love the Raspberry Pi. It's a great little inexpensive PC for small automation tasks, but I feel that it's utterly wasted as just a bridge when there are dedicated devices which do the job frankly better for way less money.
-
Hi,
the easiest way is to use a bridge. Something like this (small, enough bandwith):
Costs about 20 euros and can be powered over the 5 Volt pins of the duet.
I'm using it with my maestro and it's perfect.
-
Whilst I totally agree simplicity is better, there are situations where it makes sense.
When you are already using the Pi for other things eg motioneye for remote monitoring / recording or octoprint etc.
I would prefer to minimize additional components. -
All are great solutions to various scenarios... @incogizmo hit the nail on the head for my scenario, I am using something similar to motion eye to stream jpegs to DWC. I also have 5 RPI3s, about 10 USB Wifi adaptors and other little tid bits. I have a monoprice duplicator 6 with a Duet WiFi and love it. I just decided to give the Ethernet version a shot on a whim. Who knows, I may come up with other ideas where the RPI3 will come in handy.
-
Here's another possible and much easier solution... use "socat" to bridge the ports...
Give the duet and the pi ethernet port static addresses say 10.0.0.2/24 and 10.0.0.1/24 respectively then run socat as follows...
socat \ TCP4-LISTEN:80,bind=<pi_wifi_addr>,reuseaddr,fork \ TCP4:10.0.0.2:80,bind=10.0.0.1
Now any connection that comes in over wifi to port 80 will get forwarded to the duet's port 80.