Turn router into a smart WIFI (1): when someone goes home, it sends a text message to tell you that wifi
I want to turn my router into a smart WIFI. idea is like this: when my wife returns home, router will automatically send a text message to me.
After a few days of hard work, let's talk about it:
My vro model: vromini mini (129 yuan for Taobao on the official website)
Step 1: First enable SSH on the router to obtain the root
In this regard, is open and its operation methods are available on the Internet. For more information, see click to open the link.
Step 2: log in via SSH to learn what the router looks like
Enter the following command on your computer to log on to the router (my router IP address is 192.168.31.1)
ssh root@192.168.31.1
Enter the root password obtained in step 1 (if the password is correct, you will still see permission denied, leave it alone, and press Enter ).
BusyBox v1.19.4 (2015-01-22 17:52:04 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.
-----------------------------------------------------
Welcome to XiaoQiang!
-----------------------------------------------------
root@XiaoQiang:~#
Use df to view the storage status
root@XiaoQiang:~# df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 11008 11008 0 100% /
/dev/root 11008 11008 0 100% /
tmpfs 62872 2580 60292 4% /tmp
tmpfs 512 0 512 0% /dev
/dev/mtdblock7 1024 864 160 84% /data
/dev/mtdblock7 1024 864 160 84% /etc
tmpfs 62872 2580 60292 4% /userdisk/sysapihttpd
/dev/root 1024 864 160 84% /mnt
/dev/mtdblock7 1024 864 160 84% /mnt
/dev/sda1 976762580 297860468 678902112 30% /extdisks/sda1
The root directory of the router is read-only, only/etc/data can be written, and the remaining space is small (The table above shows only 160 K). Only small programs can be written.
If you need a large space, you can insert a USB flash drive or a mobile hard disk. As shown in the figure above:/dev/sda1, this is my mobile hard disk.
All directories placed on tmpfs (for example,/tmp/userdisk/sysapihttpd) are in temporary storage. If you write the files on it, all files will be lost after restart.
Check the Operating System
root@XiaoQiang:~# cat /etc/*release
DISTRIB_ID="OpenWrt"
DISTRIB_RELEASE="Attitude Adjustment"
DISTRIB_REVISION="r40348"
DISTRIB_CODENAME="attitude_adjustment"
DISTRIB_TARGET="ramips/mt7620a"
DISTRIB_DESCRIPTION="OpenWrt Attitude Adjustment 12.09.1"
The operating system is OpenWrt 12.09.1 and the chip is mt7620a.
OpenWrt is a linux
We recommend that you first go to various directories to familiarize yourself
Check which commands and programs can be used
Use help to view the built-in commands
root@XiaoQiang:~# help
Help returns a list of available commands
Check which programs are available
root@XiaoQiang:~# echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin
Most common programs are in these bin directories. Commonly used linux programs are generally available (such as grep awk wget find)
Step 3: Write a Shell script to check whether a mobile phone is connected to the vro
The router has two Wi-Fi SSID, one at a frequency of 2.4G Hz and the other at a frequency of 5G Hz.
The idea is as follows:
1. MAC is a sign for identifying mobile phones.
2. If the phone is connected to a Wi-Fi SSID, the WIFI will know the MAC code of the phone.
3. Check the current connection user of each SSID to see if there is a specified MAC code. If yes, use wget to access an external website URL and trigger a webpage.
4. This webpage triggers the SMS notification again. My mobile phone number is 189. I sent an email to my 189 Mobile Phone mailbox. Using the SMS notification function of my 189 email address, my mobile phone will receive a text message.
Use ifconfig to check several NICs.
root@XiaoQiang:~# ifconfig
br-lan Link encap:Ethernet HWaddr 64:09:80:18:7B:C0
...
eth0 Link encap:Ethernet HWaddr 64:09:80:18:7B:C0
...
pppoe-wan Link encap:Point-to-Point Protocol
...
wl0 Link encap:Ethernet HWaddr 64:09:80:18:7B:C2
inet6 addr: fe80::6609:80ff:fe18:7bc2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1003 errors:0 dropped:0 overruns:0 frame:0
TX packets:602 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:176410 (172.2 KiB) TX bytes:103339 (100.9 KiB)
Interrupt:13
wl1 Link encap:Ethernet HWaddr 64:09:80:18:7B:C1
inet6 addr: fe80::6609:80ff:fe18:7bc1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4042 errors:0 dropped:0 overruns:0 frame:0
TX packets:2064 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:869980 (849.5 KiB) TX bytes:832223 (812.7 KiB)
Interrupt:4
Wl0 and wl1 are two Wi-Fi NICs, each generating an SSID
Run the iwinfo command to check the number of connected users and MAC codes under a nic.
root@XiaoQiang:~# iwinfo wl1 assoclist
D0:33:AE:5F:63:AE -50 dBm / -95 dBm (SNR 45) 0 ms ago
RX: 1.0 MBit/s 0 Pkts.
TX: 65.0 MBit/s 0 Pkts.
7C:E1:D3:EA:81:D5 -57 dBm / -95 dBm (SNR 38) 0 ms ago
RX: 130.0 MBit/s 0 Pkts.
TX: 130.0 MBit/s 0 Pkts.
Each row contains the following information: D0: 33: AE: 5F: 63: AE. This is the MAC code, -50 dBm this is its signal strength (based on this value, we can determine the distance between the mobile phone and the vro and its changes)
Start to write a Shell script and save it to the/etc directory named smart_wifi.
root@XiaoQiang:~# cd /etc
root@XiaoQiang:/etc # vi smart_wifi
The script content is as follows:
#!/bin/shMAC="E0:19:1D:E4:22:25"URL="http://192.168.31.131/miwifi/find.php?mac="#check duration, in secondsinterval=2#To avoid notify continously, last_time is the last time we find the MAClast_time=$(date +%y%m%d%H%M%S)let last_time=last_time-interval-interval#Function: Find MAC address in associated users , query the url while matched. return 1 if found, return 0 if not foundfind_mac() { #Use ifconfig to find interface which name starts with wl ifconfig | grep wl[0-9] | awk '{print $1}' | while read WLAN do #Use iwinfo to find MAC address of connected users iwinfo $WLAN assoclist | grep dBm | awk '{print $1}' | while read MAC1 do #if MAC1 address is the target we want if [ $MAC1 = $MAC ] then #Calculate time passed since last_time this_time=$(date +%y%m%d%H%M%S) let time_passed=this_time-last_time let interval_min=interval+1 if [ $time_passed -gt $interval_min ] then #construct the url , append the MAC address to the end QUREY_URL="${URL}${MAC}" #Use wget to query the url wget -q -O web_response $QUREY_URL echo "FIND $MAC" return 1 fi fi done done return 0}#Main program: it's a dead loop, exec find_mac() every n seconds(defined by interval)while [ 1 -lt 2 ]do if find_mac then #if found, update last_time last_time=$(date +%y%m%d%H%M%S) fi sleep $interval done
MAC is the MAC address (that is, the MAC code of the old lady's mobile phone)
A URL is an external website URL. The actual MAC address is used as a parameter during access to the website.
The URL in the script is LAN, which can be used on the Internet.
Script annotation in English
The program uses the round-robin system and finds it every other time.
The main program runs the find_mac function once every interval seconds,
The interval value defines the number of seconds to query.
The find_mac () function is a function used to find the MAC code. If the current WIFI user finds the MAC code, then the access URL (the URL will send a text message), the return value is 1. If not found, return 0
If a mobile phone keeps connected to WIFI, it can be found every time it is searched. If it keeps sending text messages, it is annoying. Therefore, the program uses a variable last_time to record the last time the MAC code was found, if it is found consecutively, the webpage is not triggered.
I don't have much to explain. Let's take a look.
To make smart_wifi run, do not forget to grant it the permission to run. The command is as follows:
root@XiaoQiang:/etc # chmod +x smart_wifi
Step 4: Write a webpage (URL) for receiving and sending text messages (I use PHP)
<?phpif (isset($_GET['mac'])) {mail("189XXXXXXXX@189.cn","Warning: Your wife is home.", "You know what to do");echo "ok";} else echo "error";?>
Implementation of text message: my mobile phone is 189, and the program sends an email to my 189 Mobile Phone mailbox, using the SMS notification function of 189 mailbox, my mobile phone will receive a text message with the following content:
Warning: Your wife is home.
Step 5: run the Shell script to check the actual effect.
Return to SSH on the router and run the following command:
root@XiaoQiang:~# /etc/smart_wifi &
In the command, the "&" character is used to run it in the background.
To stop the script running, run the ps | grep smart_wifi command to view the process and kill it.
Running Effect: OK. When my wife appears and connects to WIFI, my mobile phone will receive a text message.
A little smart WIFI!
Summary:
1. In fact, many vrouters use OpenWrt. This script can be run at the same time, not limited to vrouters.
2. As long as you have imagination, this script can do anything else. For example, when you enter the door, the light will automatically turn on. As soon as you are close to the TV, the TV is on.
3. In the preceding example, If you restart the vro, the script will not start automatically. In practical use, you should write another service and start it on your own (in this case, I will not talk about it anymore ...)
Refer:
Http://wiki.openwrt.org/doc/techref/start