Smart Cities - Setup IoT WiFi Router
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.