Categories
Technical Technology

Raspberry PI wifi with static IP address

I have been tinkering with Raspberry Pi’s for some time now, and after finding uses for two I order a couple more. I thought I would have a more methodical approach to setting up the next one. I needed a platform to experiment with to me number one is a static IP for the network interfaces. There is plently of documentation out there to explain how to set a static IP for the Ethernet adapter but I struggled to find instructions to do the same for a wifi adapter. My aim is to have a PI that boots up and always has the same IP addresses on the wired and wireless adapters.

Firstly lets get the static IP on the wired interface. You need to edit the file /etc/network/interfaces find the reference to eth0 then edit it to set the static IP network mask and default gateway.

iface eth0 inet static
address 192.168.0.41
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.254

Once you have made the changes reboot the PI and check that the settings have stuck. Alternatively you can use the commands ifup and ifdown. so to close down the eth0 interface issue the command sudo ifdown etho then use sudo ifup etho will bring it back up again. Clearly if you have ssh’d into the PI you will lose your connection so you will have to reboot the PI anyway. Use ifconfig to check the status of the ports.

Now for the more complicated bits. To get the wifi up and running. First you need to update the files referenced from the interfaces config file /etc/wpa_supplicant/wpa_supplicant.conf this contains the configuration of the wifi interface and how it connects to access points. Find the section like the ones below and add in the details of the access points you want to connect to. I have 2 access points which I have called home1 and home2 (see “id_str=”). The details there tell the system how to connect to the access point i.e. what the name is and what the preshared key is.

network={
ssid="WifiAccessPoint_1"
psk="PresharedKeyForAccessPoint_1"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
auth_alg=OPEN
id_str="home1"
}

network={
ssid="WifiAccessPoint_2"
psk="PresharedKeyForAccessPoint_2"
proto=WPA
key_mgmt=WPA-PSK
pairwise=TKIP
auth_alg=OPEN
id_str="home2"
}

Next you need to update /etc/network/interfaces to set the details of the wifi access points and how they should be dealt with. See below I needed to to do the same as I did for the etho inerface but I reference the access points using the names I used in the id_str settings from above.

iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface home1 inet static
address 192.168.0.42
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.254

iface home2 inet static
address 192.168.0.42
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.254

I used a few websites to figure all this here are some of the more useful ones :

http://wiki.debian.org/WiFi
http://wiki.debian.org/WiFi/HowToUse
http://www.debian.org/doc/manuals/debian-reference/ch05.en.html
http://omer.me/2012/04/setting-up-wireless-networks-under-debian-on-raspberry-pi/