Last active
August 14, 2023 23:41
-
-
Save mshuffett/c38261e86a0fa04d98911004fea5ad8a to your computer and use it in GitHub Desktop.
Wifi Signal Strength
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import matplotlib.pyplot as plt | |
import threading | |
# Use a nicer style for the plot | |
plt.style.use('seaborn-darkgrid') | |
def get_wifi_data(): | |
# Obtain WiFi data using airport command | |
result = subprocess.check_output(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I']).decode('utf-8') | |
rssi = int([line.split(":")[1].strip() for line in result.split('\n') if "agrCtlRSSI" in line][0]) | |
noise = int([line.split(":")[1].strip() for line in result.split('\n') if "agrCtlNoise" in line][0]) | |
return rssi, noise, rssi-noise | |
def plotter(): | |
i = 0 | |
while True: | |
rssi, noise, snr = get_wifi_data() | |
x.append(i) | |
y_rssi.append(rssi) | |
y_noise.append(noise) | |
y_snr.append(snr) | |
line_rssi.set_xdata(x) | |
line_rssi.set_ydata(y_rssi) | |
line_noise.set_xdata(x) | |
line_noise.set_ydata(y_noise) | |
line_snr.set_xdata(x) | |
line_snr.set_ydata(y_snr) | |
# Draw vertical line if a room change is detected | |
if room_changes.get(i, None): | |
ax_rssi.axvline(x=i, color='gray', linestyle='--') | |
ax_noise.axvline(x=i, color='gray', linestyle='--') | |
ax_snr.axvline(x=i, color='gray', linestyle='--') | |
# Annotate the room name by the line on the rssi graph | |
ax_rssi.annotate(room_changes[i], (i, ax_rssi.get_ylim()[1]), textcoords="offset points", xytext=(0,5), ha='center', fontsize=8, color='gray', va='bottom') | |
# Update the axes to fit all data and redraw | |
for ax in [ax_rssi, ax_noise, ax_snr]: | |
ax.relim() | |
ax.autoscale_view() | |
# Adjust x-axis to show all data points | |
if x: | |
ax.set_xlim([0, x[-1]]) | |
# Y-axis settings | |
if ax == ax_rssi: | |
y_min = min(-60, -70, min(y_rssi)) # Include thresholds -60 and -70 | |
y_max = max(-60, -70, max(y_rssi)) | |
ax.set_ylim(y_min, y_max) | |
elif ax == ax_noise: | |
ax.set_ylim(min(y_noise), max(y_noise)) | |
elif ax == ax_snr: | |
y_min = min(15, 25, min(y_snr)) # Include thresholds 15 and 25 | |
y_max = max(15, 25, max(y_snr)) | |
ax.set_ylim(y_min, y_max) | |
plt.draw() | |
plt.pause(1) | |
i += 1 | |
# Initial Setup for live graph | |
plt.ion() | |
fig, (ax_rssi, ax_noise, ax_snr) = plt.subplots(3, 1, sharex=True) | |
x, y_rssi, y_noise, y_snr = [], [], [], [] | |
line_rssi, = ax_rssi.plot(x, y_rssi, color='blue', label="RSSI") | |
line_noise, = ax_noise.plot(x, y_noise, color='green', label="Noise") | |
line_snr, = ax_snr.plot(x, y_snr, color='red', label="SNR") | |
# Initial axis settings and quality lines | |
for ax in [ax_rssi, ax_noise, ax_snr]: | |
ax.legend() | |
ax.set_xlim(0, 10) | |
ax.set_ylim(-100, 100) | |
# Coloring based on thresholds | |
ax_rssi.axhspan(-60, ax_rssi.get_ylim()[1], color='green', alpha=0.1, label="Good RSSI Zone") | |
ax_rssi.axhspan(-70, -60, color='yellow', alpha=0.1, label="Fair RSSI Zone") | |
ax_rssi.axhspan(ax_rssi.get_ylim()[0], -70, color='red', alpha=0.1, label="Poor RSSI Zone") | |
ax_snr.axhspan(25, ax_snr.get_ylim()[1], color='green', alpha=0.1, label="Excellent SNR Zone") | |
ax_snr.axhspan(15, 25, color='yellow', alpha=0.1, label="Good SNR Zone") | |
ax_snr.axhspan(ax_snr.get_ylim()[0], 15, color='red', alpha=0.1, label="Poor SNR Zone") | |
ax_rssi.set_ylabel('dBm') | |
ax_noise.set_ylabel('dBm') | |
ax_snr.set_ylabel('dB') | |
# To track room changes | |
room_changes = {} | |
def listen_for_input(): | |
global room_changes | |
while True: | |
room = input("Enter room name: ") | |
if room: | |
room_changes[len(x)] = room | |
# Starting the listening thread | |
threading.Thread(target=listen_for_input).start() | |
# Start the plotting | |
plotter() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment