Skip to content

Instantly share code, notes, and snippets.

@yunqu
Last active July 19, 2018 18:36
Show Gist options
  • Save yunqu/20fda4065fa890101b843cec4cfe9e52 to your computer and use it in GitHub Desktop.
Save yunqu/20fda4065fa890101b843cec4cfe9e52 to your computer and use it in GitHub Desktop.
Useful notebook to connect to WiFi
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Onboard WIFI\n",
"\n",
"Some boards can have embedded WIFI modules which allow users to connect\n",
"to WIFI network. The same WIFI module can be used for:\n",
"\n",
"1. Serving as a wireless access point for users to access directly to the board.\n",
"2. Connecting to an existing WIFI network.\n",
"\n",
"This notebook shows how to switch between the above two functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import subprocess\n",
"from time import sleep\n",
"import netifaces"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configure interface\n",
"The interface configuration has to be prepared in order to use \n",
"`ifup` and `ifdown` commands."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('/etc/network/interfaces.d/wlan0', 'w') as f:\n",
" f.write('auto wlan0\\n')\n",
" f.write('iface wlan0 inet manual\\n')\n",
" f.write('wpa-conf /etc/wpa_supplicant.conf')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configure network\n",
"The following cell allows users to choose their own WIFI network."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_ = os.system(\"wpa_cli remove_network all\")\n",
"_ = os.system(\"wpa_cli save_config\")\n",
"\n",
"with open('/etc/wpa_supplicant.conf', 'w') as f:\n",
" f.write(\"ctrl_interface=/var/run/wpa_supplicant\\n\")\n",
" f.write(\"update_config=1\\n\")\n",
"\n",
"ssid = input(\"WIFI network ID:\")\n",
"passwd = input(\"WIFI password:\")\n",
"_ = os.system(\"wpa_passphrase {0} {1} >> \"\n",
" \"/etc/wpa_supplicant.conf\".format(ssid, passwd))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Bring up `wlan0`\n",
"By default, `wlan1` is used for wireless access point, `wlan0` is reserved\n",
"for the use of WIFI connection to external network. The following cell\n",
"will check whether `wlan0` is brought up properly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_ = os.system(\"wpa_cli -i wlan1 terminate\")\n",
"_ = os.system(\"iw dev wlan1 del\")\n",
"_ = os.system(\"ifup wlan0\")\n",
"\n",
"wlan0_found=False\n",
"for i in range(5):\n",
" command = \"wpa_cli -i wlan0 ping\"\n",
" status = subprocess.run(\n",
" command.split(' '), \n",
" stdout=subprocess.PIPE).stdout.decode('utf-8').strip('\\n')\n",
" if status == \"PONG\":\n",
" wlan0_found=True\n",
" break\n",
" sleep(1)\n",
"\n",
"if wlan0_found:\n",
" print(\"Successfully found interface wlan0.\")\n",
"else:\n",
" raise RuntimeError(\"Cannot find interface wlan0.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Connecting to WIFI\n",
"With the configurations set above, we now try to connect to the WIFI network."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"wlan0 = netifaces.ifaddresses('wlan0')\n",
"if netifaces.AF_INET6 not in wlan0:\n",
" _ = os.system(\"ifdown wlan0\")\n",
" _ = os.system(\"ifup wlan0\")\n",
"\n",
"_ = os.system(\"dhclient wlan0\")\n",
"ip_found = False\n",
"for index in range(10):\n",
" wlan0 = netifaces.ifaddresses('wlan0')\n",
" if netifaces.AF_INET not in wlan0:\n",
" sleep(3)\n",
" else:\n",
" ipv4 = netifaces.ifaddresses('wlan0')[netifaces.AF_INET][0]['addr']\n",
" ip_found = True\n",
" break\n",
"\n",
"if ip_found:\n",
" print(\"IPv4 address: {}\".format(ipv4))\n",
"else:\n",
" raise RuntimeError(\"Cannot obtain IP address.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change back to wireless access point\n",
"After changing back, you will be able to use the WIFI access point again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"_ = os.system(\"ifdown wlan0\")\n",
"_ = os.remove('/etc/wpa_supplicant.conf')\n",
"_ = os.remove('/etc/network/interfaces.d/wlan0')\n",
"if os.system('systemctl restart wpa_ap'):\n",
" RuntimeError(\"Cannot restart wpa_ap service.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment