4

I'm pretty new to Raspberry and I searched the net for a while now trying to find a good and simple answer to this question.

Ok, so I have a network that is connected to the internet. I want to start a new network for IOT devices around my workplace to do and alert me on various things. I'm using HomeAssistant for the system, and I have a separate router for the IOT network.

My question is how can I set it all up so the Raspberry will be connected to the internet through the wifi and connected to the IOT network through the ethernet?

I heard they can't work together at the same time and I want it to either switch between them automatically or if it does work if someone can point me in the right direction of where to read or what to look for that will really help me a lot.

I run the Raspberry Pi 3 model B+.

EDIT: I posted the solution as an answer. at least what has worked for me for the last few days now.

  • 1
    I don't know where you "heard they can't work together at the same time" - just plug it in. Whether it works depends on the IOT network – Milliways Jun 7 '18 at 5:26
  • Ok so if I have a router that have internet connection, gives a static up to the rpi through the WiFi... and the rpi connected by Ethernet cable to another router that serves as the IOT network router, there shouldn’t be any problem with communication through home assistant after I get all the settings right? Just making sure, or do I have to install the packages on the other link you gave me here? – Sagi Rokach Jun 7 '18 at 10:23
  • @SagiRokach troubleshooting multiple networks can be tricky unless you use something like "sudo ifconfig" to verify what exactly the Pi is connected to. If that is your difficulty then we can make that the official answer if we edit your question slightly. – OyaMist Jun 7 '18 at 13:29
  • I know I can make the router assign the up to the pi by the MAC address that is not the problem, I just want to know that that is at all possible before I start spending time figuring it out with my network configuration. – Sagi Rokach Jun 7 '18 at 14:17
2

Yes, it is possible. The rpi loaded with raspbian (or any other GNU/Linux based distro) can be used as a router to connect different networks, only that technically there are a number of different subjects related to networking that you have to be able to cope and solve in order to get it working. So... the questions:

  • Do you want to set up a new network segment for the wired network so that the RPI and the IOT devices can see each other? The answer there is probably "yes", right?
  • Will you use a dhcp service on this network segment so that all devices get automagically set up? Will the DHCP service run on the RPI? If that's the case, you will have to use dnsmasq, probably (there are other services available, like udhcpd, for example but dnsmasq also helps with the DNS set up).
  • Do you want your IOT devices to be reachable to devices outside of the RPI (like, traffic coming from the wireless). This will probably require some hacking on the routing of your whole network (or at least the boxes that you want to be able to reach the IOT devices) to teach them that in order to reach network segment x.x.x.x/24 they have to throw the traffic through the rpi.
  • Do you want your IOT devices to be able to reach devices outside of the IOT network while not making this network accessible to devices outside of this segment? This means you will need to set up some DNAT or masquerade on the RPI.
  • In comments the OP mentions that "the rpi connected by Ethernet cable to another router that serves as the IOT network router", so presumably the Pi will just be a regular node there and dhcp, etc. of the other nodes will be handled by the router. – goldilocks Jun 7 '18 at 15:44
  • @goldilocks you are right, at least that is the plan. – Sagi Rokach Jun 7 '18 at 16:35
1

If the Pi is a regular node on both networks, this is just a matter of adding a route for the non-internet connected IOT subnet; your default route will be the WLAN, since that's what you want to use to connect to anything that isn't part of the IOT subnet.

If the CIDR of the IOT subnet is 10.100.0.0/16, the router for it is at 10.100.0.1, and the ethernet interface is eth0 (note this presumes that interface is up, connected, and has a 10.100.0.0/16 IP address):

sudo ip route add 10.100.0.0/16 via 10.100.0.1 dev eth0

ip route with no other commands will show the current routing table (you don't need sudo for that). It might now look like this:

default via 192.168.0.1 dev wlan0 
192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.20
10.100.0.0/16 via 10.100.0.1 dev eth0

The first line is the default route mentioned earlier; everything that doesn't match the other two is presumed to go through the 192.168.0.1 router connected via wlan0 (this may not be the same, it's whatever your wifi link is). The second line is a direct broadcast route on the WLAN. The last one is for the IOT subnet.

You can add this to your networking configuration to make it permanent, but alas, I don't use the conventional configuration system so I don't know how to do that (pretty sure it is just adding the last part of that ip route add command to a file somewhere).

so here is what I got from the ip route command:

default via 192.168.1.1 dev eth0 src 192.168.1.50 metric 202
default via 10.0.0.138 dev wlan0 src 10.0.0.101 metric 303

You should not have two default routes. Try:

sudo ip route del default via 10.0.0.138 dev wlan0 src 10.0.0.101
  • I’ll try looking into that. Thank you for the help! – Sagi Rokach Jun 7 '18 at 16:38
  • It appears to be working without the route tables... but I'll keep looking if it starts to mess things up and I'll that. THANK YOU! – Sagi Rokach Jun 10 '18 at 7:06
  • I added a suggestion above about the routing problem. – goldilocks Jun 10 '18 at 13:35
  • wait.... I think our networks are the opposite.... the wlan is 10.0.0.0 and the eth is 192.168.1.0..... so I still should remove the default from the wlan? or do I need to change it to remove the eth default? – Sagi Rokach Jun 10 '18 at 13:39
  • I removed the other line cause our networks are reversed. it worked! thank you so much! – Sagi Rokach Jun 10 '18 at 13:56
1

After connecting the pi over the wlan to the internet network and through eth0 to the IOT network, the mqtt I run over the IOT network works, but the notifications that worked before stopped working. I tried installing some packages but it wouldn't find them and won't install them, that's when I started thinking it was a problem with the routing table like @goldilocks suggested.

what I got from the ip route command:

pi@hassbian:~ $ ip route
default via 192.168.1.1 dev eth0 src 192.168.1.50 metric 202
default via 10.0.0.138 dev wlan0 src 10.0.0.101 metric 303
10.0.0.0/24 dev wlan0 proto kernel scope link src 10.0.0.101 metric 303
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50 metric 202

In order to work through the internet which is run on the 10.0.0.0/24 network I needed to edit the /etc/dhcpcd.conf file and add at the end of the file:

interface wlan0
metric 200

interface eth0
metric 300

Assigning lower metric give precedence over the other in terms of where the internet connection comes from (at least that is what I understood). This will result in the wlan0 making all the internet connection.

I hope this will help people solve their connection problems.

  • This is the brute force approach. The correct solution is to add interface eth0 nogateway to /etc/dhcpcd.conf See How to set up networking/WiFi As goldilocks said "You should not have two default routes". – Milliways May 28 at 3:48
0

If the 2 networks have DHCP servers (operating on their own network - using multiple DHCP servers on a single network is problematical) then the Pi should obtain the necessary configuration parameters, then it should work.

No additional configuration should be required if using the default dhcpcd network manager. route will show the final routing. If this cannot be done automatically then you may need to manually configure the routing table as suggested by goldilocks - this is more involved, as the table is normally automatically configured and will overwrite manual configuration.

  • But they are connected to 2 completely different networks that won’t be connected. And the pi will have a static up from both of the routers, will it still be a problem? – Sagi Rokach Jun 8 '18 at 4:49
  • You CANNOT supply a static IP from a router - most routers can be configured to always offer the same IP address to a MAC, which is provided by DHCP, but should also provide other network configuration. – Milliways Jun 8 '18 at 5:03
  • Week yeah that’s what I meant sorry. – Sagi Rokach Jun 8 '18 at 5:41
  • For now it appears to be working without any configuration needed. THANK YOU for the information. – Sagi Rokach Jun 10 '18 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.