programmatically wi-fi configuration

Hi all, I'm trying to make an app that can change wifi password of the arduino yun. To do that the app send a POST message to arduino, where php loads the script to change the settings. But after the script runs the wifi password is set but dhcp seems stop working, because no ip is assigned to the connected device.

Here's the script:

#!/bin/sh
/sbin/uci set wireless.@wifi-iface[0].mode=ap
/sbin/uci set wireless.@wifi-iface[0].ssid="BikeBoard"
/sbin/uci set wireless.@wifi-iface[0].encryption=psk-mixed
/sbin/uci set wireless.@wifi-iface[0].key="pippo123"
/sbin/uci commit wireless
/sbin/wifi
/etc/init.d/network  restart

I'll appreciate any kind of help you can give me :)

...
/etc/init.d/dnsmasq restart
/etc/init.d/network restart

http://www.ibuyopenwrt.com/index.php/8-yun-compatible/110-set-up-dhcp-server-at-wan-ethernet-port

Thank you @sonnyyu, i've tried your code, unfortunately I discovered the problem is not the dhcp, 'cause still no ip is assigned to the connected device... so i tried to run the script manually, line by line, I discovered that when commands wifi and /etc/init.d/network restart are run the answer is that:

command failed: Device or resource busy (-16)
Configuration file: /var/run/hostapd-phy0.conf
wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
wlan0: interface state COUNTRY_UPDATE->HT_SCAN
Using interface wlan0 with hwaddr 90:a2:da:f3:1c:58 and ssid "BikeBoard"
wlan0: interface state HT_SCAN->ENABLED
wlan0: AP-ENABLED

that command failed doesn't seem friendly...

...
/sbin/uci commit wireless
wifi down; wifi up
...

http://www.ibuyopenwrt.com/index.php/14-yun-compatible/network-and-yun/29-setup-monitor-mode

p4ever: ... unfortunately I discovered the problem is not the dhcp, 'cause still no ip is assigned to the connected device...

Does Yun get IP address? or connected device to Yun does not get IP address from Yun's DHCP server?

Yun's IP should be static, while others devices connected should receive the IP from Yun's DHCP Server.

i tried your suggestion but nothing changed, I tried this:

...
uci commit wireless; 
wifi down; 
/etc/init.d/dnsmasq restart;
/etc/init.d/network restart; 
wifi up

and this:

...
uci commit wireless; 
wifi down; 
wifi up

the same error occurred...

Here's my network config:

config interface 'loopback'
    option ifname 'lo'
    option proto 'static'
    option ipaddr '127.0.0.1'
    option netmask '255.0.0.0'

config interface 'lan'
    option proto 'static'
    option ipaddr '192.168.240.1'
    option netmask '255.255.255.0'

config interface 'wan'
    option ifname 'eth1'
    option proto 'dhcp'
    option metric '10'

config interface 'wwan'
    option proto 'dhcp'

Connect to wifi network:

#!/bin/sh
/sbin/uci set wireless.@wifi-iface[0].mode=ap
/sbin/uci set wireless.@wifi-iface[0].ssid="BikeBoard"
/sbin/uci set wireless.@wifi-iface[0].encryption=psk-mixed
/sbin/uci set wireless.@wifi-iface[0].key="pippo123"
/sbin/uci commit wireless
wifi down; wifi up
/etc/init.d/network  restart

Set up dhcp server at wan (ethernet port):

http://www.ibuyopenwrt.com/index.php/8-yun-compatible/110-set-up-dhcp-server-at-wan-ethernet-port

it’s already set up both for wan and lan (which is the one “BikeBoard” is connected to), here’s the dhcp config:

...
config dhcp 'lan'
 option interface 'lan'
 option leasetime '12h'
 option start '190'
 option limit '8'
 option ignore '0'

config dhcp 'wan'
 option interface 'wan'
 option start '100'
 option limit '150'
 option leasetime '12h'
 option ignore '0'

EDIT:
Even with your code:

#!/bin/sh
/sbin/uci set wireless.@wifi-iface[0].mode=ap
/sbin/uci set wireless.@wifi-iface[0].ssid="BikeBoard"
/sbin/uci set wireless.@wifi-iface[0].encryption=psk-mixed
/sbin/uci set wireless.@wifi-iface[0].key="pippo123"
/sbin/uci commit wireless
wifi down; wifi up
/etc/init.d/network  restart

the error “command failed: Device or resource busy (-16)” comes out…

  1. Reset Yun to factory default by:
reset-to-factory-anyway
reboot

( backup your own data )

  1. Setup WIFI STA mode:

STA for managed (client) mode

http://www.ibuyopenwrt.com/index.php/8-yun-compatible/110-set-up-dhcp-server-at-wan-ethernet-port

/sbin/uci set wireless.@wifi-iface[0].mode=sta

if You setup Yun as client of WIFI Router.

STA for managed (client) mode

http://www.ibuyopenwrt.com/index.php/14-yun-compatible/network-and-yun/28-sta-for-managed-client-mode

... config dhcp 'lan' option interface 'lan' option leasetime '12h' option start '190' option limit '8' option ignore '0'

config dhcp 'wan' option interface 'wan' option start '100' option limit '150' option leasetime '12h' option ignore '0'

You can't have both lan and wan as dhcp server.

Wan connection is not used, so i think there is no conflict problem, as far as i see if i reboot the yun the configuration is accepted (and i think it's faster then backup my preferences and reload it each time), now i have just to reset also the ATMega from Linux before the reboot

EDIT: This is my solution

#!/bin/sh
/sbin/uci set wireless.@wifi-iface[0].mode=ap
/sbin/uci set wireless.@wifi-iface[0].ssid="BikeBoard"
/sbin/uci set wireless.@wifi-iface[0].encryption=psk-mixed
/sbin/uci set wireless.@wifi-iface[0].key=$1
/sbin/uci commit wireless; 
echo "Password changed, system is rebooting"
reset-mcu
/sbin/reboot

It might be upstream bug.

/etc/init.d/dnsmasq restart

Restore to factory default.

reset-to-factory-anyway
reboot

Turn on AP encryption and Wifi ( lan ) DHCP

nano setencryptiondhcp.sh
#!/bin/sh
/sbin/uci set wireless.@wifi-iface[0].mode=ap
/sbin/uci set wireless.@wifi-iface[0].ssid=Yun
/sbin/uci set wireless.@wifi-iface[0].encryption=psk-mixed
/sbin/uci set wireless.@wifi-iface[0].key=password
/sbin/uci commit wireless
wifi down; wifi up
/sbin/uci set dhcp.lan.start=100
/sbin/uci set dhcp.lan.limit=150
/sbin/uci set dhcp.lan.leasetime=6h
/sbin/uci set dhcp.lan.ignore=0
/sbin/uci commit dhcp
/etc/init.d/network restart
/etc/init.d/dnsmasq reload

restart is failed, but not reload. I will submit upstream for further help.

https://forum.openwrt.org/viewtopic.php?pid=284184#p284184