A Raspberry Pi WiFi Access Point for HiBal Missions

A Raspberry Pi WiFi Access Point for HiBal Missions

Abstract:

During Pre-flight checkout, and post-flight data dumps, I’d like a balloon payload flight computer to function as a stand alone wireless access point. I show how to setup a Raspberry Pi as a stand alone wireless access point, and include shell scrips to switch between a mode to connect to my home wifi network, and a standalone access point mode. This capability enables communicating with the Raspberry Pi flight computer via ssh, ftp, and a web browser.

Objectives:

I would like to be able to switch between two WiFi configurations. The first is for home use, to provide WiFi access to a Raspberry Pi (RPi) over the home WiFi, and let it access the outside world. The second is to provides an access point before, during and after balloon missions. This would include httpd access (Apache), ssh and ftp access for file transfer and controlling the PI. It should provide an SSID access point, dhcp IP address assignment, and some local DNS hostname resolution. A configuration that provides both, with a bridge between the networks might be ideal, But that project will wait.

Introduction:

I am using the Raspberry Pi A+ V1.1 with an Ourlink 150M 802.11n Wireless USB Adapter, I also have a Tenda W522U dual band USB Adapter. I also have a Fedora Linux desttop and several android tablets.

Needed:

1) Raspberry Pi – I’m using a model 1 A+

2) WiFi USB adapter – I have an Ourlink 150M 802.11n Wireless USB Adaptor. Some current RPis have a wireless adaptor built in. It must have the capability of acting as an access point.

3) Network connection – to download the needed software.

4) Another device with wireless wifi, to test the access point, This could be a phone or tablet, or a desktop computer.

Helpful:

1) Monitor and keyboard/mouse connection to the RPi.

2) A Linux OS computer. I use this mount, read and backup the Raspian memory card, and to connect and communicate with the RPi via ssh, and a web browser. Ssh can be used from almost any desktop or laptop, but using an X11 based OS lets some of the graphical interface programs run on the Rpi.

3) Familiarity with ssh and lxterminal for working with command line commands.

For the most part this guide follows the Raspberry pi.org documentation, Setting up a Raspberry Pi as an access point in a standalone network. Some other helpful links include Adafruit.com, and MakeTechEasier.com.

A few useful commands are

a) iw list shows the capabilities of the radio wireless WiFi card/adapter. Under supported interface modes, AP must appear to function as an access point. Both adapters I have include this capability.

b) iwlist wlan0 scan provides a list of local SSID’s. This verifies that, at least at some level, the adapter is working.

c) ifconfig shows the available network devices, their IP addresses, and packet transmission, I.e. whether they a functioning. ifconfig wlan0 shows the information for the wireless wifi adapter, wlan0, if it is connected to a network.

I tried to set this up exclusively using a network connection, and using ssh [-X IPAddress]. I almost succeeded, but couldn’t find a typographical error, and had to resort to a keyboard and monitor connection to find it. While it is possible to mount the RPi SD card on another Linux system to repair things, Testing and debugging are easier using direct control when messing with the network connection.

MakeTechEasier.com has a troubleshooting section that helped me.

Procedure:

Step One Create a current backup card image, to have available after we hammer something. There is probably a right way to do this, perhaps using the dump, or rsync command. But for now, I follow the lead of the Raspberry Pi OS distributions by creating a card image on a second linux machine with the dd command. (then compressing it for longer term storage).

sudo dd status=progress bs=4M if=/dev/sdX of=/mnt/Backup/StratoPi_YYMMDD.img

where /dev/sdX is the location of the card. The software guide, Backing Up Your Home Folder gives help including backing up your home folder between computers using rsync. While this backup is smaller than the card backup, if the OS configuration is accidental killed, the memory image is easy to reinstall.

Step Two Initially I planned to use the network-manager package, or the wicd package. However neither of these are included in the default distribution. A brief look around the internet suggests both of these can be a challenge to set up. Wireless-tools is used to manage WiFi connections instead.

