Smart Cities - Setup IoT WiFi Router: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Set up the IoT WiFi router SSID =
= Set up the IoT WiFi router SSID =


= Test that your Raspberry Pi can connect to the Experimental Router =
To verify that your Raspberry Pi is connected to the experimental router with the 192.168.2.x subnet, you can perform a few simple checks. These checks will confirm whether the Raspberry Pi is using the expected network configuration. Here's how you can do it:


= Set up the Raspberry Pi server static IP Address =
==1. Check IP Address==
The first step is to verify that your Raspberry Pi has an IP address in the 192.168.2.x range, which indicates it is connected to the experimental router.
 
Run the following command to display the IP address of the wlan0 (WiFi) or eth0 (Ethernet) interface:
 
<syntaxhighlight lang="bash">
ip a
</syntaxhighlight>
 
Look for the inet line under the wlan0 or eth0 section. If you see something like this:
 
<syntaxhighlight lang="bash">
inet 192.168.2.x/24
</syntaxhighlight>
 
This confirms that your Raspberry Pi is connected to a router in the 192.168.2.x subnet. If you see an IP address in a different range (like 192.168.1.x), the Pi is connected to the wrong network.
 
==2. Check Default Gateway==
The default gateway is the router through which your Raspberry Pi sends traffic to other networks, such as the internet. To check if the gateway is your experimental router:
 
<syntaxhighlight lang="bash">
ip route
</syntaxhighlight>
 
You should see an output similar to this:
 
<syntaxhighlight lang="bash">
default via 192.168.2.1 dev wlan0
</syntaxhighlight>
 
* default via 192.168.2.1: This confirms that your Raspberry Pi is using the experimental router (which likely has the IP address 192.168.2.1) as the default gateway.
* dev wlan0: This shows that the connection is via the WiFi interface (use eth0 for Ethernet).
If the gateway IP is different (e.g., 192.168.1.1), then your Pi is connected to Router #1 instead.
 
==3. Check Router Response with ping==
You can ping the experimental router (192.168.2.1) to ensure that the Raspberry Pi is communicating with it correctly:
 
<syntaxhighlight lang="bash">
ping 192.168.2.1
</syntaxhighlight>
 
You should get a response like:
 
<syntaxhighlight lang="bash">
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=1.23 ms
</syntaxhighlight>


To set up a static IP address on your Raspberry Pi so it connects to a WiFi router (e.g., setting it to 192.168.2.2), you will need to modify the Raspberry Pi's network configuration. Below are the steps to achieve this:
If you receive responses, the Raspberry Pi is successfully connected to the experimental router. If not, the connection might be to a different network.


== Step 1: Find Your Current Network Information ==
==4. Check Connected SSID (if using WiFi)==
Before configuring the static IP, find out your current network details, such as the gateway and DNS servers, by running the following command:
If your Raspberry Pi is connected to the experimental router via WiFi, you can check the SSID of the network to ensure it matches the experimental router’s SSID.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
ip r
iwgetid
</syntaxhighlight>
</syntaxhighlight>


This command will output something like this:
The output will show something like:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
default via 192.168.2.1 dev wlan0  
wlan0     ESSID:"YourExperimentRouterSSID"
192.168.2.0/24 dev wlan0 proto dhcp src 192.168.2.45 metric 303
</syntaxhighlight>
</syntaxhighlight>


In this example:
This confirms the Raspberry Pi is connected to the correct WiFi network.
 
= Set up the Raspberry Pi server static IP Address =
 
