connecting to wireless network with linux

hello i need to make the Arduino Yun to search for a specific wireless network
and connect to it (if it's already connected to a network it needs to disconnect and connect to the right one).

i tried writing these commands to the linux:
ifconfig wlan0 down (to close the wifi)
iwconfig wlan0 essid [SSID] key s:[PASS] (the network has WEP security standard)

then when i type:
/usr/bin/pretty-wifi-info.lua
i don't see i'm connected to the new network

another thing: the command dhcpclient doesn't seem to work on the OpenWrt-Yun

thank you

Go the webpanel, click "configure", then click the link on top "advanced configuration panel". There you'll find "luci", the web interface for low level configuration. Try using luci instead than the command line

Just in case you needed a package not available out-of-the-box, plenty luci modules are available for download
http://downloads.arduino.cc/openwrtyun/1/packages/index.html

Is there a way doing this from the command line?
i need to connect and disconnect from networks automatically

Of course there is. Configuration in openwrt is done via "uci". Wireless conf is documented here Wireless configuration [Old OpenWrt Wiki]

you'll end up with a couple of "uci set" and a final "uci commit". Then calling "wifi" should restart wifi applying the new configuration

could you please help me finding what i need..

i want to connect to a network:
ssid: REX001
key: 1234
encryption: wep

the file /etc/config/wireless looks like this:

config wifi-device 'radio0'
        option type 'mac80211'
        option hwmode '11ng'
        option path 'platform/ar933x_wmac'
        option htmode 'HT20'
        list ht_capab 'SHORT-GI-20'
        list ht_capab 'SHORT-GI-40'
        list ht_capab 'RX-STBC1'
        list ht_capab 'DSSS_CCK-40'
        option disabled '0'
        option ssid 'REX002'
        option channel '11'

config wifi-iface
        option device 'radio0'
        option network 'lan'
        option mode 'ap'
        option encryption 'wep'
        option ssid 'REX001'
        option key '1234'

then i wrote: /etc/init.d/network restart
and got this message:

Command failed: Not found
Configuration file: /var/run/hostapd-phy0.conf
HT (IEEE 802.11n) with WEP is not allowed, disabling HT capabilities
Using interface wlan0 with hwaddr 90:a2:da:f6:08:84 and ssid "REX001"
wlan0: interface state UNINITIALIZED->ENABLED
wlan0: AP-ENABLED
Command failed: Not found

when i check via iwconfig, i don't see i'm connected to the REX001 network

thank you

Take a look at the common options Wireless configuration [Old OpenWrt Wiki] : the mode is incorrect
Please compare each configuration entry with the docs

thank you but it doesn't seem yet to work.
i see that the iwconfig detatils change, but i dont have ping or see my ip
how do i get the ip from the dhcp server?

Can you please detail each single command you use? iwconfig won't help configuring, only querying the wifi status

i wrote these commands:
uci set wireless.@wifi-iface[0].ssid=REX002
uci set wireless.@wifi-iface[0].encryption=wep
uci set wireless.@wifi-iface[0].key=aaaa
uci commit wireless
wifi

and i see when i type the command: /usr/bin/pretty-wifi-info.lua
this:

root@Arduino:/# /usr/bin/pretty-wifi-info.lua
Current WiFi configuration
SSID: REX002
Mode: Master
Signal: 0%
Encryption method: WEP Open System (WEP-40)
/usr/bin/lua: /usr/bin/pretty-wifi-info.lua:67: attempt to get length of field 'ipaddrs' (a nil value)
stack traceback:
/usr/bin/pretty-wifi-info.lua:67: in main chunk

: ?[/quote]

i think it is because the arduino doesn't get the ip from the router.
what is the command for getting an ip address in dhcp?

http://forum.arduino.cc/index.php?topic=216850.msg1586874#msg1586874

thank you, i wrote the commands and get this:

/sbin/uci: Parse error (invalid command) at line 16, byte 0
/sbin/uci: Parse error (invalid command) at line 16, byte 0
/sbin/uci: Parse error (invalid command) at line 16, byte 0
/sbin/uci: Parse error (invalid command) at line 16, byte 0
[ 1057.970000] wlan0: deauthenticating from 00:1f:1f:f8:87:ac by local choice (reason=3)
command failed: Device or resource busy (-16)
Command failed: Not found
Successfully initialized wpa_supplicant
Command failed: Not found
[ 1059.290000] wlan0: authenticate with 00:1f:1f:f8:87:ac
[ 1059.300000] wlan0: send auth to 00:1f:1f:f8:87:ac (try 1/3)
[ 1059.300000] wlan0: authenticated
[ 1059.300000] ath9k ar933x_wmac: wlan0: disabling HT/VHT due to WEP/TKIP use
[ 1059.320000] wlan0: associate with 00:1f:1f:f8:87:ac (try 1/3)
[ 1059.320000] wlan0: RX AssocResp from 00:1f:1f:f8:87:ac (capab=0xc11 status=0 aid=1)
[ 1059.320000] wlan0: associated
[ 1065.010000] wlan0: deauthenticating from 00:1f:1f:f8:87:ac by local choice (reason=3)
command failed: Device or resource busy (-16)
Command failed: Not found
Successfully initialized wpa_supplicant
Command failed: Not found
root@Arduino:/# [ 1066.580000] wlan0: authenticate with 00:1f:1f:f8:87:ac
[ 1066.590000] wlan0: send auth to 00:1f:1f:f8:87:ac (try 1/3)
[ 1066.590000] wlan0: authenticated
[ 1066.590000] ath9k ar933x_wmac: wlan0: disabling HT/VHT due to WEP/TKIP use
[ 1066.610000] wlan0: associate with 00:1f:1f:f8:87:ac (try 1/3)
[ 1066.610000] wlan0: RX AssocResp from 00:1f:1f:f8:87:ac (capab=0xc11 status=0 aid=1)
[ 1066.620000] wlan0: associated

and when i type the command: /usr/bin/pretty-wifi-info.lua
i get this:

Current WiFi configuration
SSID: REX002
Mode: Client
Signal: 100%
Encryption method: WEP Open System (WEP-40)
/usr/bin/lua: /usr/bin/pretty-wifi-info.lua:67: attempt to get length of field 'ipaddrs' (a nil value)
stack traceback:
        /usr/bin/pretty-wifi-info.lua:67: in main chunk
        [C]: ?

it still doesn't seem to connect

post your code.

I have not use wep for years, try this:

#!/bin/sh
/sbin/uci set  network.lan=interface
/sbin/uci set  network.lan.proto=dhcp
/sbin/uci delete  network.lan.ipaddr
/sbin/uci delete  network.lan.netmask
/sbin/uci set wireless.@wifi-iface[0].mode=sta
/sbin/uci set wireless.@wifi-iface[0].ssid=wifi_ssid
/sbin/uci set wireless.@wifi-iface[0].encryption=wep
/sbin/uci set wireless.@wifi-iface[0].key1="786468646b6b6577696f646464"
/sbin/uci set wireless.@wifi-iface[0].key=1
/sbin/uci commit wireless; /sbin/wifi
/etc/init.d/network  restart

the error looks the same :\

does the code asks for the ipaddr in dhcp?

/sbin/uci set  network.lan.proto=dhcp

asks for the ipaddr in dhcp

What is line number of error?

do you mean this message? /sbin/uci: Parse error (invalid command) at line 16, byte 0

the setupwifi.sh file has 12 lines

/sbin/uci: Parse error (invalid command) at line 16

means error coming from /etc/config/wireless line 16

near

option mode 'ap'

this is what i see:

the cursor is in line 16 where it says: option device 'radio0'

sample of /etc/config/wireless

config wifi-device 'radio0'
        option type 'mac80211'
        option hwmode '11ng'
        option path 'platform/ar933x_wmac'
        option htmode 'HT20'
        list ht_capab 'SHORT-GI-20'
        list ht_capab 'SHORT-GI-40'
        list ht_capab 'RX-STBC1'
        list ht_capab 'DSSS_CCK-40'
        option disabled '0'
        option channel 'auto'
        option country '00'

config wifi-iface
        option device 'radio0'
        option network 'lan'
        option mode 'sta'
        option ssid 'openwrt'
        option encryption 'psk2'
        option key 'password'

Yours seem broken to me.