|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import matplotlib.pyplot as plt |
|
|
import random |
|
|
import time |
|
|
|
|
|
st.set_page_config(page_title="📶 WiFi Signal Analyzer", layout="centered") |
|
|
st.title("📡 Real-Time WiFi Signal Strength (Bar View)") |
|
|
|
|
|
st.markdown("Roam around and monitor your WiFi signal strength. Strong signals are shown in **green**, weak in **red**.") |
|
|
|
|
|
|
|
|
if "tracking" not in st.session_state: |
|
|
st.session_state.tracking = False |
|
|
if "data" not in st.session_state: |
|
|
st.session_state.data = [] |
|
|
|
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
with col1: |
|
|
if st.button("▶️ Start Tracking", disabled=st.session_state.tracking): |
|
|
st.session_state.tracking = True |
|
|
with col2: |
|
|
if st.button("⏹ Stop Tracking", disabled=not st.session_state.tracking): |
|
|
st.session_state.tracking = False |
|
|
|
|
|
|
|
|
bar_chart_placeholder = st.empty() |
|
|
status_placeholder = st.empty() |
|
|
best_placeholder = st.empty() |
|
|
|
|
|
|
|
|
while st.session_state.tracking: |
|
|
signal = random.randint(-90, -30) |
|
|
position = len(st.session_state.data) + 1 |
|
|
st.session_state.data.append({"Position": position, "Signal": signal}) |
|
|
|
|
|
df = pd.DataFrame(st.session_state.data) |
|
|
|
|
|
|
|
|
def color_by_strength(s): |
|
|
if s >= -60: |
|
|
return 'green' |
|
|
elif s >= -75: |
|
|
return 'orange' |
|
|
else: |
|
|
return 'red' |
|
|
|
|
|
bar_colors = df["Signal"].apply(color_by_strength) |
|
|
|
|
|
|
|
|
fig, ax = plt.subplots() |
|
|
ax.bar(df["Position"], df["Signal"], color=bar_colors) |
|
|
ax.set_xlabel("Position") |
|
|
ax.set_ylabel("Signal Strength (dBm)") |
|
|
ax.set_title("WiFi Signal Strength by Position") |
|
|
ax.set_ylim(-100, -20) |
|
|
bar_chart_placeholder.pyplot(fig) |
|
|
|
|
|
|
|
|
status_placeholder.markdown(f"📍 **Current Signal: `{signal} dBm` at Position {position}**") |
|
|
|
|
|
|
|
|
best = df[df["Signal"] == df["Signal"].max()].iloc[0] |
|
|
best_placeholder.markdown(f"✅ **Best Signal so far: `{best['Signal']} dBm` at Position {best['Position']}**") |
|
|
|
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
if not st.session_state.tracking and st.session_state.data: |
|
|
df = pd.DataFrame(st.session_state.data) |
|
|
bar_colors = df["Signal"].apply(lambda s: 'green' if s >= -60 else 'orange' if s >= -75 else 'red') |
|
|
fig, ax = plt.subplots() |
|
|
ax.bar(df["Position"], df["Signal"], color=bar_colors) |
|
|
ax.set_xlabel("Position") |
|
|
ax.set_ylabel("Signal Strength (dBm)") |
|
|
ax.set_title("Final WiFi Signal Chart") |
|
|
ax.set_ylim(-100, -20) |
|
|
st.pyplot(fig) |
|
|
|