If you have two routers at home, one for internet access (Router #1) and the other for experimentation (Router #2), and you want to set a static IP address for your Raspberry Pi only when it's connected to Router #2, here's how to handle it:
 
== Scenario Overview:==
* Router #1: Used for internet access (e.g., 192.168.1.1)
* Router #2: Used for experimentation (e.g., 192.168.2.1)
* You want the Raspberry Pi to use a static IP (e.g., 192.168.2.2) when connected to Router #2.
 
==Plan:==
* Configure the Raspberry Pi to have a static IP only when connected to Router #2's network.
* Ensure that when connected to Router #1, the Raspberry Pi continues to use dynamic IP (DHCP) for regular internet access.
 
==Steps to Set a Static IP for the Experimental Router Only==
==1. Find Your Network Information for Router #2==
 
You need the following network details from Router #2:
 
* Router (gateway) IP address: Likely 192.168.2.1
* DNS server: Can be Router #2's IP or a public DNS like 8.8.8.8.
* Subnet mask: /24 usually corresponds to 255.255.255.0.


Gateway (router): 192.168.2.1
==2. Configure Static IP for Specific WiFi Network (Router #2)==
Network range: 192.168.2.0/24


== Step 2: Modify the DHCP Client Configuration ==
The Raspberry Pi can be set to use different network configurations based on the WiFi network it connects to. This is done in the /etc/dhcpcd.conf file by associating static IP settings with a specific SSID (network name).
Edit the dhcpcd.conf file to configure the static IP. This file tells your Raspberry Pi to always request the same IP address when connecting to the router.


Open the configuration file using a text editor like nano:
Open the DHCP configuration file for editing:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 34: Line 100:
</syntaxhighlight>
</syntaxhighlight>


== Step 3: Add Static IP Configuration ==
At the end of the file, add the following section, which will apply only when connected to the WiFi network of Router #2:
Scroll to the bottom of the file and add the following lines:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
interface wlan0
interface wlan0
ssid "YourExperimentRouterSSID"
static ip_address=192.168.2.2/24
static ip_address=192.168.2.2/24
static routers=192.168.2.1
static routers=192.168.2.1
Line 44: Line 110:
</syntaxhighlight>
</syntaxhighlight>


* interface wlan0: This specifies the wireless network interface.
* ssid "YourExperimentRouterSSID": Replace this with the actual SSID (network name) of Router #2.
* static ip_address: Set the static IP you want (e.g., 192.168.2.2).
* static ip_address: This sets the static IP for the Raspberry Pi when connected to Router #2.
* static routers: Set this to your router’s IP (e.g., 192.168.2.1).
* static routers: The IP of Router #2 (likely 192.168.2.1).
* static domain_name_servers: Specify the DNS servers (usually your router's IP and an external DNS like Google's 8.8.8.8).
* static domain_name_servers: The DNS server; it can be the router’s IP or an external DNS (like Google’s 8.8.8.8).


== Step 4: Save and Exit ==
==3. Save and Exit==
After editing the file, save the changes:


Press CTRL + X to exit.
* Press CTRL + X to exit.
Press Y to confirm saving the changes.
* Press Y to save changes.
Press Enter to confirm the file name.
* Press Enter to confirm.


== Step 5: Reboot the Raspberry Pi ==
==4. Keep DHCP for Other Networks (e.g., Router #1)==
Reboot your Raspberry Pi to apply the changes:
 
When the Pi connects to other networks (such as Router #1 for internet access), it will continue to use DHCP (dynamic IP), because we have not specified static settings for other SSIDs.
 
==5. Reboot the Raspberry Pi==
 
Reboot your Raspberry Pi for the changes to take effect:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 63: Line 133:
</syntaxhighlight>
</syntaxhighlight>


== Step 6: Verify the Static IP Address ==
6. Testing the Setup
After rebooting, verify that your Raspberry Pi is using the static IP address you set (192.168.2.2) by running:
 
Connect to Router #2 (experimental network): Verify the Pi gets the static IP (192.168.2.2) by running:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 70: Line 141:
</syntaxhighlight>
</syntaxhighlight>


Look for the wlan0 interface, and you should see 192.168.2.2 as the IP address.
* Look for the wlan0 interface, and check that the IP is 192.168.2.2.
* Connect to Router #1 (internet network): Verify that the Raspberry Pi receives an IP dynamically via DHCP from Router #1’s IP range (192.168.1.x or similar).


== Troubleshooting ==
==Important Notes:==
* If the static IP isn't applied, check if the WiFi connection is working and the router's DHCP isn't conflicting with the static IP.
* Different Subnets: Routers #1 and #2 should be on different subnets (e.g., 192.168.1.x for Router #1 and 192.168.2.x for Router #2). This ensures no IP conflicts between the two routers.
* Make sure the static IP (192.168.2.2) is outside your router’s DHCP range or reserved for static assignment.
* SSID: Make sure the SSID of Router #2 is correctly specified in /etc/dhcpcd.conf, as the static IP configuration will only apply when connected to that specific SSID.
* This should successfully configure your Raspberry Pi with a static IP address connected to your WiFi router.
* This setup ensures that the Raspberry Pi will use a static IP only when connected to the experimental router (Router #2), while using dynamic IP (DHCP) when connected to other networks like Router #1.

Latest revision as of 05:57, 5 October 2024

Set up the IoT WiFi router SSID

Test that your Raspberry Pi can connect to the Experimental Router

To verify that your Raspberry Pi is connected to the experimental router with the 192.168.2.x subnet, you can perform a few simple checks. These checks will confirm whether the Raspberry Pi is using the expected network configuration. Here's how you can do it:

1. Check IP Address

The first step is to verify that your Raspberry Pi has an IP address in the 192.168.2.x range, which indicates it is connected to the experimental router.

Run the following command to display the IP address of the wlan0 (WiFi) or eth0 (Ethernet) interface:

ip a

Look for the inet line under the wlan0 or eth0 section. If you see something like this:

inet 192.168.2.x/24

This confirms that your Raspberry Pi is connected to a router in the 192.168.2.x subnet. If you see an IP address in a different range (like 192.168.1.x), the Pi is connected to the wrong network.

2. Check Default Gateway

The default gateway is the router through which your Raspberry Pi sends traffic to other networks, such as the internet. To check if the gateway is your experimental router:

ip route

You should see an output similar to this:

default via 192.168.2.1 dev wlan0
  • default via 192.168.2.1: This confirms that your Raspberry Pi is using the experimental router (which likely has the IP address 192.168.2.1) as the default gateway.
  • dev wlan0: This shows that the connection is via the WiFi interface (use eth0 for Ethernet).

If the gateway IP is different (e.g., 192.168.1.1), then your Pi is connected to Router #1 instead.

3. Check Router Response with ping

You can ping the experimental router (192.168.2.1) to ensure that the Raspberry Pi is communicating with it correctly:

ping 192.168.2.1

You should get a response like:

64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=1.23 ms

If you receive responses, the Raspberry Pi is successfully connected to the experimental router. If not, the connection might be to a different network.

4. Check Connected SSID (if using WiFi)

If your Raspberry Pi is connected to the experimental router via WiFi, you can check the SSID of the network to ensure it matches the experimental router’s SSID.

iwgetid

The output will show something like:

wlan0     ESSID:"YourExperimentRouterSSID"

This confirms the Raspberry Pi is connected to the correct WiFi network.

Set up the Raspberry Pi server static IP Address

If you have two routers at home, one for internet access (Router #1) and the other for experimentation (Router #2), and you want to set a static IP address for your Raspberry Pi only when it's connected to Router #2, here's how to handle it:

Scenario Overview:

  • Router #1: Used for internet access (e.g., 192.168.1.1)
  • Router #2: Used for experimentation (e.g., 192.168.2.1)
  • You want the Raspberry Pi to use a static IP (e.g., 192.168.2.2) when connected to Router #2.

Plan:

  • Configure the Raspberry Pi to have a static IP only when connected to Router #2's network.
  • Ensure that when connected to Router #1, the Raspberry Pi continues to use dynamic IP (DHCP) for regular internet access.

Steps to Set a Static IP for the Experimental Router Only

1. Find Your Network Information for Router #2

You need the following network details from Router #2:

  • Router (gateway) IP address: Likely 192.168.2.1
  • DNS server: Can be Router #2's IP or a public DNS like 8.8.8.8.
  • Subnet mask: /24 usually corresponds to 255.255.255.0.

2. Configure Static IP for Specific WiFi Network (Router #2)

The Raspberry Pi can be set to use different network configurations based on the WiFi network it connects to. This is done in the /etc/dhcpcd.conf file by associating static IP settings with a specific SSID (network name).

Open the DHCP configuration file for editing:

sudo nano /etc/dhcpcd.conf

At the end of the file, add the following section, which will apply only when connected to the WiFi network of Router #2:

interface wlan0
ssid "YourExperimentRouterSSID"
static ip_address=192.168.2.2/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1 8.8.8.8
  • ssid "YourExperimentRouterSSID": Replace this with the actual SSID (network name) of Router #2.
  • static ip_address: This sets the static IP for the Raspberry Pi when connected to Router #2.
  • static routers: The IP of Router #2 (likely 192.168.2.1).
  • static domain_name_servers: The DNS server; it can be the router’s IP or an external DNS (like Google’s 8.8.8.8).

3. Save and Exit

  • Press CTRL + X to exit.
  • Press Y to save changes.
  • Press Enter to confirm.

4. Keep DHCP for Other Networks (e.g., Router #1)

When the Pi connects to other networks (such as Router #1 for internet access), it will continue to use DHCP (dynamic IP), because we have not specified static settings for other SSIDs.

5. Reboot the Raspberry Pi

Reboot your Raspberry Pi for the changes to take effect:

sudo reboot

6. Testing the Setup

Connect to Router #2 (experimental network): Verify the Pi gets the static IP (192.168.2.2) by running:

ip a
  • Look for the wlan0 interface, and check that the IP is 192.168.2.2.
  • Connect to Router #1 (internet network): Verify that the Raspberry Pi receives an IP dynamically via DHCP from Router #1’s IP range (192.168.1.x or similar).

Important Notes:

  • Different Subnets: Routers #1 and #2 should be on different subnets (e.g., 192.168.1.x for Router #1 and 192.168.2.x for Router #2). This ensures no IP conflicts between the two routers.
  • SSID: Make sure the SSID of Router #2 is correctly specified in /etc/dhcpcd.conf, as the static IP configuration will only apply when connected to that specific SSID.
  • This setup ensures that the Raspberry Pi will use a static IP only when connected to the experimental router (Router #2), while using dynamic IP (DHCP) when connected to other networks like Router #1.