Blog
OpenEnergyMonitor

Using emonPi Offline: How to setup a WIFI Hotspot on an emonPi

This guide details how to setup a WIFI Hotspot using the Edimax USB WIFI adapter on an EmonPi or EmonBase to make it possible to connect directly to an EmonPi from a tablet or computer without the need for a router or internet connection.

The guide is based on Dave Conroy's guide here but also covers setting up a DHCP server rather than ethernet to WIFI bridge.

There are 3 pieces of software that need to be installed to get this to work:
  • hostapd – WIFI Hotspot
  • edimax version of hostapd
  • isc-dhcp-server – DHCP Server
1) Install dhcp server 

sudo apt-get install isc-dhcp-server 

2) Install Edimax version of hostapd

Either use precompiled binary or compile yourself:

Pre-compiled (update: does not seem to work with wheezy, use self compile see below)
wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
unzip hostapd.zip
sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
sudo mv hostapd /usr/sbin/hostapd.edimax
sudo ln -sf /usr/sbin/hostapd.edimax /usr/sbin/hostapd
sudo chown root.root /usr/sbin/hostapd
sudo chmod 755 /usr/sbin/hostapd


Compile yourself (recommended for wheezy, tested March16)

sudo apt-get install libnl-genl-3-dev libnl-3-dev
git clone https://github.com/lostincynicism/hostapd-rtl8188
cd hostapd-rtl8188/hostapd
sudo make
sudo make install 

3) Configure /etc/network/interfaces

Open the network interfaces file to edit:

sudo nano /etc/network/interfaces

Replace content with:

auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

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


iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0

allow-hotplug eth1
iface eth1 inet dhcp

If required add bridge to bridge eth0 to wlan0 - although it's better to use NAT iptables forwarding since then we can still access pi via ssh (see below)

auto br0
iface br0 inet dhcp
bridge_ports eth0 wlan0
pre-up ifconfig eth0 0.0.0.0 up
pre-up ifconfig wlan0 0.0.0.0 up
pre-up brctl addbr br0
pre-up brctl addif br0 eth0
post-down ifconfig wlan0 0.0.0.0 down
post-down ifconfig eth0 0.0.0.0 down
post-down brctl delif br0 eth0
post-down brctl delbr br0

4) Configure /etc/hostapd/hostapd.conf
Open hostapd config file with:

sudo nano /etc/hostapd/hostapd.conf

Add the following lines to hostapd.conf (set SSID and password of your choice)

WITHOUT AUTHENTICATION  USE

interface=wlan0
ssid=emonPi
driver=rtl871xdrv
hw_mode=g
channel=6
auth_algs=1
wmm_enabled=0

WITH_AUTENTICATION USE

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


Manual start to test everything is working, you should see a Wifi network called 'emonPi':

$ sudo hostapd -dd /etc/hostapd/hostapd.conf



5) Configure /etc/default/hostapd

sudo nano /etc/default/hostapd

Set

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


6) Configure /etc/dhcp/dhcpd.conf
Open dhcpd config file:

sudo nano /etc/dhcp/dhcpd.conf

Add the following to the end of the file:

lease-file-name "/home/pi/data/dhcpd.leases";
subnet 192.168.42.0 netmask 255.255.255.0 {
        range 192.168.42.2 192.168.42.20;
        option broadcast-address 192.168.42.255;
        option routers 192.168.42.1;
        default-lease-time 600;
        max-lease-time 7200;
        option domain-name "local";
        option domain-name-servers 8.8.8.8, 8.8.4.4;
}

comment out #option domain-name "example.org";

#option domain-name-servers ns1.example.org, ns2.example.org;

uncomment

authoritative;

7) Configure /etc/default/isc-dhcp-server

sudo nano /etc/default/isc-dhcp-server 

Remove the comment from DHCPD_CONF, DHCPD_PID and then set the INTERFACES to "wlan0" (including ""), see example below:

# Defaults for isc-dhcp-server initscript
# sourced by /etc/init.d/isc-dhcp-server
# installed at /etc/default/isc-dhcp-server by the maintainer scripts
#
# This is a POSIX shell fragment
#

# Path to dhcpd's config file (default: /etc/dhcp/dhcpd.conf).
DHCPD_CONF=/etc/dhcp/dhcpd.conf

# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
DHCPD_PID=/var/run/dhcpd.pid

# Additional options to start dhcpd with.
#       Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
#       Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="wlan0"



8.) Setup NAT forwarding to bridge eth0 the wlan0 while still allowing us to access the Pi via SSH (pure network bridge does not)

To set this up automatically on boot, edit the file /etc/sysctl.conf and add the following line to the bottom of the file:

net.ipv4.ip_forward=1

Second, to enable NAT in the kernel, run the following commands:

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT


If bride is also required to eth1 (for 3G dongle) add:
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o eth1 -j ACCEPT




sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Now edit the file /etc/network/interfaces and add the following line to the bottom of the file:

up iptables-restore < /etc/iptables.ipv4.nat # This did not work for me I had to add entry to rc.local (see below)

8) Edit /etc/rc.local

sudo nano /etc/rc.local

Add before exit 0 the following lines to tell the wlan interface to use ip address 10.0.1.1 and then start the isc-dhcp-server:

sudo iptables-restore < /etc/iptables.ipv4.nat
sudo ifconfig wlan0 192.168.42.1

sudo service isc-dhcp-server restart
sudo iptables-restore < /etc/iptables.ipv4.nat



9) Start services at boot


sudo update-rc.d isc-dhcp-server defaults
sudo update-rc.d hostapd defaults



Thats it restart your emonpi/emonbase to finish and then connect to network emonPi with password: raspberry

11) Add a Real Time Clock (RTC)
When running an emonPi in offline mode we recommend adding a hardware RTC to ensure the system time is always correct. See emonPi Wiki:



Hardware RTC





Resources 
Useful blogs, guides and forum threads that I used to work out how to set up the above:
http://www.daveconroy.com/turn-your-raspberry-pi-into-a-wifi-hotspot-with-edimax-nano-usb-ew-7811un-rtl8188cus-chipset
http://askubuntu.com/questions/140126/how-do-i-configure-a-dhcp-server
http://ubuntuforums.org/showthread.php?t=2068111
http://raspberrypi.stackexchange.com/questions/9678/static-ip-failing-for-wlan0
https://forums.opensuse.org/showthread.php/438756-DHCP-can-t-write-to-dhcpd-leases
http://elinux.org/RPI-Wireless-Hotspot
http://askubuntu.com/questions/140126/how-do-i-configure-a-dhcp-server
http://www.instructables.com/id/Raspberry-Pi-as-a-3g-Huawei-E303-wireless-Edima/?ALLSTEPS


To engage in discussion regarding this post, please post on our Community Forum.