Dpkg-query can tell you what packages are installed on your Pi. dpkg-query -l gives a list of installed packages. On mine, I see both wireless-tools and net-tools, With no network-manager or wicd. The commands to set up wireless appear to be related to the iw(8) command (via man -k wireless).

See more /usr/share/doc/wireless-tools/README.Debian

RaspberryPi.org provides some Networking Lessons, a nice little set of tutorials and class lessons. These are geared to a hardwired ethernet network connection, not a wireless wifi connection. They help explain dhcp and dns. While geared to a wired network, it shows how to set up networking information and how to use dhcp. It uses the files /etc/dhcpcd.conf or /etc/network/interfaces to set up networking information.

For a dhcp and domain name server, RaspberryPi.org suggests installing and using dnsmasq, Other sites suggest different dhcp servers.

Step Three Make sure the Rpi’s wifi is set up. This is usually accomplished during the operating system initialization. Instructiosn to set up wireless wifi, via the command line are here. Setting WIFI up via the command line

Instructions to set up a wireless access point are here.

This includes instructions to set up the dhcp server (dnsmasq) and the access point software (hostapd). I won’t try use the bridge software (bridge-utils) yet.

Step Four update the Raspian OS

sudo apt-get update
sudo apt-get dist-upgrade

and reboot. Does the OS still work?

sudo apt-get install dnsmasq hostapd

reboot to see if the system still works, then backup SD Card again.

Installation may start up the hostapd and dnsmasq services and you probably should stop them.


sudo service hostapd stop
sudo service dnsmasq stop

Step Five Create backup copies of some of the working configuration files.
These will be used to get back to a working local network.


sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.orig
sudo cp /etc/network/interfaces /etc/network/interfaces.orig
sudo /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.orig
sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo cp /etc/default/hostapd /etc/default/hostapd.orig

Actually we won’t be changing the wpa_suppliant.conf file, but it doesn’t
hurt to make a backup. This file contains the information to connect
to your local wifi network. And it may be helpful to know where that
information is stored. We will also be creating a file /etc/hostapd/hostapd.conf that you may wish to make a backup of.

Step Six Write a couple of scripts to copy the *.orig files into the configuration files and restart the network services. This is used to restore connections to the local network. They can be stored in /usr/local/bin.

Start with a script to bring down and restart the network.

>Sudo nano /usr/local/bin/reset_net

to open the nano editor. While I like to use the leafpad editor, X-windows security may not let the root user (from sudo) open a window in user pi’s window. For editing root owned system files, I use nano or vi.



#! /bin/bash
#/usr/local/bin/reset_net
# make this file executable with
# sudo chmod
a+x /usr/local/bin/reset_net
# restart network services
echo usage:
sudo reset_net
#restart services
service dhcpcd restart
ifdown wlan0
ifup wlan0


The above script lets you restart network services over an ssh connection, without losing the connection. If you tried to enter the
commands separately ifdown would sever the connection. However,
this routine takes longer than I expected to complete, presumably due to
reestablishing the wireless connection.

sudo nano /usr/local/bin/net_restore
to open the nano editor again.



#! /bin/bash
# call with sudo
echo usage: sudo net_reset
# copy configuration files
cp /etc/dhcpcd.conf.orig /etc/dhcpcd.conf
cp /etc/dnsmasq.conf.orig /etc/dnsmasq.conf
cp /etc/network/interfaces.orig /etc/network/interfaces

# stop services
service hostapd stop
service dnsmasq stop
# restart network
/usr/local/bin/reset_net


/etc/wpa_supplicant/wpa_supplicant.conf
gives the current wifi network login information. A man page exists describing this file. Apparently multiple configurations can be stored here, and presumably wpa_supplicant will move down the list trying to connect, or use the priority parameter.

