2

does anyone know it it is possible to scan for Wi-Fi and display the output of the results using a Python program that is run by a PHP button on a Raspberry Pi web page server?

The idea would be to have the user to press a scan button, (PHP), which will then display a list of networks. The user will then be allowed to choose which network they want to connect to.

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

Any sort of information would be really useful.

Darth Vader
4,21824 gold badges47 silver badges71 bronze badges
asked Jun 28, 2017 at 3:51
7
  • Isn't this how the gui networking tool works? can you explain why you seem to be reinventing the wheel? Commented Jun 28, 2017 at 3:52
  • I'm sorry, I am very new to Raspberry Pi and Linux itself. Could you explain to me what you meant by gui networking tools? Commented Jun 28, 2017 at 4:05
  • In Raspbian's graphical interface there is an icon in the top bar (or one that can be added) which when you click on it drops down showing a list of availbale networks. supply your passphrase and your done. raspberrypi.org/documentation/configuration/wireless/… Commented Jun 28, 2017 at 4:27
  • Another question, given your solution how do you stop anyone from changing the network connection while another user is connected to a different network. Commented Jun 28, 2017 at 4:44
  • Hi Steve, thanks for the reply. The final set up for the project that I am working on will not have any monitors plugged in to the RPi itself. The user will access the RPi through their own devices(eg phone) and key in a IP address which will then direct them to the web page server for the RPi. Commented Jun 28, 2017 at 5:17

1 Answer 1

1

Yes, it's possible.

See This demo. Click on the menu icon at the top right, and then the "WiFi setup" button.

HTML Web-page:

When the page is opened, the ReqWifiList() JavaScript script creates an AJAX "GET" request for the list of networks. When the response comes back, the ShowWiFiNetworks(wifiNetListStr) script is called, which lists the networks.

// Request a list of WiFi networks
function ReqWifiList() {
 // Constants
 var GetSrvc = '/getWiFiList';
 var xmlhttp = new XMLHttpRequest(); 
 xmlhttp.onreadystatechange = function() {
 if (xmlhttp.readyState==4 && xmlhttp.status==200){
 // Show the networks
 ShowWiFiNetworks(xmlhttp.responseText);
 }
 }
 xmlhttp.open("GET", GetSrvc, true);
 xmlhttp.send(); 
}
// Show the available WiFi Networks
function ShowWiFiNetworks(wifiNetListStr) {
 // Show the networks
}

Web server (Flask):

 from flask import Flask, render_template, request
import lithiumate_data_logger
app = Flask(__name__)
# Show the page
@app.route('/')
def index():
 return render_template('index.html')
# Handle a request from the page for the list of WiFi networks
@app.route('/getWiFiList', methods=['GET'])
def getWiFiList():
 srvResp = 'Fail'
 if request.method == 'GET':
 srvResp = lithiumate_data_logger.getWiFiList()
 return srvResp
if __name__ == '__main__':
 app.run(debug=True, host='0.0.0.0')

Python script:

 def getWiFiList():
 """Get a list of WiFi networks"""
 wifiNetworkList = ''
 # Presently connected network
 connectedNetworkNameResponse = ''
 try:
 connectedNetworkNameResponse = subprocess.check_output(['sudo','iwgetid'])
 break
 except subprocess.CalledProcessError as e:
 print 'ERROR get connected network: '
 print e
 connectedNetworkNameStr = re.findall('\"(.*?)\"', connectedNetworkNameResponse) #" Find the string between quotes
 wifiNetworkList = connectedNetworkNameStr[0] # [0] returns the first one
 # Available networks
 availableNetworksResponse = ''
 try:
 availableNetworksResponse = subprocess.check_output(['sudo','iw','dev','wlan0','scan'])
 break
 except subprocess.CalledProcessError as e:
 print 'ERROR get list of networks: '
 print e
 print availableNetworksResponse
 availableNetworksLines = availableNetworksResponse.split('\n')
 for availableNetworksLine in availableNetworksLines:
 if 'SSID' in availableNetworksLine:
 # Typical line:
 # SSID: elithion belkin
 essid = availableNetworksLine.replace('SSID:','').strip()
 wifiNetworkList = wifiNetworkList + ',' + essid 
 return wifiNetworkList

Note that the response string is of the form: 'connected SSID, an SSID, an SSID, an SSID,... an SSID'

answered Sep 24, 2017 at 16:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.