wifi_scan.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import subprocess
  2. import csv
  3. import time
  4. def get_wifi_signal():
  5. scan_output = subprocess.check_output(['sudo', 'iwlist', 'wlan0', 'scan']).decode('utf-8')
  6. return scan_output
  7. def parse_signal_strength(scan_output):
  8. signal_strengths = []
  9. for line in scan_output.split('\n'):
  10. if "Signal level=" in line:
  11. strength = line.split("Signal level=")[1].split(" ")[0]
  12. signal_strengths.append(int(strength))
  13. return signal_strengths
  14. def collect_data(file_path, location_id):
  15. with open(file_path, 'a', newline='') as csvfile:
  16. fieldnames = ['Location', 'SignalStrength']
  17. writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  18. if csvfile.tell() == 0:
  19. writer.writeheader()
  20. scan_output = get_wifi_signal()
  21. signal_strengths = parse_signal_strength(scan_output)
  22. for strength in signal_strengths:
  23. writer.writerow({'Location': location_id, 'SignalStrength': strength})
  24. # Example usage
  25. location_id = 'Showcase1' # Change to unique ID for each location
  26. file_path = 'wifi_signals.csv'
  27. while True:
  28. collect_data(file_path, location_id)
  29. time.sleep(60) # Collect data every minute