phase_1_-_the_wifi_analyzer_software
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| phase_1_-_the_wifi_analyzer_software [2026/08/20 16:21] – [Introduction] walkeradmin | phase_1_-_the_wifi_analyzer_software [2026/08/20 16:52] (current) – [Wifi Analyzer Software] walkeradmin | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| \\ | \\ | ||
| {{ : | {{ : | ||
| + | Back to [[pico_wifi_analyzer_-_phase_1|Pico Wifi Analyzer Phase 1]] | ||
| ---- | ---- | ||
| ===== Introduction ===== | ===== Introduction ===== | ||
| \\ | \\ | ||
| When you did the section where you connected up the LCD to the Pico, there was some simple test Python scripts to see that the LCD works. Also, you have tested your Wifi connection to see that you can talk to your router. Remember, the Pico only supports 2.4Ghz Wifi. | When you did the section where you connected up the LCD to the Pico, there was some simple test Python scripts to see that the LCD works. Also, you have tested your Wifi connection to see that you can talk to your router. Remember, the Pico only supports 2.4Ghz Wifi. | ||
| + | \\ | ||
| + | \\ | ||
| + | Here is the Python code you need to save to your Pico. To start, I would just copy this code in to Thonny and run it from there. | ||
| + | |||
| + | < | ||
| + | # ---------------------------------------------------- | ||
| + | # Wifi Analyzer | ||
| + | # Scans Wifi SSIDs every 15 minutes to keep track of | ||
| + | # current Wifi status. | ||
| + | # Alan Walker & CoPilot | ||
| + | # 19/08/2026 | ||
| + | # ---------------------------------------------------- | ||
| + | |||
| + | from machine import Pin, I2C | ||
| + | import ssd1306 | ||
| + | import time | ||
| + | import network | ||
| + | import socket | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # OLED Setup | ||
| + | # ---------------------------------------------------- | ||
| + | i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000) | ||
| + | lcd = ssd1306.SSD1306_I2C(128, | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Manual Button (GP16) | ||
| + | # ---------------------------------------------------- | ||
| + | button = Pin(16, Pin.IN, Pin.PULL_UP) | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Onboard LED (Pico 2 W) | ||
| + | # ---------------------------------------------------- | ||
| + | led = Pin(" | ||
| + | |||
| + | # Global WiFi status (updated after each scan) | ||
| + | wifi_ok = True | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # LED Controller (continuous blink if ANY SSID down) | ||
| + | # ---------------------------------------------------- | ||
| + | def update_led(): | ||
| + | global wifi_ok | ||
| + | |||
| + | if wifi_ok: | ||
| + | led.value(1) | ||
| + | else: | ||
| + | # continuous blink (non-blocking) | ||
| + | led.value(1) | ||
| + | time.sleep(0.15) | ||
| + | led.value(0) | ||
| + | time.sleep(0.15) | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # UI Renderer | ||
| + | # ---------------------------------------------------- | ||
| + | def draw_ui(header=" | ||
| + | linksys=" | ||
| + | status=" | ||
| + | |||
| + | lcd.fill(0) | ||
| + | |||
| + | lcd.text(header, | ||
| + | lcd.text(" | ||
| + | |||
| + | lcd.text(" | ||
| + | lcd.text(" | ||
| + | |||
| + | lcd.text(" | ||
| + | lcd.text(" | ||
| + | |||
| + | lcd.show() | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Scanning Animation | ||
| + | # ---------------------------------------------------- | ||
| + | def animate_scanning(): | ||
| + | frames = [" | ||
| + | for frame in frames: | ||
| + | draw_ui(status=frame) | ||
| + | update_led() | ||
| + | time.sleep(0.3) | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # WiFi Test Engine | ||
| + | # ---------------------------------------------------- | ||
| + | def test_network(ssid, | ||
| + | print(" | ||
| + | |||
| + | wlan = network.WLAN(network.STA_IF) | ||
| + | |||
| + | wlan.active(False) | ||
| + | time.sleep(0.2) | ||
| + | wlan.active(True) | ||
| + | |||
| + | wlan.connect(ssid, | ||
| + | |||
| + | for _ in range(20): | ||
| + | if wlan.isconnected(): | ||
| + | break | ||
| + | time.sleep(0.5) | ||
| + | |||
| + | if not wlan.isconnected(): | ||
| + | return False | ||
| + | |||
| + | try: | ||
| + | socket.getaddrinfo(" | ||
| + | return True | ||
| + | except: | ||
| + | return False | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Test SSIDs | ||
| + | # ---------------------------------------------------- | ||
| + | networks = [ | ||
| + | (" | ||
| + | (" | ||
| + | ] | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Rolling History | ||
| + | # ---------------------------------------------------- | ||
| + | history = { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Startup Scan | ||
| + | # ---------------------------------------------------- | ||
| + | animate_scanning() | ||
| + | print(" | ||
| + | |||
| + | time.sleep(1) | ||
| + | |||
| + | results = [] | ||
| + | for ssid, pw in networks: | ||
| + | ok = test_network(ssid, | ||
| + | results.append(ok) | ||
| + | |||
| + | # Update global WiFi status | ||
| + | wifi_ok = all(results) | ||
| + | |||
| + | linksys_status = " | ||
| + | draytek_status = " | ||
| + | |||
| + | history[" | ||
| + | history[" | ||
| + | |||
| + | if len(history[" | ||
| + | history[" | ||
| + | if len(history[" | ||
| + | history[" | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Time Sync | ||
| + | # ---------------------------------------------------- | ||
| + | if any(results): | ||
| + | try: | ||
| + | import ntptime | ||
| + | ntptime.settime() | ||
| + | now = time.localtime(time.time() + 3600) | ||
| + | except: | ||
| + | now = time.localtime() | ||
| + | else: | ||
| + | now = time.localtime() | ||
| + | |||
| + | network.WLAN(network.STA_IF).disconnect() | ||
| + | |||
| + | timestamp = " | ||
| + | |||
| + | linksys_hist = "" | ||
| + | draytek_hist = "" | ||
| + | |||
| + | draw_ui( | ||
| + | last=timestamp, | ||
| + | linksys=linksys_hist, | ||
| + | draytek=draytek_hist, | ||
| + | status=" | ||
| + | next_scan=" | ||
| + | ) | ||
| + | |||
| + | # ---------------------------------------------------- | ||
| + | # Auto-scan loop | ||
| + | # ---------------------------------------------------- | ||
| + | next_scan_seconds = 900 | ||
| + | scanning_in_progress = False | ||
| + | |||
| + | while True: | ||
| + | |||
| + | for _ in range(600): | ||
| + | |||
| + | update_led() | ||
| + | |||
| + | if not scanning_in_progress and not button.value(): | ||
| + | scanning_in_progress = True | ||
| + | animate_scanning() | ||
| + | print(" | ||
| + | |||
| + | results = [] | ||
| + | for ssid, pw in networks: | ||
| + | ok = test_network(ssid, | ||
| + | results.append(ok) | ||
| + | |||
| + | wifi_ok = all(results) | ||
| + | |||
| + | linksys_status = " | ||
| + | draytek_status = " | ||
| + | |||
| + | history[" | ||
| + | history[" | ||
| + | |||
| + | if len(history[" | ||
| + | history[" | ||
| + | if len(history[" | ||
| + | history[" | ||
| + | |||
| + | if any(results): | ||
| + | try: | ||
| + | import ntptime | ||
| + | ntptime.settime() | ||
| + | now = time.localtime(time.time() + 3600) | ||
| + | except: | ||
| + | now = time.localtime() | ||
| + | else: | ||
| + | now = time.localtime() | ||
| + | |||
| + | network.WLAN(network.STA_IF).disconnect() | ||
| + | |||
| + | timestamp = " | ||
| + | |||
| + | linksys_hist = "" | ||
| + | draytek_hist = "" | ||
| + | |||
| + | draw_ui( | ||
| + | last=timestamp, | ||
| + | linksys=linksys_hist, | ||
| + | draytek=draytek_hist, | ||
| + | status=" | ||
| + | next_scan=f" | ||
| + | ) | ||
| + | |||
| + | scanning_in_progress = False | ||
| + | time.sleep(0.3) | ||
| + | |||
| + | time.sleep(0.1) | ||
| + | |||
| + | next_scan_seconds -= 60 | ||
| + | |||
| + | update_led() | ||
| + | |||
| + | mins = next_scan_seconds // 60 | ||
| + | |||
| + | draw_ui( | ||
| + | last=timestamp, | ||
| + | linksys=linksys_hist, | ||
| + | draytek=draytek_hist, | ||
| + | status=" | ||
| + | next_scan=f" | ||
| + | ) | ||
| + | |||
| + | if next_scan_seconds <= 0: | ||
| + | |||
| + | scanning_in_progress = True | ||
| + | animate_scanning() | ||
| + | print(" | ||
| + | |||
| + | results = [] | ||
| + | for ssid, pw in networks: | ||
| + | ok = test_network(ssid, | ||
| + | results.append(ok) | ||
| + | |||
| + | wifi_ok = all(results) | ||
| + | |||
| + | linksys_status = " | ||
| + | draytek_status = " | ||
| + | |||
| + | history[" | ||
| + | history[" | ||
| + | |||
| + | if len(history[" | ||
| + | history[" | ||
| + | if len(history[" | ||
| + | history[" | ||
| + | |||
| + | if any(results): | ||
| + | try: | ||
| + | import ntptime | ||
| + | ntptime.settime() | ||
| + | now = time.localtime(time.time() + 3600) | ||
| + | except: | ||
| + | now = time.localtime() | ||
| + | else: | ||
| + | now = time.localtime() | ||
| + | |||
| + | network.WLAN(network.STA_IF).disconnect() | ||
| + | |||
| + | timestamp = " | ||
| + | |||
| + | linksys_hist = "" | ||
| + | draytek_hist = "" | ||
| + | |||
| + | draw_ui( | ||
| + | last=timestamp, | ||
| + | linksys=linksys_hist, | ||
| + | draytek=draytek_hist, | ||
| + | status=" | ||
| + | next_scan=" | ||
| + | ) | ||
| + | next_scan_seconds = 900 | ||
| + | scanning_in_progress = False | ||
| + | </ | ||
phase_1_-_the_wifi_analyzer_software.1787242869.txt.gz · Last modified: by walkeradmin
