A PORTABLE HOTSPOT WITH WATCHDOG USING RASPBERRY Pi
Autor: Carlos Roberto | Publicado: 2015-11-09T19:18:00-08:00


This project is um update of Hotspot Portatil that is another post of mine. While using the hot spot I decided to add the watchdog and write another post.&nbsp;
With the massive use of mobile devices, the demand for
open hot spot or wifi has become common among users of portable devices.
Some time ago I was having a few days on the beach in a house where my only access
point to internet was a 3G USB modem from Huawei Technologies, model E173s.&nbsp;In
that occasion I was using 2 laptops and 1 smartphone Samsung. To switch the
internet connection from one laptop to another, what I had to do was just move the
3G USB modem from one laptop to another. But connect the smartphone to internet
was a bit more complicated and any solution would require to keep the laptop turned
on to allow smartphone to connect to internet.&nbsp;So, to avoid all these
inconvenient, I did remember that I had a Raspberry kit Pi B + with me. Then
after taking a tour of the internet, I decided to put all the things together and
create a hotspot with the RPi, which would help me to connect any device to the
Internet, without the need to keep switching the modem from one device to
another and also would avoid to keep the laptop working. Cool. But you could
ask why to make a hotspot if you can do the same thing enabling the Access
Point in the smartphone?&nbsp;Good question.&nbsp;But the answer is pretty
convincing (at least in Brazil).&nbsp;For example, if you want to buy a 10 Gb
package for your cell phone, you will pay 3-4 times more than if you buy the
same 10 Gb of data to 3G/4G USB modem. In other words, the price for data is
cheap than the price for voice data.

The other question is: why to make a watchdog? Well, many
times when the connection dropped I had to turn off / turn on the RPi to make
the hotspot work again. The watchdog makes the same job, automatically. You
will find all the configuration files and shell scripts used in this project in
github here.
Well, let’s go to our project.&nbsp;The
material used was:&nbsp;

Software:
Linux Raspberrypi 3.18.5+&nbsp;



Hardware:&nbsp;

Raspberry Pi B + V 1.2&nbsp;

USB modem Huawei 3G, model
E173s&nbsp;

USB wireless modem Ralink Technologies, model RT530

Final view of the project
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->





1. Switching Modem
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

Some modems have two modes of operation: usb flash
memory mode and usb modem mode.
That was the first problem I encountered: I could not do my 3G modem switch from
flash memory to 3G modem when connected to the RPI.&nbsp;After trying several
options, the only solution that worked was as follows: 

I connected the modem to a Windows laptop and, via the
Control Panel&gt; Device Manager, I noticed it was using COM4. Then with
putty.exe utility, I opened a terminal window, as shown below.

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->





In the terminal window, I typed the command "AT ^
U2DIAG = 0" without quotes.&nbsp;Then plugged the modem back to RPi and
figured that, finally, the modem was switched to the correct interface, I mean,
modem mode vendor = 0x12d1 and product = 0x1c05, as shown below:



With the 3G modem being recognized by RPI kernel, now
we need to configure it.

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

2. Setting up the 3G modem on Raspberry Pi

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

There are numerous tutorials on the Internet that can help
on this task.
The starting point is to first find out if the modem is installed correctly in
the RPi.&nbsp;This can be done by checking the messages dmesg command shown below
and the information displayed by the lsusb command, as shown below.
<!--[endif]-->






When you run the lsusb command, the output shows that the modem mode is
connected.
<!--[endif]-->





In the picture above you can see the 3G modem ID and
the status is on, meaning that the modem has been correctly identified by the
RPi.&nbsp;



2.1.&nbsp;Install the necessary software
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

To install the packages needed for this project,
execute the following command.&nbsp;Note that the&nbsp;most used commands
require login&nbsp;root&nbsp;or&nbsp;sudo
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

pi@raspberrypi
~ $
apt-get install ppp wvdial hospapd udhcpd



Once installed the
packages, run the command:&nbsp;

pi@raspberrypi ~ $
wvdialconf
/etc/wvdial.conf


The tool will do a check on your 3G modem, running a
serie of AT commands and in the end will generate the file /etc/wvdial.conf
with a default setting that you can edit and replace or add desired values in
the configuration or add a new configuration, using the one as a default
template.&nbsp;Follow is the setting of my wvdial.conf file.&nbsp;

