Sorry Dave, something is being lost in translation for me.
(error)
exit code: 1, File "wifiscan5.py", line 25
print(f"Network: {ssid}, Frequency: {frequency}, Signal Level: {signal_level}")
^
SyntaxError: invalid syntax
Code:
import json
import subprocess
import re
def scan_wifi():
# Run the iwlist command to scan for wireless networks
cmd = subprocess.Popen(['sudo', 'iwlist', 'wlan0', 'scan'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, _ = cmd.communicate()
# Decode the output from bytes to string
output = output.decode()
# Extract the relevant information using regular expressions
networks = re.findall(r'ESSID:"(.*?)".*?Frequency:(.*?)\s.*?Signal level=(.*?)\s', output, re.DOTALL)
# Return the list of networks
return networks
wifi_networks = scan_wifi()
for network in wifi_networks:
ssid = network[0]
frequency = network[1]
signal_level = network[2]
print(f"Network: {ssid}, Frequency: {frequency}, Signal Level: {signal_level}")
# this = print(f,"Network: {ssid}, Level: {signal_level}")
output = json.dumps({"ssid": network[0], "signal_level": network[2]})
print(output)
I am bad with code at the start and leave a lot of old stuff in so I know what to NOT do in subsequent tries. When all working I take out all the comments that aren't needed/old lines.
For the sake of it this is how it is done in NR
[{"id":"057c784d0fb29dfc","type":"pythonshell in","z":"c56bddee.ca0a18","name":"wifiscan","pyfile":"/home/pi/python_stuff/wifiscan5.py","virtualenv":"","continuous":false,"stdInData":false,"x":145,"y":3490,"wires":[["b7ae270d16a37445","cab9900ce3a0acc3"]],"l":false},{"id":"cab9900ce3a0acc3","type":"debug","z":"c56bddee.ca0a18","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":3330,"wires":[]}]
OK, sorry, I goofed!
Line 25. I commented it out and it works.
Hang on.