Skip to content

Commit 3e6bb9e

Browse files
committed
hotspot - testing
1 parent e6a86c2 commit 3e6bb9e

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

scripts/hotspot.sh

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,58 @@
1212
#Other setup required find out more at
1313
#http://www.raspberryconnect.com
1414

15-
CreateHotSpot()
15+
CleanUp()
1616
{
17-
echo "Killing wifi client"
17+
systemctl stop hostapd
18+
systemctl stop dnsmasq
1819
wpa_cli terminate ${WLAN_INTERFACE} >/dev/null 2>&1
19-
echo "Creating HotSpot"
20-
systemctl stop dnsmasq > /dev/null 2>&1
21-
systemctl stop hostapd > /dev/null 2>&1
20+
ip addr flush ${WLAN_INTERFACE}
2221
ifconfig ${WLAN_INTERFACE} down
23-
ifconfig ${WLAN_INTERFACE} up
2422
ip link set dev ${WLAN_INTERFACE} down
23+
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
24+
}
25+
26+
CreateHotSpot()
27+
{
28+
CleanUp
29+
echo "Creating HotSpot"
2530
ip a add 10.0.34.1/24 brd + dev ${WLAN_INTERFACE}
2631
ip link set dev ${WLAN_INTERFACE} up
2732
dhcpcd -k ${WLAN_INTERFACE} >/dev/null 2>&1
2833
systemctl start dnsmasq
2934
systemctl start hostapd
3035
}
3136

32-
KillHotSpot()
37+
ConnectToWifi()
3338
{
34-
echo "Shutting Down HotSpot"
35-
ip link set dev ${WLAN_INTERFACE} down
36-
ifconfig ${WLAN_INTERFACE} down
39+
CleanUp
40+
echo "Starting WiFi connection"
3741
ifconfig ${WLAN_INTERFACE} up
38-
39-
systemctl stop hostapd
40-
systemctl stop dnsmasq
41-
ip addr flush dev ${WLAN_INTERFACE}
4242
ip link set dev ${WLAN_INTERFACE} up
4343
dhcpcd -n ${WLAN_INTERFACE} >/dev/null 2>&1
44-
}
45-
46-
CreateWifiClient()
47-
{
48-
echo "Starting WiFi connection"
4944
wpa_supplicant -B -i ${WLAN_INTERFACE} -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
5045
echo "Waiting 20 seconds"
5146
sleep 20 #give time for connection to be completed to router
47+
return IsWifiConnected
5248
}
5349

54-
IsWifiDisconnected()
50+
IsWifiConnected()
5551
{
5652
wpa_cli -i ${WLAN_INTERFACE} status | grep 'ip_address' >/dev/null 2>&1
5753
return $?
5854
}
5955

6056
areKnownNetworksNearBy()
6157
{
62-
SSID_LIST=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//')
63-
IFS=","
58+
SSID_REGISTERED=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//')
6459

65-
SSID_REPLY=`iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:"`
60+
SSID_AVAILABLE=`iw dev ${WLAN_INTERFACE} scan ap-force | grep "SSID:" | cut -d ' ' -f 2`
6661

67-
for SSID in ${SSID_LIST}
62+
for SSID in ${SSID_REGISTERED};
6863
do
69-
SSID_CLEAN=$(echo ${SSID} | tr -d '\r')
70-
echo ${SSID_REPLY} | grep ${SSID_CLEAN} > /dev/null 2>&1
71-
if [[ $? ]]
72-
then
73-
return 0
74-
fi
64+
[[ $SSID_AVAILABLE =~ (^|[[:space:]])$SSID($|[[:space:]]) ]] && return 0
7565
done
66+
7667
return 1
7768
}
7869

