Painless Wi-Fi for OctoPrint

We all love Raspberry Pi. But everywhere I go, I hear painful stories about setting up Wi-Fi on Raspberry Pi.

We all love OctoPrint. But everywhere I go, I hear painful stories about setting up Wi-Fi for OctoPrint.

If you are nodding your head, this post is for you. I’ll describe a few techniques that will make dealing with Wi-Fi in your OctoPrint as painless as it can get.

Set up Wi-Fi without monitor, keyboard, or mouse

When you set up OctoPrint on Raspberry Pi for the first time, or you move your Pi to a new place with different Wi-Fi network, what are the first things you scramble for? The answer: HDMI monitor, USB keyboard and mouse to set up Wi-Fi connection.

Not anymore. There are actually a mechanism (shameless brag: I did it) in latest OctoPi that allows you to add new Wi-Fi connection/password to your SD card without even booting into your Pi!

Neat.

And here is how you do it.

  • Download latest OctoPi image here. This link is a pre-release build of the latest version. Install it as if you were installing an original Raspbian image.
  • In the boot partition of the newly-made SD card, you will find a text file called octopi-wpa-supplicant.txt. Open it with your favorite text editor. Uncomment a network block so that it reads:
network={
ssid="Your Wifi SSID"
psk="supersecretwifipassword"
}

Of course, replace the Wi-Fi SSID and password with the real ones.

  • As explained in the comments in octopi-wpa-supplicant.txt, you can have as many network blocks as you want. Your Pi will try to connect to each of them until it has a successful connection or exhaust the list.

Important notes:

  • If you don’t enjoy going down the rabbit hole to figure out why things don’t work as expected, double-check the file to make sure the quotes are ", not those “smart quotes”. This is true for all the text editing mentioned in this post.
  • Do NOT use Change Wi-fi Country in raspi-config . A subtle bug in raspi-config would render octopi-wpa-supplicant.txt in-effective. Instead, just open octopi-wpa-supplicant.txt in an editor and directly change country setting.

Turns your Pi into a Wi-Fi hotspot, automatically

Admit it. There are just times when you can’t get hold of any Wi-Fi connection (yeah it’s 2017 but…), or you are too lazy to even edit that little octopi-wpa-supplicant.txtfile. Fine. With a little bit of work upfront, you can tell your Pi to start a Wi-Fi hotspot when it can connect to none Wi-Fi networks. Now you can connect to OctoPrint anywhere and get your 3D printer to work — even there is no Wi-Fi whatsoever.

  • Connect to your Pi on which OctoPrint runs. You can ssh, login through serial console or GUI, what have you.
  • Install software needed to run Wi-Fi hotspot:
$ sudo apt-get update
$ sudo apt-get install hostapd dnsmasq iw
$ sudo systemctl disable hostapd dnsmasq
  • Configure hostapd

$ sudo nano /etc/hostapd/hostapd.conf

and copy-paste the following:

interface=wlan0
driver=nl80211
ssid=OctoHotspot
hw_mode=g
channel=6
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=1234567890
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Press control-x to save the file.

$ sudo nano /etc/default/hostapd

to change:

#DAEMON_CONF=""
to
DAEMON_CONF="/etc/hostapd/hostapd.conf"

Again press control-x to save the file.

  • $ sudo nano /etc/dnsmasq.conf

to add to the bottom of the file:

#OctoHotspot Config
#stop DNSmasq from using resolv.conf
no-resolv
#Interface to use
interface=wlan0
bind-interfaces
dhcp-range=10.0.0.3,10.0.0.20,12h
  • $ sudo nano /etc/network/interfaces

to add # in front of wpa-conf for wlan0so it looks like:

allow-hotplug wlan0
iface wlan0-raspbian inet manual
# wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
  • Finally, add a script to start Wi-Fi hotspot when failing to connect to any existing Wi-Fi networks.

$ sudo nano /usr/bin/autohotspot

and copy-paste:

#!/bin/bash
#
createAdHocNetwork()
{
ip link set dev wlan0 down
ip a add 10.0.0.5/24 dev wlan0
ip link set dev wlan0 up
systemctl start dnsmasq
systemctl start hostapd
}
connected=false
while read ssid
do
if iw dev wlan0 scan ap-force | grep $ssid > /dev/null
then
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null 2>&1
if dhclient -1 wlan0 && iwconfig wlan0 | grep $ssid > /dev/null
then
connected=true
break
else
wpa_cli terminate
break
fi
else
echo "Not in range, WiFi with SSID:" $ssid
fi
done <<< "$(grep -i '^[^#].*ssid=' /etc/wpa_supplicant/wpa_supplicant.conf | sed -n 's/.*ssid="*\([^"]*\)"*/\1/ip')"
if ! $connected; then
createAdHocNetwork
fi

Important: Please make sure you copy-paste the content above, rather than typing it. Some long commands that have to be in the same line are displayed as if they were in multiple lines.

Create a new service file

$ sudo nano /etc/systemd/system/autohotspot.service

[Unit]
Description=Generates a non-internet Hotspot for ssh when no wifi network is in range.
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/autohotspot
[Install]
WantedBy=multi-user.target

Activate the service so that it’ll run the script when system boots up:

$ sudo chmod +x /usr/bin/autohotspot
$ sudo systemctl enable autohotspot.service

Whew, that’s it! Next time when you boot up OctoPrint in a new place with no known Wi-Fi networks, open up your computer and look for Wi-Fi with the name OctoHotspot, which is the new Wi-Fi network your little Pi creates! Connect to it using password 1234567890. Once that is done, your old friend OctoPrint will be available at http://10.0.0.5/. Fire up your browser and print away!

Special thanks to roboberry for his original post on setting up auto Wi-Fi hotspot.