Raspberry Pi Zero as Hotspot to control LEDs

I wanted to be able to control my Raspberry Pi Zero via my phone outside of my house where there isn’t any network infrastructure (i.e. The Pi Zero doesn’t have to connected to any public or private WIFI).

I plan on using a USB power battery to run this when I’m on the go. What I did was use the Pi Zero to create a WIFI hot-spot, connect to it, and use a webserver on the PI to control a LED. (I call this a WIFI hotspot, but it doesn’t really connect to the Internet—only the Pi Zero)

I have a few ideas for other projects etc and decided to build this as a test system. I took a few days to get all of this going, but using the instructions below took only about an hour (most of the time was burning the Jesse Lite image and doing apt-get update/upgrade)

Here goes: There are a million ways to get started.

Burn Image/initial start-up

The first thing I did was burn a Jesse Lite image to my SD card and boot up my Pi Zero. I connected my PI to a monitor using pin jumpers connected to the TV (RCA video) connectors directly on the Pi Board.

Using an OTG USB cable connected to the PI, I connected a USB Hub, a RaLink Chipset WiFi Dongle, and a Keyboard After booting and getting logged in, I ran raspi-config and did the usual settings changes: Use full partition, change password, set location, etc.

The most important for me was changing the Local Settings and Keyboard setting so the my keyboard will work properly. (I set things to En_US-UTB8, generic 104 US English keyboard).

If you are in the USA and skip this step you will not be able to type in double quotes. It asked if I wanted to reboot, YES I did.

Connect to wifi using command line

my source: https://www.raspberrypi.org/documentation/configuration/wireless/wireles...

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Go to the bottom of the file and add the following:

network={ ssid="Your SSID" psk="Your_wifi_password" }

I reboot the Pi Zero, and then when everything was back up I saw that it connected to my WIFI.

Then I ran
sudo apt-get update

and then

sudo apt-get upgrade

This took a LONG time

Reboot sudo reboot

Installing webserver (lighttpd)

(my source was http://www.penguintutor.com/linux/light-webserver )

To install the lighttpd web server issue the command.

sudo apt-get install lighttpd

This will install the web server and also pull in any other packages (called dependencies) that are required. The server will be automatically started and set to start by default after a reboot.

[ ok ] Starting web server: lighttpd.

Test the webserver

Make sure you are connected to the same WIFI system as your Pi Zero

Open browser to ip of your zero, you should see a placeholder screen like this one:

Install PHP

Perl is installed as part of the operating system so I will just be adding PHP, which is an interpreted programming language. You don’t really have to install PHP, but I’m very familiar with it from all the web/CMS stuff I’ve done over the years, so I used PHP to create this test.

The following commands will install PHP version 5.

sudo apt-get install php5-common php5-cgi php5

Note it's important to install in the order listed above. If you try to install php5 without first installing the php5-cgi package then it will install Apache as well, which we don't want for this light-weight lighttpd server.

To enable the server to handle php scripts the fastcgi-php module should be enabled by issuing in the command

sudo lighty-enable-mod fastcgi-php

Then reload the server using

sudo service lighttpd force-reload

You can test webserver again, just in case.

Wiring Pi

Ok, so now comes the fun part. Controlling stuff via the Pi Zero GPIO. I’ve done some other projects using WebioPI, but it seems like that isn’t being maintained. I decided to go with Wiring Pi. (I’m really glad I did)

Here is where I got my WiringPI info from: http://wiringpi.com/download-and-install/

First you need to install GIT

sudo apt-get install git-core

Then get WiringPi using GIT:

git clone git://git.drogon.net/wiringPi

To build/install there is a new simplified script:

cd wiringPi ./build

Test wiringPi’s installation run the gpio command to check the installation:

gpio -v gpio readall

That should give you some confidence that it’s working OK.

Now you'll see why I like Wiring PI so much!

Connecting LEDs to the PI

Ok, so here is the LED connecting PHP sample I drew from: http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi

To put Wiring Pi to practical use, they created a super-simple PHP app for controlling an LED. Connect an LED with a resistor to GPIO pin 17 and GND.

switch to the

/var/www/html directory (the instructions on the pi-geek website had the directory wrong)

Then, use the command:

sudo nano gpio.php

 to create the gpio.php file for editing. Here is a link to the code: Simple PHP App to Control an LED

Make sure you remove the line numbers! If you have your resister and LED connected you can now test that it works by going to your browser and entering:

http://YourPIsIPaddress/gpio.php

You should see this simple screen that allows you to turn on and off the LED

 

If you are able to turn on and off the light you are almost there.

Now we have to install an access point server, and a DHCP server, and set the PI to a static IP address

I really liked this for getting the access point going: http://rpi.vypni.net/wifi-ap-rt5370-on-raspberry-pi/

First get into Super User Mode:

sudo su

Check on USB WIFI to make sure it is the right chipset

lsusb

Install hostapd and dnsmasq:

aptitude install hostapd dnsmasq

set to a static IP

nano /etc/network/interfaces

allow-hotplug
wlan0 iface wlan0 inet static
address 192.168.100.1
netmask 255.255.255.0

DON’T REBOOT yet—as you’ll lose your network connection if you are SSH’d into your pi… If you are using a keyboard attached to the PI/monitor than you can reboot to see if the static IP gets set.

Configure Access Point using Hostapd

Create new configuration for hostapd in /etc/hostapd/hostapd.conf

nano /etc/hostapd/hostapd.conf

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

Edit files /etc/default/hostapd

nano /etc/default/hostapd
and uncomment DAEMON_CONF, and add path to config.
So line will looks like this:

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

Then, you can finally restart hostapd, and you have your hotspot up and running.

/etc/init.d/hostapd restart

This will set hostpad to run every time:

update-rc.d hostapd enable

You should be able to see you hotspot on your computer as an available network, but without the DHCP server below, you can’t get an IP address automatically assigned

Setup DHCP Server using dnsmasq

edit dnsmasq by following command

nano /etc/dnsmasq.conf

add this on end of the file:

interface=wlan0
except-interface=eth0
dhcp-range=192.168.100.2,192.168.100.150,255.255.255.0,12h

Sudo reboot

Now you system should reboot, take the static IP address you assigned (I used 192.168.100.1), be hosting an access point using the SSID you set.

Connect to the SSID; enter the password you set as the PSK. Open the browser to the IP address you set for your PI and you should see the placeholder page Goto YourPiPIAddress/gpio.php to control your LED using only the PI Zero!