8

Some tutorials I found are outdated, but the following answer contains a collection of notes I have used to set up home, school(enterprise), & Ad-Hoc wireless connections on my Raspberry Pi 3.

2 Answers 2

14

Raspberry Pi 3 (kernel 4.14.30-v7+)

This post is a collection of notes and tutorials I have used for setting up wireless connections to the Raspberry Pi.

Please let me know if you experience any issues.


Headless set-up

Raspbian currently looks in the boot partition for a couple files to allow a headless set-up or connect a Raspberry Pi to a network and establish an ssh connection from an external machine without a monitor, screen or mouse.

After you load Raspbian OS onto an micro SD card, Ubuntu command:

unzip -p 2018-03-13-raspbian-stretch-lite.zip | sudo dd of=/dev/sdX bs=4M conv=fsync status=progress

Add two files to the boot partition before moving the SD card to the Pi.

  1. SSH must be enabled by placing a file named 'ssh', without any extension, onto the boot partition of the SD card. This will allow you to connect from an external device.
  2. Network connections must be defined
    • add file named "wpa_supplicant.conf" in the boot partition and fill with network information following tutorial below.

Example "wpa_supplicant.conf" for home connection:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
    ssid="NETWORK_NAME"
    psk="password"
}

Now what is the Pi's IP address? Search for the MAC address, all RPi 3 have the same start to their MAC address. (Won't work on enterprise network, ask admin about privliages)

Ubuntu command:

sudo nmap -sn 192.168.1.0/24 | grep -B 2 B8:27:EB

SSH

Automatically disabled during initial boot.

Enable with

sudo raspi-config

Then select Interfacing OptionsSSHEnable


Home Network Connection

Easiest way:

sudo raspi-config 

Select Network OptionsWi-fi then follow on screen instructions.

Manual way:

Edit wpa_suplicant.conf

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following:

network={
    ssid="NETWORK_NAME"
    psk="password"
}

Then save the file


Enterprise Network Connection

This is how I connect to my university wifi

First create password hash and save to file (keep the single quotes around your password):

echo -n 'YOUR_REAL_PASSWORD' | iconv -t utf16le | openssl md4 > hash.txt

Then edit wpa_supplicant.conf

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Add the following:

network={
    ssid="NETWORK_NAME"
    proto=RSN
    key_mgmt=WPA-EAP
    group=CCMP TKIP
    identity="YOUR_USERNAME"
    password=hash:5ac87xxxxxxxxxxxxxxxxxxetc
    phase1="peaplabel=0"
    phase2="auth=MSCHAPV2"
}

To add password hash:

  1. Move cursor to where you want to place text
  2. Press ctrl+r then insert hash.txt file
    • hash.txt contains (stdin)= 5ac87xxxxxxxxxxxxxxxxxxetc
    • replace "(stdin)= " with "hash:"
  3. Save the file
  4. Clear your bash history

    history -c
    
  5. Remove hash.txt

    rm hash.txt
    

Managing Multiple Connections

Add priority flags to each network in wpa_supplicant.conf

default priority=0 (greatest priority goes first)

My prioritized wpa_supplicant.conf:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
    priority=1
    ssid="NETWORK_A"
    psk="password"
}

network={
    priority=2
    ssid="NETWORK_B"
    proto=RSN
    key_mgmt=WPA-EAP
    group=CCMP TKIP
    identity="YOUR_USERNAME"
    password=hash:5ac87xxxxxxxxxxxxxxxxxxetc
    phase1="peaplabel=0"
    phase2="auth=MSCHAPV2"
}

There are many more configuration options, run man wpa_supplicant.conf for info.


Ad-Hoc Network / Access Point / Bridge Connection

You want to wireless connect to the pi but there are no available wireless networks. Create your own!

The Raspberry Pi can be set up to broadcast a wireless network. This network allows you to connect the the pi, but will not connect to the internet.

Simple Pi to Pi ad-hoc setup

On Each Rpi

Copy the current file for backup

cp /etc/network/interfaces /etc/network/interfaces.old

Open file to edit

sudo nano /etc/network/interfaces

The only line that should be different in the following file is address everything else should be identical. And you may change the name of your network or add a password

Append the following on Rpi A:

auto wlan0
iface wlan0 inet static
    address 192.168.1.1 
    netmask 255.255.255.0
    wireless-channel 1
    wireless-essid MYNETWORK
    wireless-mode ad-hoc

Append the following Rpi B:

auto wlan0
iface wlan0 inet static
    address 192.168.1.2 `
    netmask 255.255.255.0
    wireless-channel 1
    wireless-essid MYNETWORK
    wireless-mode ad-hoc

Save the file and exit the editor

Raise the interface:

sudo ifup wlan0

Scan for ad-hoc networks:

iwlist wlan0 scan

You may add more devices to the network, you just must assign the device a new different static IP address.

The default operating frequency/channel (2.412 GHz: channel 1) is frequently congested. Try using a different channel in the event of difficulties.

Change wireless-channel to a different number

Use sudo iwlist wlan0 scan to find other channels.

src: https://wiki.debian.org/WiFi/AdHoc


Less Simple

More complicated but will connect to known networks and create ad-hoc if none are found.

Follow this tutorial (no ethernet bridge, I have used this successfully) http://www.raspberryconnect.com/network/item/331-raspberry-pi-auto-wifi-hotspot-switch-no-internet-routing

hostname -I 

Run this on Pi, Then I connected from to the second IP address listed via ssh pi@[ipaddr] from my Ubuntu host machine.

With ethernet bridge (I have not tested this, but from the same tinkerers as ↑) http://www.raspberryconnect.com/network/item/330-raspberry-pi-auto-wifi-hotspot-switch-internet


KNOWN ERRORS:

Ssh connection fails from host machine to Pi? run with verbose output:

ssh -vvvv [email protected]

If the last commands before close are:

debug1: SSH2_MSG_KEXINIT sent
Connection closed by raspberrypi.local port 22

Try regenerating the host keys they may be missing or corrupt (do this on Pi):

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server 

https://www.raspberrypi.org/forums/viewtopic.php?t=168310


References

man wpa_supplicant.conf

https://www.raspberrypi.org/forums/viewtopic.php?t=111100

https://www.doc.ic.ac.uk/~ajd/Robotics/RoboticsResources/wifi_setup.txt

https://gist.github.com/chatchavan/3c58511e3d48f478b0c2

3
  • This is a great post... there's so much useful stuff in here! Suggestion: get a GitHub account, and post this 'recipe' there.
    – Seamus
    Commented Apr 13, 2018 at 23:02
  • 2
    Scanning the arp cache has some disadvantages: you only see the neighbors and the wanted device must be addressed before in any way. If not then it isn't in the cache. Pinging the broadcast address before may work to address all neighbors but your network admin does not like that! An alternative is nmap, e.g. sudo nmap -sn 192.168.10.0/24 | grep -B 2 B8:27:EB for neighbors.
    – Ingo
    Commented Apr 14, 2018 at 14:52
  • 1
    You are writing about bridging. Be aware that you cannot real bridge wifi on OSI layer 2 with a raspi because of hardware/driver limits.
    – Ingo
    Commented Apr 14, 2018 at 15:06
0

Use this easy program I found on Github: Rasp-Connect - Easy python Script to Connect Raspbian to a Wifi Network Using Python.

It is an easy Python script that configures and sets it up for you, you just need to select what encryption type and enter the SSID, and the password. It works very well and also supports running the tool over SSH.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.