Skip to content
This repository has been archived by the owner on Oct 19, 2019. It is now read-only.

Improved WiFi setup #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@
*.pyc
board/yio-remote/rpi0/rootfs_overlay/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
configs/yio-remote-defconfig.old
### OSX ###
.DS_Store
# Thumbnails
._*
### Linux ###
*~
48 changes: 48 additions & 0 deletions README-YIO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# YIO remote buildroot

## Build

### Prepare Build Environment

1. Prepare Ubuntu for buildroot:

sudo apt-get install bison g++ flex gettext texinfo patch git-core libtool autoconf build-essential libncurses5-dev python unzip

1. Checkout sources:

SRC_DIR=~/projects/yio

mkdir -p ${SRC_DIR}
cd ${SRC_DIR}
git clone https://github.com/YIO-Remote/buildroot.git

1. Retrieve matching Raspberry Pi kernel sources:

RPI_KERNEL_VERSION=1.20180417-1

mkdir -p ${SRC_DIR}/buildroot/linux_kernel
cd ${SRC_DIR}/buildroot/linux_kernel
wget -O - https://github.com/raspberrypi/linux/archive/raspberrypi-kernel_${RPI_KERNEL_VERSION}.tar.gz | gunzip -c > linux-raspberrypi-kernel_${RPI_KERNEL_VERSION}.tar

### Build SD Card Image

1. Set configuration for YIO:

cd ${SRC_DIR}/buildroot
make defconfig BR2_DEFCONFIG=buildroot_config

1. Build:

make

## TODO

- [x] Fix WiFi setup
- [x] Handle SSIDs with spaces (e.g. "IoT Net")
- [x] Network scanning fails after initial boot
- [ ] Create dedicated project for YIO and pull in buildroot release
- [ ] Development (runtime) flag
- [ ] Log messages (Rsyslog?)
- [ ] USB gadget drivers (UART, ethernet) for easier remote access
- [ ] Create dedicated project for webserver & shell scripts.
Buildroot should only pull in releases.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
mkdir -p /mnt/boot
mount /dev/mmcblk0p1 /mnt/boot

/usr/bin/yio-remote/wifi-copy-config.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ then
# setup hostname
#--------------------
echo "$SSID" > /etc/hostname
rm /etc/hosts
rm -f /etc/hosts
echo "127.0.0.1 localhost
#127.0.0.1 $SSID" >> /etc/hosts
hostnamectl set-hostname "$SSID"
Expand All @@ -54,12 +54,11 @@ then
# scan for nearby wifis
#--------------------
iw dev wlan0 scan >> /dev/null
/usr/bin/yio-remote/wifi_network_list.sh >> /networklist
/usr/bin/yio-remote/wifi_network_list.sh > /networklist

#--------------------
# set static IP address
#--------------------
rm /etc/systemd/network/20-wireless.network
echo "[Match]" > /etc/systemd/network/20-wireless.network
echo "Name=wlan0" >> /etc/systemd/network/20-wireless.network
echo "" >> /etc/systemd/network/20-wireless.network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
#--------------------
# Create WPA supplicant file
#--------------------
# delete existing configuration file
rm -rf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

# create a configuration file
# (re-)create a configuration file
mkdir -p /etc/wpa_supplicant
echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1

network={
key_mgmt=WPA-PSK
ssid="\"$1\""
psk="\"$2\""
}" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
}" > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

echo "{\"ssid\":\"$1\",\"password\":\"$2\"," > /wificred
echo "$1" > /ssid
Expand All @@ -32,12 +30,11 @@ killall -9 dnsmasq
#--------------------
# set dynamic IP address
#--------------------
rm /etc/systemd/network/20-wireless.network
echo "[Match]
Name=wlan0

[Network]
DHCP=yes" >> /etc/systemd/network/20-wireless.network
DHCP=yes" > /etc/systemd/network/20-wireless.network

#systemctl daemon-reload
systemctl restart systemd-networkd
Expand All @@ -58,5 +55,5 @@ sleep 1
#cp /etc/lighttpd/lighttpd-config.conf /etc/lighttpd/lighttpd.conf
#systemctl restart lighttpd.service

rm /wifisetup
rm -f /wifisetup
touch /firstsetup
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash
rm /networklist
rm /ssid
rm /firstsetup
rm /wificred
rm -f /networklist
rm -f /ssid
rm -f /firstsetup
rm -f /wificred
touch /wifisetup

# create a configuration file
rm /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
# reset configuration file
mkdir -p /etc/wpa_supplicant
echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
ap_scan=1" > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

#systemctl daemon-reload
systemctl restart systemd-networkd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ then
systemctl stop wpa_supplicant@wlan0.service
sleep 5
# copy config file
mkdir -p /etc/wpa_supplicant
cp /mnt/boot/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
sleep 5
# restart wifi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#!/bin/bash

