File size: 2,702 Bytes
d4f8d36
 
 
 
 
 
29d7742
 
d4f8d36
29d7742
d4f8d36
29d7742
d4f8d36
 
 
 
 
29d7742
d4f8d36
 
 
 
 
 
 
 
29d7742
 
 
 
d4f8d36
29d7742
d4f8d36
29d7742
d4f8d36
29d7742
d4f8d36
 
 
29d7742
 
 
 
 
 
 
 
d4f8d36
29d7742
d4f8d36
29d7742
 
 
 
 
 
 
 
d4f8d36
29d7742
 
d4f8d36
29d7742
 
 
 
 
 
 
 
d4f8d36
29d7742
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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**.")

# Session state for data and tracking
if "tracking" not in st.session_state:
    st.session_state.tracking = False
if "data" not in st.session_state:
    st.session_state.data = []

# Start / Stop buttons
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

# Placeholders for real-time updates
bar_chart_placeholder = st.empty()
status_placeholder = st.empty()
best_placeholder = st.empty()

# Tracking loop
while st.session_state.tracking:
    signal = random.randint(-90, -30)  # Simulated dBm
    position = len(st.session_state.data) + 1
    st.session_state.data.append({"Position": position, "Signal": signal})

    df = pd.DataFrame(st.session_state.data)

    # Assign colors based on signal strength
    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)

    # Draw bar chart
    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)  # Consistent y-axis scale
    bar_chart_placeholder.pyplot(fig)

    # Status text
    status_placeholder.markdown(f"📍 **Current Signal: `{signal} dBm` at Position {position}**")

    # Best signal detection
    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)

# Show final plot if tracking stopped
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)