WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Static IP over WIFI on RaspberryPI  (Read 4562 times)

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Static IP over WIFI on RaspberryPI
« on: November 01, 2013, 04:53:09 AM »
Hello,
Sorry if this question has already been solved, I haven't been able to found it.

I would like to allow the raspberrypi to connect to a wifi using an static IP.
I have been able to configure the wifi using the /usr/local/bin/wifi.sh  and I also have been able to config at init adding

/usr/local/bin/wifi.sh -a 2>&1 >/tmp/wifi.log to the /opt/bootlocal.sh

However the previous use a DHCP approach, and I would like to fix the ip for the device.
I have been trying to just execute a script after the previous line at the load that is
Code: [Select]
pkill udhcpc
ifconfig wlan0 192.168.1.30 netmask 255.255.255.0 up
route add default gw 192.168.1.1
echo nameserver 8.8.8.8 > /etc/resolv.conf
echo nameserver 8.8.4.4 >> /etc/resolv.conf

But I wonder if there a way to avoid "kill udhcpc" and call the ifconfig again (that is already called on the wifi.sh).

Thank you in advance
Regards
Gabriel


Offline roberts

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: Static IP over WIFI on RaspberryPI
« Reply #1 on: November 01, 2013, 05:45:13 AM »
Use boot code nodhcp
10+ Years Contributing to Linux Open Source Projects.

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Static IP over WIFI on RaspberryPI
« Reply #2 on: November 01, 2013, 12:21:46 PM »
Hello,

Thank you for the response, I have a naive question, how can I add a bootcode? I have been searching and I don't found what file I should modify.

Regarding the dhcp, once disconnect the dhcp using the bootcode, how can I asign the static ip / gateway ...?
On a normal linux I would use the network/interfaces, but such file is not here.

Thank you again

Offline roberts

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: Static IP over WIFI on RaspberryPI
« Reply #3 on: November 01, 2013, 12:41:44 PM »
AFAIK, update cmdline.txt

At least on the initial builds of Core on Rpi, before I handed it off to Bmarkus, on the sdcard, there is/was a file named cmdline.txt
You would have to edit this plain text file to add the nodhcp boot code, or any other boot codes.

On Armv7 systems I had made a script to mount the sdcard and call an editor for such purposes. I don't know of Bmarkus has made such available.

Anyway, I would start by checking if sdcard is mounted, if not mount it and look for cmdline.txt. Be sure to use a text editor to add nodhcp to the end of the line and do not start a new line.

I am sure Bmarkus will chime in if the procedure has changed from my initial builds.
10+ Years Contributing to Linux Open Source Projects.

Online bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 6907
    • My Community Forum
Re: Static IP over WIFI on RaspberryPI
« Reply #4 on: November 01, 2013, 12:54:06 PM »
No change, it is still the same /mnt/mmcblk0p1/cmdline.txt file where the boot codes are.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Static IP over WIFI on RaspberryPI
« Reply #5 on: November 01, 2013, 02:58:57 PM »
Again, thank you.

However it doesn't solve the wifi connections.
I've been reviewing the wifi.sh script to try to figure out how it works, and I think it would be great that it stores on the wifi.db the static ip, gatewat and broadcast  (if any) so it could be automatically used instead of dhcp. In case there is not such values, the dhcp would be used as now.
My bash script skill is very poor, but it would be a great improvement.

Gabriel


Offline roberts

  • Administrator
  • Hero Member
  • *****
  • Posts: 7361
  • Founder Emeritus
Re: Static IP over WIFI on RaspberryPI
« Reply #6 on: November 01, 2013, 05:39:31 PM »
The wifi script is for scanning. A static IP is not scanning.
I would think using the control panel -> Network should see your wlan0.
There is where you would setup your static IP.
10+ Years Contributing to Linux Open Source Projects.

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Static IP over WIFI on RaspberryPI
« Reply #7 on: November 02, 2013, 09:09:34 AM »
I agree about being for scanning the different networks available, but the WiFi script also allows to be used wi the -p parameter and in that case it also ask for automatic (dhcp) configuration or manual configuration asking the user to introduce the IP gateway and broadcast, using ifconfig and route instead of dhcp to configure.
So why not just store those values alongside the WiFi ssid an password so it could be used to configure the WiFi?
Also I'm not capabnle of using the visual tool as I'm just using a CLI.
The idea is to allow to use tci to be used on a raspi to create embedded software that is network controllable.

Gabriel

Offline gpulido

  • Jr. Member
  • **
  • Posts: 57
Re: Static IP over WIFI on RaspberryPI
« Reply #8 on: November 05, 2013, 11:38:52 AM »
Hello,
I write down here the solution I've implemented, just in case anyone could find it usefull:

1) Create a file /opt/wlan0.sh with the following content:

Code: [Select]
#!/bin/sh
interface="wlan0"
ssid="YOUR_SSID"
pass="YOUR_PASS"
ipaddr="192.168.1.31"
netmask="255.255.255.0"
gateway="192.168.1.1"
dns1="8.8.8.8"
dns2="8.8.4.4"

wpa_passphrase  ${ssid}  ${pass} > /etc/wpa_supplicant.conf
wpa_supplicant -i  ${interface}  -c /etc/wpa_supplicant.conf -B >/dev/null 2>&1
ifconfig  ${interface}  ${ipaddr}  netmask  ${netmask}  up
route add default gw ${gateway}
echo nameserver ${dns1} >> /etc/resolv.conf
echo nameserver ${dns2} >> /etc/resolv.conf

2) Make the previous file executable:
Code: [Select]
sudo chmod 777 /opt/wlan0.sh
3) Call it on the boot. If you use the previous script, do not use the wifi.sh script. Both are incompatibles.
Add a line to the /opt/bootlocal.sh
Code: [Select]
/opt/wlan0.sh 2>&1 >/tmp/wifi0.log
4) Persist the changes. If you already have the opt dir added to the /opt/.filetool.sh (it is added by default)
Code: [Select]
filetool.sh -b
5) Reboot