#check if there is a file
mkdir -p /etc/wpa_supplicant
if [ -e /etc/wpa_supplicant/wpa_supplicant-wlan0.conf ]; then

# delete existing configuration file
rm -rf /etc/wpa_supplicant/wpa_supplicant-wlan0.conf

# create a configuration file
# re-create a configuration file
echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1

network={
key_mgmt=WPA-PSK
ssid="\"$1\""
psk="\"$2\""
}" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
}" > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
else
# create a configuration file
echo "ctrl_interface=/var/run/wpa_supplicant
Expand All @@ -24,5 +22,5 @@ network={
key_mgmt=WPA-PSK
ssid="\"$1\""
psk="\"$2\""
}" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
}" > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# BusyBox compatible awk parsing of 'iw wlan0 scan' output
# Inspired by: https://gist.github.com/elecnix/182fa522da5dc7389975
# Returns rows of <signal_strength>,<SSID> values
#BEGIN {
# # table header
# printf("%s,%s\n","signal","SSID");
#}
NF > 0{
if ($1 == "BSS" && $2 ~ /^[a-z0-9:]{17}\(?/) {
if( e["MAC"] ){
# new block: print result from last block
printf("%s,%s\n",e["sig"],e["SSID"]);
}
e["MAC"] = $2;
}
if ($1 == "SSID:") {
# $2 might not contain the full SSID name if it has an awk separator char in it's name!
e["SSID"] = substr($0, index($0,$2));
}
if ($1 == "freq:") {
e["freq"] = $NF;
}
if ($1 == "signal:") {
e["sig"] = $2;
e["sig%"] = (60 - ((-$2) - 40)) * 100 / 60;
}
}
END {
# final block
printf("%s,%s\n",e["sig"],e["SSID"]);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/bin/bash
i=0
# -----------------------------------------------------------------------------
# Returns a list of WiFi networks.
# Format: <signal_strength>,<ssid>
# Example:
# -35.00,Guest Network
# -61.00,Unifi
# -75.00,DIRECT-gL-BRAVIA
# -----------------------------------------------------------------------------
# Called from firstrun.sh if:
# - /wifisetup marker file present
# - and /wificopy marker file doesn't exist
# Output is written to /networklist
# -----------------------------------------------------------------------------

wpa_cli scan | grep 'OK' &> /dev/null

if [ $? == 0 ]; then
# echo 'OK'
wpa_cli scan_results |
while IFS= read -r line
do
if [ "$i" -gt "1" ]; then
echo $line | awk '{ print $3, $5 }' | tr ' ' ,
# echo $line | awk '{ print $5; }'
fi
i=$((i+1))
done
else
echo 'Scan failed'
fi
# 'iw wlan0 scan' should always work, whereas 'wpa_cli -i wlan0 scan' is a pain to work with
iw wlan0 scan | awk -f /usr/bin/yio-remote/wifi_network_list.awk
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function customWifi() {
}
?>
<div id="othernetwork" class="networklist_element" onClick="customWifi()">
<p>Joint other network...</p>
<p>Join other network...</p>
</div>
</div>
<p id="ssid" class="ssid">
Expand Down Expand Up @@ -143,6 +143,8 @@ function customWifi() {
document.getElementById('timezonetext').innerHTML = Intl.DateTimeFormat().resolvedOptions().timeZone

// get current time
// TODO use date format functions instead of manual formatting
// TODO use current locale and timezone in dateTime format
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes();
document.getElementById('currenttime').innerHTML = time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
</div>
<div id="networklist_container" class="networklist_container">
<?php
// TODO use PHP file functions instead of shell commands
$output = shell_exec("cat /networklist");
$lines = explode("\n", $output);

Expand All @@ -102,7 +103,7 @@
}
?>
<div id="othernetwork" class="networklist_element" onClick="customWifi()">
<p>Joint other network...</p>
<p>Join other network...</p>
</div>
</div>
<p id="ssid" class="ssid">
Expand Down Expand Up @@ -143,6 +144,8 @@
document.getElementById('timezonetext').innerHTML = Intl.DateTimeFormat().resolvedOptions().timeZone

// get current time
// TODO use date format functions instead of manual formatting
// TODO use current locale and timezone in dateTime format
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes();
document.getElementById('currenttime').innerHTML = time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$ssid = $_POST['ssid'];
$password = $_POST['password'];
echo "Connecting";
exec("/usr/bin/yio-remote/first-time-setup/wifi_network_setup.sh $ssid $password");
exec("/usr/bin/yio-remote/first-time-setup/wifi_network_setup.sh ".escapeshellarg($ssid)." ".escapeshellarg($password));
?>