As noted above sudo reset_net does not kill the ssh session, but does take a while to return. wpa_cli reconfigure>, may also reset the wifi network.

Step Seven Set a static IP address.
sudo nano /etc/dhcpcd.conf
set hostname
add a line denyinterfaces toward the end of the file, before any (if any) interfaces lines

Now make a copy of that file, then turn to the interfaces file.


sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.m1
sudo nano /etc/network/interfaces

comment out the wlan0 lines with using a ‘#’ character


# iface wlan0 inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

leave the allow-hotplug wlan0 line alone and below the allow-hotplug line add


iface wlan0 inet static
address 192.168.XX.XXX (chosen IP address, for example 192.168.10.55)
netmask 255.255.255.0
network 192.168.XX.0

and make a copy of the new interfaces file

sudo cp /etc/network/interfaces /etc/network/interfaces.m1

You may want to fix the /etc/hosts file by adding a line
192.168.XX.XXX hostname hostname.local
using whatever your hostname is, and its IP address..

If you get cut off from the network, some fixing of the configuration files can be accomplished by powering down the RPi, removing the SD card, and mounting it in a Linux computer. The files can be manipulated from there, but su privileges may be needed.

At this stage the RPi may NOT be able to reconnect to the existing network if rebooted since there is no link to wpa_supplicant.conf.

Step Eight Set up the DHCP server, dnsmasq.

Create a new /etc/dnsmasq.conf file, since we already moved the old one to /etc/dnsmasq.conf.orig

sudo nano /etc/dnsmasq.conf.m1



interface=wlan0 # Use the required wireless interface - usually wlan0
dhcp-range=192.168.10.125,192.168.10.148,255.255.255.0,24h


This has to be on the same subnet as the RPi and you can specify your own range of connections. The time, 24h, is the duration of the dhcp lease. Now copy this file to the expected location.

sudo cp /etc/dnsmasq.conf.m1 /etc/dnsmasq.conf

I have not yet explored making the domain name server fancier, but that option is available.

Start the daemon with

sudo service dnsmasq start

Step Nine Set up the Access Point software.

References for this section include RaspberryPi.org, Adafruit.com, and MakeTechEasier.com.  If a /etc/hostapd/hostapd.conf file exists, back it up.

sudo mv /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.orig

and write a new one

sudo nano /etc/hostapd/hostapd.conf

which includes the following



interface=wlan0
driver=nl80211
#driver=rtl871xdrv
ssid=NameOfNetwork
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=AardvarkBadgerHedgehog
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP


You can choose your SSID, and wpa_passphrase. The passphrase must be at least 8 characters. The RaspberyPi.org documentation recomment’s not using quotes for either of these. hw_mode=g chooses the 2.4GHz band, hw_mode=a chooses the 5GHz band if available. The channel can be
from 1-14 for 2.4GHz. I’m not sure how to designate 5GHz channels.
Possibly use iw list to guess at channel numbers. I haven’t figured out how to create an open passphraseless SSID.

If the en-el-80211 driver does not work, try rtl871xdrv. Adafruit wifi
adapters may use this driver, but may also need their Adafruit’s version of hostapd. Check out Adafruit’s
Setting
up a Raspberry Pi as a WiFi access point
An annotated version of
hostapd.conf is available from w1.fi.

If you are feeling adventurous, and have a monitor/keyboard/mouse
attached to your RPi you can try running the hostapd daemon.

sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf

This will give you some confidence that hostapd is working, and if not, may provide error messages that help identify problems. The SSID should show up on your wireless phone/tablet’s list
of SSID’s but if the dnsmasq server is not yet working you
may not be able to connect. There is probably an elegant way to close the running hostapd server, but I use Ctl-C instead.


sudo cp /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.m1

