import network
import time
import ntptime
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
try:
print("Synchronizing time with NTP server...")
ntptime.settime()
print("Time synchronized with NTP server")
except Exception as e:
print("Error synchronizing time:", str(e))
ap_ssid = "ESPSCANNER"
ap_password = "esp@scan"
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ap_ssid, password=ap_password)
print(f'AP SSID: {ap_ssid}, Password: {ap_password}')
while True:
try:
scan_results = []
current_time_unix = time.mktime(time.localtime())
current_time_unix += 19800
current_time = time.localtime(current_time_unix)
scan_data = ap.scan()
print(scan_data)
for result in scan_data:
ssid, bssid, channel, RSSI, authmode, hidden = result
timestamp = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(
current_time[0], current_time[1], current_time[2],
current_time[3], current_time[4], current_time[5]
)
scan_results.append({
"timestamp": timestamp, # Add the timestamp
"ssid": ssid.decode("utf-8"), # Convert to string
"BSSID": ":".join("{:02X}".format(byte) for byte in bssid),
"channel": channel,
"RSSI": RSSI,
"auth_mode": authmode,
"hidden": hidden,
})
if len(scan_results) > 0:
print("WiFi Scan Results:")
for result in scan_results:
print(f"Timestamp (IST): {result['timestamp']}")
print(f"SSID: {result['ssid']}")
print(f"BSSID: {result['BSSID']}")
print(f"Channel: {result['channel']}")
print(f"RSSI: {result['RSSI']} dBm")
print(f"Auth Mode: {result['auth_mode']}")
print(f"Hidden: {result['hidden']}")
print()
time.sleep(2)
except Exception as e:
print("Reconnected to Wi-Fi")