@@ -82,55 +73,61 @@ ACTIVE_MODE=
8273
while true;
8374
do
8475
if [[ ${ACTIVE_MODE} == "ap" ]]; then
85-
echo "Running in access point mode, next check in 300 seconds"
8676
sleep 300
87-
if [[ areKnownNetworksNearBy ]]; then
77+
if areKnownNetworksNearBy; then
78+
systemctl stop omnipy-beacon.service
8879
systemctl stop omnipy.service
89-
KillHotSpot
90-
echo "Hotspot Deactivated, Bringing Wifi Up"
80+
echo "Known networks are nearby, deactivating hotspot and connecting to wi-fi"
9181
CreateWifiClient
92-
if [[ IsWifiDisconnected ]]; then
82+
if ! IsWifiConnected; then
9383
echo "Failed to connect to wifi, going back into hotspot mode"
9484
CreateHotSpot
9585
ACTIVE_MODE="ap"
9686
else
9787
ACTIVE_MODE="client"
9888
fi
89+
systemctl restart omnipy-beacon.service
90+
systemctl stop omnipy.service
9991
systemctl start omnipy.service
92+
10093
fi
10194
elif [[ ${ACTIVE_MODE} == "client" ]]; then
102-
echo "Running in wi-fi client mode, next check in 60 seconds"
10395
sleep 60
104-
if [[ IsWifiDisconnected ]]; then
105-
systemctl stop omnipy.service
96+
if ! IsWifiConnected; then
10697
echo "Wi-fi disconnected, retrying"
10798
CreateWifiClient
108-
if [[ IsWifiDisconnected ]]; then
99+
if ! IsWifiConnected; then
109100
echo "No wi-fi connection, creating hot-spot"
110101
CreateHotSpot
111102
ACTIVE_MODE="ap"
112103
else
113104
echo "Wi-fi connection re-established"
114105
fi
106+
systemctl restart omnipy-beacon.service
107+
systemctl stop omnipy.service
115108
systemctl start omnipy.service
109+
116110
fi
117111
else
118112
echo "Checking current network state"
119-
if [[ IsWifiDisconnected ]]; then
113+
if ! IsWifiConnected; then
120114
echo "Wi-fi not connected, scanning"
121-
if [[ areKnownNetworksNearBy ]]; then
115+
if areKnownNetworksNearBy; then
122116
echo "Found known networks, will try to connect"
123-
KillHotSpot
124117
CreateWifiClient
125118
fi
126119
fi
127120

128-
if [[ IsWifiDisconnected ]]; then
121+
if ! IsWifiConnected; then
129122
echo "No wi-fi connection, creating hotspot"
130123
CreateHotSpot
124+
systemctl restart omnipy-beacon.service
125+
systemctl stop omnipy.service
126+
systemctl start omnipy.service
131127
ACTIVE_MODE="ap"
132128
else
129+
echo "Wi-fi is connected"
133130
ACTIVE_MODE="client"
134131
fi
135132
fi
136-
done
133+
done

scripts/omnipy.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ExecStart=/usr/bin/python3 -u /home/pi/omnipy/restapi.py
77
WorkingDirectory=/home/pi/omnipy
88
StandardOutput=inherit
99
StandardError=inherit
10-
TimeoutStopSec=90
10+
TimeoutStopSec=240
1111
Restart=on-abort
1212
User=pi
1313

scripts/pi-update-finalize.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ sudo cp /home/pi/omnipy/scripts/omnipy-hotspot.service /etc/systemd/system/
3939
sudo systemctl enable omnipy.service
4040
sudo systemctl enable omnipy-beacon.service
4141
sudo systemctl enable omnipy-pan.service
42-
sudo systemctl enable omnipy-hotspot.service
43-
sudo systemctl start omnipy-hotspot.service
42+
sudo systemctl disable omnipy-hotspot.service
4443
sudo systemctl start omnipy.service
4544
sudo systemctl start omnipy-beacon.service
4645
sudo systemctl start omnipy-pan.service

scripts/pi-update.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ git pull
1818

1919
echo
2020
echo "Stopping omnipy services"
21-
sudo systemctl disable omnipy.service
22-
sudo systemctl disable omnipy-beacon.service
23-
sudo systemctl disable omnipy-pan.service
2421
sudo systemctl stop omnipy.service
2522
sudo systemctl stop omnipy-beacon.service
2623
sudo systemctl stop omnipy-pan.service
24+
sudo systemctl stop omnipy-hotspot.service
2725

2826

2927
/bin/bash /home/pi/omnipy/scripts/pi-update-finalize.sh

scripts/recovery.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ if [[ -f ${BT_RESET_FILE} ]]; then
3737
/bin/rm ${BT_RESET_FILE}
3838
fi
3939

40+
if [[ -f ${HOT_SPOT_FILE} ]]; then
41+
systemctl start omnipy-hotspot.service
42+
fi
43+
4044
#if [[ -f ${HOT_SPOT_FILE} ]]; then
4145
#
4246
# mkdir -p /home/pi/omnipy/data

0 commit comments

Comments
 (0)