0

The goal is for the user to select a WiFi network. I'm running a webserver with php and would like to list the available networks.

The idea would be to have the user to press a scan button on the web page, which will then display a list of networks. User will then be allowed to choose which network they want to connect to. I am currently thinking of running a python script that gets the info.

Then the selected network will be saved in the /etc/wpa_supplicant/wpa_supplicant.conf for connection.

gram95
  • 103
  • 2
  • 15
  • Check out the subprocess module. And this answer https://stackoverflow.com/a/31868561/5352244 – NOP da CALL Jun 28 '17 at 04:07
  • Hi, I'll check it out. Thanks. – gram95 Jun 28 '17 at 04:10
  • Hi, I just tried it out. I encountered this error. FileNotFoundError [Errno2] No such file or directory :'netsh' Any idea what it could mean? – gram95 Jun 28 '17 at 04:23
  • Thats for windows, you will need to use subprocess with the raspberry pi terminal commands that display the info you want , I dont have a clue about raspberry pi. – NOP da CALL Jun 28 '17 at 04:44
  • I see, thanks anyways. – gram95 Jun 28 '17 at 05:18
  • Welcome to Stack Overflow! I edited the question to have shorter sentences and clearer overview of what you're trying to accomplish. As a side note, you may want to explain why you want to use python to obtain the information, when you're already running php. Good luck! –  Jun 28 '17 at 11:35
  • I used python as I didn't know that Php was capable of scanning of wifi. @Melvyn – gram95 Jun 29 '17 at 01:11

1 Answers1

0

There's no reason to run python, since the information is easiest obtained by running wpa_cli. Php uses shell_exec to run commands, which is the equivalent of os.system and to some extent subprocess.check_output in python.

You have to run two commands in order. If the first fails, the second has no use as the WiFi card is not able to scan.

wpa_cli -i wlan0 scan

This should return "OK" and wlan0 is the name of the wireless interface as shown by ifconfig. This is typically wlan0 and does not change over time, only when more WiFi cards are added to the pi.

wpa_cli -i wlan0 scan_results

This returns several lines with a header:

bssid / frequency / signal level / flags / ssid
b2:c2:87:77:62:73       2437    -50     [WPA2-EAP-CCMP][ESS]    Ziggo
1c:3a:de:c3:f8:cf       2472    -54     [WPA2-PSK-CCMP+TKIP][WPS][ESS]  HZN246837438

You can discard the first line and split the rest of the lines at whitespace. Php can do this and make sure you allow at maximum 4 splits (resulting in 5 elements).

I'm doing exactly what you want, on a rpi-3, with Django (python). For a webserver with php the process is the same.

  • Hi Melvyn, thanks for the information. I tried the commands you said in the terminal of thr RPi and it worked. Also, when you said that Php can do that, does it mean I just have to type the exact same commands into a php file and run it? – gram95 Jun 29 '17 at 01:09
  • I linked shell_exec, forgot to do that initially. So type the commands as argument to the `shell_exec` function. –  Jun 29 '17 at 07:11