[Dialer 3gcon]
Init1 = ATZ
Init2 = AT &amp; C1 &amp; D2 &amp; FE0V1X1 S0 = 0
Init3 = AT + CGDCONT = 1, "IP", "zap.vivo.com.br"
Password = &lt;Your Password&gt;
Username = &lt;Your Login Name&gt;
Modem = /dev/ttyUSB0
Check Def Route = on
Abort on No Dialtone = off
Modem Type = Analog Modem
Stupid Mode = 1
Baud = 460800
Dial Command = ATDT
ISDN = 0
#Phome * 99 # =
Phone = * 99 *** 1 #&nbsp;
[Dialer Defaults]
Init2 = ATQ0 V1 E1 S0 = 0 &amp; C1 &amp; D2
Modem Type = Analog
Modem;&nbsp;Phone = &lt;Target Phone Number&gt;
ISDN =
0;&nbsp;Username = &lt;Your Login Name&gt;
Init1 =
ATZ;&nbsp;Password = &lt;Your Password&gt;
Modem = /dev/ttyUSB0
Baud = 9600

Now run the command to connect your modem.&nbsp;

pi@raspberrypi ~ $
wvdial
3gcon



You should get a screen similar to the figure below.&nbsp;If
everything is right at this point the RPi already have internet access via 3G
USB modem.&nbsp;Now let's connect the hotspot internet.
<!--[endif]-->





3. Setting up the wireless modem (hotspot)
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

Udhcpd is a small DHCP server widely used in embedded
systems.
Hostapd is a program for managing access points (AP) and wireless
authentication servers.



3.1.&nbsp;Configure the DHCP server
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

Edit the file /etc/udhcpd.conf.&nbsp;In my case, see my
final udhcpd.conf&nbsp;


# Sample udhcpd configuration file (/etc/udhcpd.conf)
# The start and end of the IP lease block&nbsp;
start 192.168.1.20 #default: 192.168.0.20
end 192.168.1.254 #default : 192.168.0.254&nbsp;
# The interface que udhcpd will use
wlan0 #default interface: eth0&nbsp;
# If remaining is true (default), udhcpd will store
the team
# remaining for each lease in the udhcpd leases file.&nbsp;This is
# for embedded systems que can not keep time between reboots.
# If you September to remaining in, the absolute team que the lease
# expires at will be stored in the dhcpd.leases file.&nbsp;
Remaining #default yes: yes&nbsp;
# The location of the leases file&nbsp;
lease_file /var/lib/misc/udhcpd.leases #defualt:
/var/lib/misc/udhcpd.leases&nbsp;
# The location of the pid file&nbsp;
pidfile /var/run/udhcpd.pid #default: /var/run/udhcpd.pid&nbsp;
opt dns 8.8.8.8 8.8.4.4 192.168.10.2 192.168.10.10
#opt dns
option subnet 255.255.255.0
opt router 192.168.1.1
option domain site
option lease # 864000 10 days of seconds

in start / stop you define the range IPs for their
customers.&nbsp;The wireless modem interface was defined as wlan0 (use ifconfig
to know the interface used by the modem).&nbsp;Also set the IP address
192.168.1.1 as the IP of my router.&nbsp;
Create an empty file for the DHCP lease that
will be filled later by udhcpd.&nbsp;

pi@raspberrypi ~ $
touch
/var/lib/misc/udhcpd.leases


Enable udhcpd permanently , commenting the following
line in /etc/default/udhcpd&nbsp;

pi@raspberrypi ~ $
nano
/etc/default/udhcpd

# DHCPD_ENABLED = No&nbsp;


Set the udhcpd to start automatically at boot.&nbsp;Use
the command below.&nbsp;

pi@raspberrypi ~ $ update-rc.d udhcpd enable


Edit /etc/network/interfaces and
set the new interface that will be used to access the wireless modem.&nbsp;In
my case, I used the wlan0 interface, which used static IP or IP 192.168.1.1, that
was set in the file udhcpd.conf shown above.&nbsp;If there is already a
definition for wlan0, comment and update.&nbsp;Here is how was my interfaces
file.&nbsp;

Auto lo
iface it inet loopback
iface eth0 inet dhcp&nbsp;
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255&nbsp;
# allow-hotplug wlan0
#iface wlan0 inet manual
#wpa -roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp&nbsp;
up iptables-restore
&lt;/etc/iptables.ap



3.2.&nbsp;Configure the server hostapd
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

/etc/hostapd/hostapd.conf Edit the file as follows&nbsp;


# interface used by access point
interface = wlan0&nbsp;
# firmware driver
driver = nl80211&nbsp;
# access point SSID
ssid = HotPi&nbsp;
# operator mode (a = IEEE 802.11a, b = IEEE 802.11b, g
= IEEE 802.11g)
hw_mode = g&nbsp;
# access point channel
channel = 11&nbsp;
macaddr_acl = 0
auth_algs = 1
ignore_broadcast_ssid = 0&nbsp;
# key management algorithm
wpa_key_mgmt = WPA-PSK
wpa_passphrase = &lt;your passkey&gt;
WPA 2 =&nbsp;
# set ciphers
wpa_pairwise = TKIP
rsn_pairwise = CCMP

You can change the ssid, the channel and wpa_passphrase
to values ​​of your choice.&nbsp;Note that the ssid is the wi-fi name that will
be propagated as a hotspot that other devices can connect to internet.&nbsp;If
you want to create an open network, simply comment wpa_passphrase parameter.
Edit the /etc/default/hostapd and make the following change&nbsp;

# DAEMON_CONF = ¨¨
to
# DAEMON_CONF ¨ = /etc/hostapd/hostapd.conf¨&nbsp;

Set hostapd server for automatic start at boot, which can be done by executing
the command below.&nbsp;

pi@raspberrypi ~ $ update-rc.d enable hostapd



4. Forwarding via NAT (Network Address Translation)

The last part of our project to allow it work
is to promote the redirection of external data network (3G modem in ppp0) to
inter-access point (wireless modem in wlan0) and vice versa, for all connected
devices.&nbsp;This can be done by setting specific rules for NAT.&nbsp;Enable
IP forwarding for editing the /etc/sysctl.conf&nbsp;



pi@raspberrypi ~ $ nano
update-rc.d /etc/sysctl.conf



net.ipv4.ip_forward = 1&nbsp;



Set up the necessary rules for routing iptables&nbsp;



pi@raspberrypi ~ $ iptables&nbsp;-t nat -A POSTROUTING -o ppp0 -j
MASQUERADE&nbsp;

pi@raspberrypi ~ $ iptables -A
FORWARD -i -o ppp0 -m state wlan0 --state RELATED, ESTABLISHED -j ACCEPT&nbsp;

pi@raspberrypi ~ $ iptables -A
FORWARD -i wlan0 -o ppp0 -j accep&nbsp;



Save the rules created in iptables to a file&nbsp;



pi@raspberrypi ~ $ sh -c
iptables-save&gt; /etc/iptables.ap¨&nbsp;



In each boot is necessary to restore the rules of
iptables.&nbsp;This can be done automatically by adding the interfaces file a
command to restore iptables, which has already made ​​some steps back.&nbsp;



pi@raspberrypi ~ $ iptables-restore up
&lt;/etc/iptables.ap&nbsp;





5.&nbsp;The watchdog&nbsp;

As I said in the beginning, I decided to add a watchdog
because, depending on the modem signal, the hotspot drops and then need to be
restarted. The RPi does not have a restart button and so I had to turn off / on
the device all the times that it stopped to work. Using a watchdog, it was
possible to do a more friendly restart automatically and avoiding a complete
reboot.

The watchdog is configured in crontab to check the
pppd process each 1 minute and if it is not running, run the wvdial command in
background. In addition, it generates a log file named check-hotspot.log with
the following format: date/time, RX bytes, TX bytes, status, where status
means: 0 – process restarted, 1 – modem working.
Here is the crontab line I am using to run the watchdog








So, checking the log file it is possible to know when the
modem dropped and was restarted. Below you can see the code.



#!/bin/bash

#
Watch Dog for Host Spot device

#
Author: carlosrl@gmail.com

#
Nov/09/15 

#
Version 1.0

#
Description: The watchdog will check the behavior of modem connected to
pre-defined interface and 

#
will call wvdial tool if the refered modem is not communicating. This will work
as a "restart modem process"

###



#
Init varibles



iTX=0

iRX=0

iRXTX=""

fRX=0

fTX=0

fRXTX=""

STATUS=0