Now hostapd has to be told where to find its configuration file.sudo cp /etc/default/hostapd /etc/default/hostapd.orig
sudo nano /etc/default/hostapd
Uncomment the DAEMON_CONF line (remove the “#“)
and change it to read
DAEMON_CONF="/etc/hostapd/hostapd.conf"
and back it up

sudo cp /etc/default/hostapd /etc/default/hostapd.m1

Step Ten Write the script for starting the access point. I call my version Spi_reset, for StratoPi. And I put it in the
/usr/local/bin directory



#!/bin/bash
#/usr/local/bin/SPi_reset
# call with sudo
echo usage: sudo SPi_reset

# copy configuration files
cp /etc/dhcpcd.conf.m1 /etc/dhcpcd.conf
cp /etc/dnsmasq.conf.m1 /etc/dnsmasq.conf
cp /etc/network/interfaces.m1 /etc/network/interfaces
cp /etc/hostapd/hostapd.conf.m1 /etc/hostapd/hostapd.conf

# stop services
service hostapd stop
service dnsmasq stop

#restart network

/usr/local/bin/reset_net
# start services
service dnsmasq start
service hostapd start


Step Eleven Test it out.
You should be able to complete the following commands through an ssh connection,
ssh pi@192.168.XX.XXX (using the correct IP address)

sudo net_reset

Does the RPi connect to the home network?

Does ssh pi@192.168.XX.XXX from
another machine/tablet connect to the RPi?

If a web server is running on the RPi, can you connect from a browser?

sudo Spi_reset # Spi is short for StratoPi

Does the access point show up? Can you connect to it?

Does
ssh pi@192.168.XX.XXX from
another machine/tablet connect to the RPi?

If a web server is running on the RPi, can you connect from a browser?

sudo reboot

Does the RPi come back up in the access point mode?

sudo net_resetsudo reboot

Does RPi come back up on the local LAN?

Step Twelve Duplicate or backup the working SD Card.

Conclusion

It should now be easy to log into your RPi using ssh, and change its networking configuration using
sudo net_reset
or
sudo SPi_reset
as needed.

References:

https://www.raspberrypi.org/learning/networking-lessons/

This scheme of work has been designed as an introduction to networking on the Raspberry Pi. Students will set up and use various networks, and will be introduced to basic networking through practical activities.

https://learn.adafruit.com/setting-up-a-raspberry-pi-as-a-wifi-access-point/overview

Would you like to use your Pi as a WiFi router? Or maybe have it as a special filtering access point? Setting up a Pi as an access point (AP) is a bit more advanced …

Pi-Point – turn your Raspberry Pi into a wireless access point (raspberrypi.org blog)

I was shown Pi Point before the holidays, but thought it was best to wait until now to spring it on you, because so many of you have got Pis for Christmas and are …

Setting up a Rsspberry Pi as a WiFi access point (Adafruit pdf)

Setting up a Raspberry Pi as a WiFi access point Created by lady ada Last updated on 2016-12-05 05:56:01 PM UTC

Raspberry Pi Wireless Access Point @Raspberry_Pi #PiDay # …

Super handy tutorial from Pi My Life Up. A Raspberry Pi wireless access point is a great way to extend the length of your Wi-Fi coverage, and provide an additional …

Exploring NetworkManager, D-Bus, systemd, and Raspberry Pi

The end result is a Raspberry Pi outfitted with LEDs that will inform me of … Exploring NetworkManager, D-Bus, systemd, and … apt-get install-y network-manager.

https://www.raspberrypi.org/learning/software-guide/backup/

Raspberry Pi Backup guide.

How to Set up a Raspberry Pi as a Wireless Access Point

Not only can you connect to a WiFi network in Raspberry Pi, you can also use the WiFi USB dongle as a wireless access point. Here’s how to do it.

Guide to connecting to WiFi

From RaspberryPi.org.

Setting WIFI up via the command line

From RaspberryPi.org.

hostapd.conf – w1.fi

An anotated version of hostapd.conf.

Update 7/26/2017: I hope I’ve fixed most of the crappy formatting.

©2017, David B. Snyder

Advertisements