Skip to content

Commit

Permalink
add ssid channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyal Cohen authored and Eyal Cohen committed Feb 2, 2021
1 parent 779ed2f commit 1e762be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ Name | Description | Default
---|---|---
DELAY | Number of seconds between scans | 600 (10 minutes)
WLAN | Interface to use | wlan0
SSID | SSID name |
MQTT_URL | Mqtt broker IP | 192.168.0.100
MQTT_TOPIC | Mqtt topic | wifi/scan

## Home assiatnt sensor

The application publish the number of discovered networks in the state field, and total number of networks per channel in the channels field (index 0 is not used, channel 1 starts from index 1)
The application publish the number of discovered networks in the state field, and total number of networks per channel in the channels field. The ssid channel will be set with the channle of the specified ssid (0 if not specified).

```
{"state":6,"channels":[0,0,0,0,0,0,0,4,0,0,0,2,0,0]}
{"state":6,"channels":[0,0,0,0,0,0,4,0,0,0,2,0,0]}
```

The following sensor can be used to track the number of networks in wifi channel 7:
Expand All @@ -51,8 +52,9 @@ The following sensor can be used to track the number of networks in wifi channel
sensor:
- platform: mqtt
name: Wifi channel 7
icon: mdi:wifi
state_topic: "wifi/scan"
value_template: "{{ value_json['channels'][7] }}"
value_template: "{{ value_json['channels'][6] }}"
```

## Wifi channels and frequency
Expand Down
11 changes: 9 additions & 2 deletions app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ mqtt_topic="${MQTT_TOPIC:-wifi/scan}"

while true
do
channels=$(iwlist $wlan scan | bash scan.sh)
result=$( iwlist $wlan scan | bash scan.sh )
for i in ${result[@]}; do
channels+=($i)
done

# Extract ssid channel and reset first item
ssid_channel=${channels[0]}
channels=("${channels[@]:1}")

# Total number of networks
total=0
for i in ${channels[@]}; do
let total+=$i
done

# Publish
message=`jo state=$total channels=$(jo -a ${channels[@]})`
message=`jo state=$total ssid_channel=$ssid_channel channels=$(jo -a ${channels[@]})`
mosquitto_pub -h $mqtt_url -t $mqtt_topic -m $message

# Wait before next scan
Expand Down
15 changes: 10 additions & 5 deletions scan.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Init networks detetced per channel (total 14 channels, chanel in index 0 is not used)
channels=(0 0 0 0 0 0 0 0 0 0 0 0 0 0)
# Init result (ssid channel, 13 channels)
result=(0 0 0 0 0 0 0 0 0 0 0 0 0 0)

# Scan and parse
while IFS= read -r line; do
Expand All @@ -19,11 +19,16 @@ while IFS= read -r line; do
[[ "$line" =~ Encrypt ]] && enc=${line##*key:}
[[ "$line" =~ ESSID ]] && {
essid=${line##*ID:}
# Accumlate networks onin channel
result[$chn]=$((result[$chn]+1));
# Set ssid channel (if specified)
if [[ "$essid" == "\"$SSID\"" ]]; then
result[0]=$chn
fi
# echo " $mac $essid $frq $chn $qual $lvl $enc" # output after ESSID
# Accumlate networks on channels
channels[$chn]=$((channels[$chn]+1))
}

done

# Return channels scan result
echo ${channels[@]}
echo "${result[@]}"

0 comments on commit 1e762be

Please sign in to comment.