├── .gitignore ├── rc.local ├── wifi.cfg ├── LICENSE ├── easy-wifi-switch └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *~ -------------------------------------------------------------------------------- /rc.local: -------------------------------------------------------------------------------- 1 | # Put your custom commands here that should be executed once 2 | # the system init finished. By default this file does nothing. 3 | 4 | easy-wifi-switch 5 | wifi-live-or-reset 6 | boot-complete-notify 7 | 8 | # Uncomment the following line in order to reset the microntroller 9 | # right after linux becomes ready 10 | 11 | #reset-mcu 12 | 13 | # Uncomment the following line in order to disable kernel console 14 | # debug messages, thus having a silent and clean serial communication 15 | # with the microcontroller 16 | 17 | #echo 0 > /proc/sys/kernel/printk 18 | 19 | exit 0 20 | -------------------------------------------------------------------------------- /wifi.cfg: -------------------------------------------------------------------------------- 1 | # Enter your WiFi network information here. 2 | 3 | # Make sure each value comes between the double quotes, and that 4 | # there are no spaces on either side of the equals sign. 5 | 6 | # Your WiFi network name. This is case-sensitive. 7 | ssid="My Network Name" 8 | 9 | # Your WiFi network password. This is case-sensitive. 10 | key="password123" 11 | 12 | # Your WiFi network encryption method. Assuming you have this set 13 | # up at home, it is very unlikely you'll have to change this. 14 | # Possible options are: 15 | # psk - wpa personal 16 | # psk2 - wpa2 personal 17 | # wpa - wpa enterprise 18 | # wpa2 - wpa2 enterprise 19 | # none - unencrypted network 20 | encryption="psk2" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Sam Brenner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /easy-wifi-switch: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | UCI="/sbin/uci" 4 | log="/var/log/easy-wifi-switch.log" 5 | 6 | # set_or_delete is borrowed from /etc/init.d/handle_wifi_reset 7 | set_or_delete() { 8 | key=$1 9 | value=$2 10 | 11 | if [ "x$value" == "x" ] 12 | then 13 | $UCI delete "$key" 14 | else 15 | $UCI set "$key=$value" 16 | fi 17 | } 18 | 19 | # check config exist 20 | wificfg_a="/mnt/sda1/wifi.cfg" 21 | wificfg_b="/mnt/sdb1/wifi.cfg" 22 | cfg_load=false 23 | 24 | # load config 25 | if [ -f "$wificfg_a" ] ; then 26 | source "$wificfg_a" 27 | cfg_load=true 28 | fi 29 | 30 | if [ -f "$wificfg_b" ] ; then 31 | source "$wificfg_b" 32 | cfg_load=true 33 | fi 34 | 35 | if [ "$cfg_load" = true ] ; then 36 | # check if we are coming out of a self-induced reboot 37 | if [ -f /root/easy-wifi-switch-reboot ] ; then 38 | rm /root/easy-wifi-switch-reboot 39 | touch /root/easy-wifi-switch-finished 40 | 41 | echo "all done!" >> $log 42 | exit 0 43 | fi 44 | 45 | # check if the config settings are any different from the UCI ones 46 | my_ssid=`/sbin/uci "get" "wireless.@wifi-iface[0].ssid"` 47 | my_key=`/sbin/uci "get" "wireless.@wifi-iface[0].key"` 48 | my_enc=`/sbin/uci "get" "wireless.@wifi-iface[0].encryption"` 49 | 50 | if [ "$my_ssid" = "$ssid" ] && [ "$my_key" = "$key" ] && [ "$my_enc" = "$encryption" ] ; then 51 | echo "no change" >> $log 52 | exit 0 53 | fi 54 | 55 | # otherwise do the actual switching 56 | echo "Updating wifi settings:" > $log #note log reset 57 | echo " - SSID: $ssid" >> $log 58 | echo " - Key: $key" >> $log 59 | echo " - Encryption: $encryption" >> $log 60 | echo " " >> $log 61 | 62 | # remove the "success" file if it exists 63 | rm /root/easy-wifi-switch-finished 64 | 65 | # apply to OpenWrt UCI 66 | # WIRELESS: see http://wiki.openwrt.org/doc/uci/wireless 67 | $UCI "set" "wireless.@wifi-device[0].channel=auto" 68 | 69 | $UCI "set" "wireless.@wifi-iface[0].mode=sta" 70 | $UCI "set" "wireless.@wifi-iface[0].ssid=$ssid" 71 | $UCI "set" "wireless.@wifi-iface[0].encryption=$encryption" 72 | $UCI "set" "wireless.@wifi-iface[0].key=$key" 73 | 74 | # NETWORK 75 | $UCI "delete" "network.lan.ipaddr" 76 | $UCI "delete" "network.lan.netmask" 77 | 78 | PROTO=`$UCI get "arduino.lan.proto"` 79 | set_or_delete "network.lan.proto" $PROTO 80 | 81 | # DHCP 82 | $UCI "delete" "dhcp.@dnsmasq[0].interface" 83 | $UCI "add_list" "dhcp.@dnsmasq[0].interface=lo" 84 | 85 | # COMMIT 86 | $UCI commit 87 | 88 | # RESTART 89 | echo "rebooting" >> $log 90 | /bin/touch /root/easy-wifi-switch-reboot 91 | /sbin/reboot 92 | else 93 | echo "wifi.cfg was not found. Leaving wifi alone." >> $log 94 | fi 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yun Easy Wifi Switch 2 | ==================== 3 | 4 | A more user-friendly method for WiFi network toggling on Arduino Yun (or any OpenWrt device with removable storage). For when you want to give a Yun-based project to your non-tech-folk friends! 5 | 6 | It works by reading a wifi network's SSID, password and encryption method from a text file that is stored on a USB drive or MicroSD card. This file is accessed every time the Yun boots up and the WiFi settings are applied accordingly. If you need to change WiFi networks, you can take the USB drive or MicroSD card out of the Yun, plug it into a computer, edit the file, and place it back in the Yun. 7 | 8 | I have tested it switching between WiFi networks and switching from AP mode (where the Arduino broadcasts its own network) to a WiFi network. 9 | 10 | Read a more detailed write-up [on my blog](http://samjbrenner.com/notes/yun-easy-wifi-switch/). 11 | 12 | Installing 13 | ------- 14 | 15 | 1. Move `easy-wifi-switch` into your Yun's `/usr/bin/` directory. 16 | 2. Move `wifi.cfg` on to the root of a USB drive or MicroSD card and plug that into the Yun. I recommend USB due to its ubiquity. 17 | 3. In your Yun's `/etc/rc.local` file, add a call to `easy-wifi-switch` before the call to `wifi-live-or-reset`. If you haven't made changes to the stock `rc.local` file, you can use the one provided here. 18 | 19 | Using 20 | ------ 21 | 22 | 1. Remove the USB drive or MicroSD card from the Yun and plug it into a computer. 23 | 2. Open `wifi.cfg` in any text editor and update the SSID, password and encryption variables to the desired values. 24 | 3. Remove the USB drive from your computer and place it back into the Yun. 25 | 4. Reboot the Yun. When it boots up, it will apply the new settings. (Note that I have to reboot the Yun again after applying the settings, so it will take a bit longer to boot up. Thoughts about this removing this requirement are definitely welcome!) 26 | 27 | Background 28 | ------ 29 | 30 | This was created for my [Trophy of the Future](https://github.com/sambrenner/future-trophy), an internet-connected fantasy football trophy built with an Arduino Yun. Because the trophy will change hands every year, I needed a simple way for its recipients to configure it to their WiFi networks without having to connect to the Yun's network or depend on an ethernet connection. 31 | 32 | More Reading 33 | ------ 34 | 35 | * [Full Write-Up](http://samjbrenner.com/notes/yun-easy-wifi-switch/) 36 | * [OpenWrt Wireless Configuration Docs](http://wiki.openwrt.org/doc/uci/wireless) 37 | * [OpenWrt DHCP Configuration Docs](http://wiki.openwrt.org/doc/uci/dhcp) 38 | * [OpenWrt UCI Docs](http://wiki.openwrt.org/doc/uci) 39 | * [`wifi-live-or-reset`](https://github.com/arduino/linino/blob/master/trunk/package/linino/yun-scripts/files/usr/bin/wifi-live-or-reset) 40 | * [Trophy of the Future Documentation Part 1](http://samjbrenner.com/notes/making-the-worlds-first-internet-enabled-fantasy-football-trophy-part-1-fabrication/), [Part 2](http://samjbrenner.com/notes/making-the-worlds-first-internet-enabled-fantasy-football-trophy-part-2-programming/) 41 | --------------------------------------------------------------------------------