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
Line 4: Line 4:
= Set up the Raspberry Pi server static IP Address =
= Set up the Raspberry Pi server static IP Address =


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 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:


== Step 1: Find Your Current Network Information ==
== Scenario Overview:==
Before configuring the static IP, find out your current network details, such as the gateway and DNS servers, by running the following command:
* 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.


<syntaxhighlight lang="bash">
==Plan:==
ip r
* Configure the Raspberry Pi to have a static IP only when connected to Router #2's network.
</syntaxhighlight>
* Ensure that when connected to Router #1, the Raspberry Pi continues to use dynamic IP (DHCP) for regular internet access.


This command will output something like this:
==Steps to Set a Static IP for the Experimental Router Only==
===1. Find Your Network Information for Router #2===


<syntaxhighlight lang="bash">
You need the following network details from Router #2:
default via 192.168.2.1 dev wlan0
192.168.2.0/24 dev wlan0 proto dhcp src 192.168.2.45 metric 303
</syntaxhighlight>


In this example:
* 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 34:
</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 44:
</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).
 
==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)==


== Step 4: Save and Exit ==
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.
After editing the file, save the changes:


Press CTRL + X to exit.
==5. Reboot the Raspberry Pi==
Press Y to confirm saving the changes.
Press Enter to confirm the file name.


== Step 5: Reboot the Raspberry Pi ==
Reboot your Raspberry Pi for the changes to take effect:
Reboot your Raspberry Pi to apply the changes:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 63: Line 67:
</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 75:
</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.

Revision as of 05:44, 5 October 2024

Set up the IoT WiFi router SSID

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.