Skip to content

Instantly share code, notes, and snippets.

@muhammaddadu
Created June 29, 2016 05:41
Show Gist options
  • Save muhammaddadu/ba3cc738e8e8b1fe66764f8ba0a6a65d to your computer and use it in GitHub Desktop.
Save muhammaddadu/ba3cc738e8e8b1fe66764f8ba0a6a65d to your computer and use it in GitHub Desktop.
Auto scan and set unused Wifi channel for Phantom 3 Standard / 4K
#!/bin/sh
#Auto scan and set unused Wifi channel for Phantom 3 Standard / 4K
#Author : Gary Kan
#Date : 20/06/2016
#Version 1.1
# 1) Add new user argument option
# -ch for set channel number, eg. -ch 13
# -booster will limit RC tx power to 20DBM, to protect your external booster
# 2) Add Restart udhcp and apsvr
# 3) Some Minor tweak
#
#Date : 21/06/2016
#Version1.2
# 1) Remove user argument option -booster
# 2) Remove apsvr due to my RC video link sometime get disconnected
# 4) Change Default DBM from 27dbm to 24 dbm on RC and drone, due to user report his RC is bricked despite of successful application of 27dbm setting, after fifth flight.
# 3) Add new user argument option
# -rc will limit RC tx power to your target mbm, eg -rc 2000 = 20DBm, Max = 2700
# -d will limit DRONE tx power to your target mbm, eg -d 2000 = 20DBm, Max = 2700
#
clear
DRONE="192.168.1.2"
###For check User argument
while [ "$1" != "" ]; do
case $1 in
-ch)
shift
SCAN="OFF"
CHANNEL=$1
;;
-rc)
shift
RC="Y"
RCDBM=$1
echo "Info : Limit RC TX Power to $1"
;;
-d)
shift
D="Y"
DDBM=$1
echo "Info : Limit Drone TX Power to $1"
;;
-h)
echo "-ch for set channel number, eg. -ch 13"
echo "-rc will limit RC tx power to your target mbm, eg -rc 2000 = 20DBm, Option = 1700 2000 2400 2700"
echo "-d will limit DRONE tx power to your target mbm, eg -d 2000 = 20DBm, Option = 1700 2000 2400 2700"
echo "-h for show this page"
exit
;;
* )
echo "Incorrect user argument !!"
echo "-ch for set channel number, eg. -ch 13"
echo "-rc will limit RC tx power to your target mbm, eg -rc 2000 = 20DBm, Option = 1700 2000 2400 2700"
echo "-d will limit DRONE tx power to your target mbm, eg -d 2000 = 20DBm, Option = 1700 2000 2400 2700"
echo "-h for show this page"
exit 1
;;
esac
shift
done
###Make Sure network is ready and Drone side is online
echo "Checking Connection with Phantom 3 Standard / 4K . . ."
ping -c 1 -w 60 $DRONE &> /dev/null
if [ $? -ne 0 ]
then
echo "ERROR : Phantom 3 Standard / 4K at $DRONE is unreachable"
exit 1
else
echo "Connection - OK."
fi
###Check unused channel or user argument
if [ "$SCAN" != "OFF" ]
then
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13
do
iw dev wlan0 scan | grep "DS Parameter set:" | awk '{FS=" "} {print $5}' | sort -n | grep -x $i &>/dev/null
if [ $? -ne 0 ]
then
echo "Channel $i - Not In Use"
echo "Set CH to $i"
CH=$i
break
else
echo "Channel $i - In Use"
if [ $i = 13 ]
then
CH=$i
fi
fi
done
else
echo "User Input Channel = $CHANNEL"
case $CHANNEL in
1|2|3|4|5|6|7|8|9|10|11|12|13)
CH=$CHANNEL
;;
*)
echo "Input Error : Channel must be 1 to 13 !!"
exit 1
;;
esac
fi
###Setup hostapd-phy0.conf with target channel number
echo "Trying To Set Channel = $CH . . ."
sed -i "/channel=/c\channel=$CH" /tmp/run/hostapd-phy0.conf
echo "Patched `grep "channel" /tmp/run/hostapd-phy0.conf`"
echo " "
###patch drone side to BO, make sure ready for CH12,13, telnet.d must be on in rcS, scan be remove is user set country = BO in rcS at drone side
echo "Checking Connection with Phantom 3 Standard / 4K . . ."
ping -c 1 -w 30 $DRONE &> /dev/null
if [ $? -ne 0 ]
then
echo "ERROR : Phantom 3 Standard / 4K at $DRONE is unreachable"
exit 1
else
echo "Connected . . ."
echo "Patching Phantom 3 Standard / 4K at $DRONE . . ."
if [ "$D" = "Y" ]
then
{ sleep 1;echo "iw reg get";echo "iw reg set BO";echo "iw reg get";echo "iw dev wlan0 set txpower limit $DDBM";sleep 1;} | telnet $DRONE
else
{ sleep 1;echo "iw reg get";echo "iw reg set BO";echo "iw reg get";echo "iw dev wlan0 set txpower limit 2400";sleep 1;} | telnet $DRONE
fi
fi
###Patch RC Side and restart network
echo ""
echo "Patching Phantom 3 Standard / 4K - RC . . ."
iw reg set BO
if [ "$RC" = "Y" ]
then
echo "Info : Limit RC TX Power to $RCDBM"
iw dev wlan0 set txpower limit $RCDBM
else
iw dev wlan0 set txpower limit 2400
fi
sleep 1
echo ""
echo "Restarting Services, it may take a moment . . ."
kill -9 `ps | grep udhcpd | grep -v grep | awk '{print $1}'`
sleep 3
kill -9 `cat /tmp/run/wifi-phy0.pid`
sleep 3
#kill -9 `ps | grep apsrv | grep -v grep | awk '{print $1}'`
#sleep 5
/usr/sbin/hostapd -P /var/run/wifi-phy0.pid -B /var/run/hostapd-phy0.conf
sleep 3
#apsrv &
#sleep 3
/usr/sbin/udhcpd -S /etc/udhcpd.conf &
sleep 3
###Final Checking, can be remove
echo ""
echo "Listing Services . . ."
ps | grep hostapd | grep -v grep
ps | grep apsrv | grep -v grep
ps | grep udhcpd | grep -v grep
echo ""
echo "Checking Final Result at $DRONE . . ."
ping -c 1 -w 60 $DRONE &> /dev/null
if [ $? -ne 0 ]
then
echo "ERROR : Phantom 3 Standard / 4K at $DRONE is unreachable"
exit 1
else
{ sleep 1;echo "iwinfo";sleep 1;} | telnet $DRONE
echo ""
echo "Checking Final Result at RC . . ."
iwinfo
echo "Patch Completed, Enjoy."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment