Create SoftAP with Hostapd on Ubuntu and Rpi.md

In Ubuntu 14.04

Check whether the module 8188eu is load lsmod|grep 8188 or lsusb

Download and compile latest hostapd with RTL8188 drivers

1
2
3
4
git clone https://github.com/lostincynicism/hostapd-rtl8188
sudo apt-get install libnl-3-dev # Fix fatal-error-netlink-genl-genl-h-no-such-file-or-directory
sudo apt-get install libnl-genl-3-dev
cd hostapd-rtl8188/hostapd && sudo make all

The complete options in /usr/share/doc/hostapd/examples/hostadp.8188eu.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface=wlan0
driver=rtl871xdrv
ssid=Soft-AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
#wpa=1
#wpa_pairwise=CCMP
wpa=2
rsn_pairwise=TKIP CCMP
wpa_passphrase=123456789
wpa_key_mgmt=WPA-PSK

Fix “Cannot find the ssid problem”

1
2
sudo nmcli nm wifi off
sudo rfkill unblock wlan # disable power saving mode, use **sudo nmcli radio wifi off** if Error: Object 'nm' is unknown then use this instead:

Following maybe not work but for reference

1
2
3
4
5
6
7
sudo systemctl disable NetworkManager.service 
sudo systemctl enable networking.service

sudo systemctl status wpa_supplicant.service

sudo systemctl --force stop wpa_supplicant.service
wpa_cli -i wlan0 terminate # stop wpa_supplicant program as it is conflicted with hostapd

Start hostapd

1
2
3
cd hostapd-rtl8188/hostapd
sudo ./hostapd -dd /usr/share/doc/hostapd/examples/hostadp.8188eu.conf
sudo ifconfig wlan0 192.168.42.1

In Raspberry Pi

Configure IP sudo ifconfig wlan0 192.168.42.100

Scan ssid iwlist wlan0 scan

Connect to ssid “Soft-AP”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# wpa_cli
>scan
>scan_results
>add_network
>set_network 0 ssid "Soft-AP"
>set_network 0 psk "123456789"
>enable_network 0
>quit
# ping 192.168.42.1 or iwconfig


wpa_cli -i wlan0 terminate (關閉wpa_supplicant)
wpa_cli -i wlan0 save_config (儲存寫入到wpa_supplicant.conf中,否責變更無效) need "update_config=1" in /etc/wpa_supplicant/wpa_supplicant.conf
wpa_cli -i wlan0 select_network (選擇不同的AP,id為AP代號0,1,2,3...)
wpa_cli -i wlan0 disable_network
wpa_cli -i wlan0 remove_network (刪除AP資訊)
wpa_cli -i wlan0 disconnect (斷線)
wpa_cli -i wlan0 reconnect (重新連線)
wpa_cli -i wlan0 list_networks (查看wpa_supplicant.conf檔裡的設定)
wpa_cli -i wlan0 interface (查看有那些無線網卡介面)

Others

In Ubuntu

  • Set proxy java -cp lib/log4j-1.2.16.jar:lib/jboss-common-logging-spi.jar:lib/netty.jar org.jboss.netty.example.proxy.HexDumpProxy 8081 10.13.135.115 8080

In RaspPi

  • Setting Up apt-get with proxy. Add file proxy.conf in “/etc/apt/“ with content Acquire::http::Proxy "http://192.168.42.1:8081/";
  • Setting Up http proxy by
    1
    2
    export http_proxy=http://192.168.42.1:8081/
    export https_proxy=http://192.168.42.1:8081/
  • Run sudo apt-get install apt-transport-https if problem “E: The method driver /usr/lib/apt/methods/https could not be found.” occurs when sudo apt-get update
  • Install mpv from https://nwgat.ninja/quick-easy-compiling-mpv-for-raspberry-pi/
  • Install livestreamer
  1. Download setuptools from https://pypi.python.org/pypi/setuptools#downloads
  2. Install setuptools python setup.py install and sudo -E easy_install livestreamer
  3. Test the audio livestreamer --https-proxy=http://192.168.42.1:8081 --http-proxy=http://192.168.42.1:8081 hlsvariant://http://rthkaudio1-lh.akamaihd.net/i/radio1_1@355864/master.m3u8 best -p mpv
  4. Change the volumn by
    4.1 amixer controls to show the Master Playback Volume
    4.2 Get details of the master playback volume control by amixer cget numid=3, it shows a pair of volume values=65536,65536(for left and right channels)
    alsamixer, save volumn level via sudo alsactl store
    amixer cset numid=1 -- 80% or amixer cset numid=3 80% Or amixer set Master 100%
    4.3 To mute the sound by amixer cset numid=4 off (Control 4), this also affects the volume values on control 3
    Reference http://blog.scphillips.com/posts/2013/01/sound-configuration-on-raspberry-pi-with-alsa/
  5. Set audio output device amixer cset numid=3 N (N=0=auto, 1=analog, 2=hdmi)
  • Start openssh-server sudo service ssh start
  • Install openvpn sudo apt-get install openvpn
  • Install adb and fastboot by sudo apt-get install android-tools-adb android-tools-fastboot
  • Configure udev service for detecting the pluged phone
    1
    2
    3
    sudo wget -O /etc/udev/rules.d/51-android.rules https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/android/51-android.rules
    sudo chmod a+r /etc/udev/rules.d/51-android.rules
    sudo service udev restart
  • Plug the mobile and mount the sdcard by sudo mount /dev/sda1 /mnt
    Check the disk by
    1
    2
    3
    sudo fdisk -l /dev/sda
    sudo lsblk
    df -h
  • USB tethering
    1
    2
    adb forward tcp:41927 tcp:41927
    sudo openvpn azilink.ovpn
  1. Write PiCast application with golang
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    package main

    import (
    "fmt"
    "github.com/gorilla/mux"
    "io/ioutil"
    "net/http"
    "os"
    "os/exec"
    "strings"
    "syscall"
    )

    func main() {
    r := mux.NewRouter()
    r.HandleFunc("/", Home)
    //r.HandleFunc("/yt-stream/{args}", Youtube)
    r.HandleFunc("/yt-stream", Youtube)

    bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
    fmt.Printf("listening on %s...\n", bind)
    err := http.ListenAndServe(bind, r)
    if err != nil {
    panic(err)
    }
    }

    func Home(res http.ResponseWriter, req *http.Request) {
    fmt.Fprint(res, "Welcome to PiCAST! In the URL, type what you want to do...\n")
    }

    func Youtube(res http.ResponseWriter, req *http.Request) {
    body := readReqBody(req)
    url := body["url"]

    if url == "" {
    args := body["args"]
    url = "https://www.youtube.com/watch?v=" + args
    }

    p := body["proxy"]

    http_proxy := ""
    https_proxy := ""

    if p != "" {
    http_proxy = "--https-proxy=" + p
    https_proxy = "--http-proxy=" + p
    }

    args := []string{http_proxy, https_proxy, url, "best", "--player=mpv"}
    fmt.Println(args)

    run(args)
    }

    func run(args []string) {
    exec.Command("killall livestreamer").Run()

    binary, lookErr := exec.LookPath("livestreamer")
    if lookErr != nil {
    panic(lookErr)
    }

    env := os.Environ()

    execErr := syscall.Exec(binary, args, env)
    if execErr != nil {
    fmt.Println("execErr", execErr)
    }
    }

    func runOld(args []string) {
    cmd := exec.Command("livestreamer", args...)
    fmt.Println(cmd.Path)
    fmt.Println(cmd.Args)
    fmt.Println(cmd.Env)

    if err := cmd.Start(); err != nil {
    fmt.Printf("cmd.Start: %v\n", err)
    }

    if err := cmd.Wait(); err != nil {
    if exiterr, ok := err.(*exec.ExitError); ok {
    if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
    fmt.Printf("Exit Status: %d\n", status.ExitStatus())
    }
    } else {
    fmt.Printf("cmd.Wait: %v\n", err)
    }
    }
    }

    func readReqBody(req *http.Request) map[string]string {
    body, _ := ioutil.ReadAll(req.Body)
    // Info.Println("body = ", string(body))
    m := make(map[string]string)

    content := strings.Split(string(body), "&")
    for _, c := range content {
    p := strings.Split(c, "=")
    if len(p) == 2 {
    m[p[0]] = p[1]
    }
    }
    return m
    }
  • Cross compile this web application in linux

    1
    2
    env GOOS=linux GOARCH=arm go build -ldflags="-s -w" main.go
    scp -i id_rsa main pi@192.168.42.100:/home/pi/
  • Install tmux on ubuntu
    sudo apt-get install tmux

    ctrl+b % - split window vertically
    ctrl+b " - split window horizontally
    ctrl+b o - Goto next pane
    ctrl+b c - Create new window
    ctrl+b w - List all windows 
    ctrl+b l - Move to the previously selected window
    

Reference

ADB FIFO example
USB tethering
https://github.com/lanceseidman/PiCAST