LOG_DIR=/var/log

LOGFILE="/var/log/check-hotspot.log"

DEV="eth0"



#
Run as root, of course.



if
[ "$UID" -ne 0 ]; then

&nbsp;&nbsp;&nbsp; echo "Must be root to run this
script."

&nbsp;&nbsp;&nbsp; exit $E_NOTROOT

fi



#
-- Gathering data from interface



if [
-n "$1" ]; then

#
Test whether command-line argument is present (non-empty).

&nbsp;&nbsp;&nbsp; dev=$1

else

&nbsp;&nbsp;&nbsp; dev=$DEV # Default, if not specified on
command-line.

fi



#
-- Verify that interface is streaming data



iRXTX=`ifconfig
$dev | awk '/RX bytes/ { $2=substr($2,7);&nbsp;
$6=substr($6,7); printf("%i %i", $2, $6) }' `

iRX=`echo
$iRXTX | cut -d' ' -f1`

iTX=`echo
$iRXTX | cut -d' ' -f2`



#
-- Gathering data from log file



FTIME=`date
+%m/%d/%y-%H:%M:%S`



if
[ -e "$LOGFILE" ]; then

&nbsp;&nbsp;&nbsp; #echo "file $LOGFILE found"

&nbsp;&nbsp;&nbsp; fRXTX=$(tail -n 1 $LOGFILE)

&nbsp;&nbsp;&nbsp;
#echo $fRXTX

&nbsp;&nbsp;&nbsp; if
[ -n "$fRXTX" ]; then

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fRX=`echo $fRXTX | cut -d' ' -f2`

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fTX=`echo $fRXTX | cut -d' ' -f3`

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; STATUS=`echo $fRXTX | cut -d' ' -f4`

&nbsp;&nbsp;&nbsp; fi

else

&nbsp;&nbsp;&nbsp; # create a new log file

&nbsp;&nbsp;&nbsp; echo "$FTIME $iRXTX 1" &gt; $LOGFILE

&nbsp;&nbsp;&nbsp;
exit 8

fi

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # check if modem process is running



proc=`ps axl | grep -E "pppd" |
grep -E "modem" | grep -E "wvdial"`

if
[ -n "$proc" ]; then

&nbsp;&nbsp;&nbsp; # hotspot is running. Just update log file



&nbsp;&nbsp;&nbsp; echo "$FTIME $iRXTX 1" &gt;&gt;
$LOGFILE

&nbsp;&nbsp;&nbsp; exit 4

else

&nbsp;&nbsp;&nbsp; # modem process not running. Lets restart
it.



&nbsp;&nbsp;&nbsp; echo "$FTIME $iRXTX 0" &gt;&gt;
$LOGFILE

&nbsp;&nbsp;&nbsp; /usr/bin/wvdial &amp;

&nbsp;&nbsp;&nbsp; exit 10

fi driver
= nl80211&nbsp;



6.&nbsp;Reboot&nbsp;



Finally, reboot the Raspberry Pi.&nbsp;After the
reboot, run the ifconfig command and verify that the interfaces have the
correct IP and are transmitting the data.&nbsp;Below is my ifconfig:
<!--[endif]-->






<!--[endif]-->

See that ppp0 interface (3G modem) is receiving and
sending data.&nbsp;The same goes for wlan0 interface, which means that data is
flowing to / from the Internet (external network) to / from devices connected
to the hotspot (internal network).
To make a check, connect a laptop, tablet or smartphone and try to connect to
the hotspot.
The figure below I connected a smartphone to the hotspot created here, ie
HotPi.

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->






<!--[endif]-->

The figure shows the final result achieved with the
Raspberry Pi connected to the monitor configuration.&nbsp;The range of the
hotspot signal provided by RT530 wireless modem, was around 30m which is quite
reasonable for use in a house.
For use on a daily basis, you do not need mouse, keyboard or a monitor device attached
to RPi. You will find all the configuration files used in this project in
github here.&nbsp;








7. References

In this project, I searched some
blogs and documents found on the internet.&nbsp;Among the many found, I list here the ones used as reference


http://elinux.org/RPI-Wireless-Hotspothttps://www.thefanclub.co.za/how-to/how-setup-usb-3g-modem-raspberry-pi-using-usbmodeswitch-and-wvdial


http://www.revsys.com/writings/quicktips/nat.html