Skip to content

3G module with local access point

Angel Pappas edited this page Oct 1, 2015 · 4 revisions

#How to create a local access point which will be connected to the internet through a 3G module

##Connect the 3G module

Install some packages:

sudo apt-get update

apt-get install wvdial ppp usb-modeswitch

Connect the usb 3G module to the raspberry. Afterwards, run the command:

sudo wvdialconf /etc/wvdial.conf

Then, edit the file /etc/wvdial.conf. The internet option is the APN of the provider. The Phone, Username and Password are provider specific. The contents must be something like this:

[Dialer Defaults]
Modem = /dev/ttyACM0
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","internet"
Stupid Mode = 1
Modem Type = USB Modem
Phone = *99#
ISDN = 0
Username = { }
Password = { }
Baud = 460800

Start the wvdial

sudo wvdial Defaults &

This will create a network interface with name ppp0.

##Create the access point

Install the software:

sudo apt-get -y install iw isc-dhcp-server hostapd

Edit the file /etc/network/interfaces. Replace the section indicated by wlan0 with the following:

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.255.254
netmask 255.255.0.0

Edit the file /etc/dhcp/dhcpd.conf. Replace everything with the following:

ddns-update-style none;
authoritative;
log-facility local7;
subnet 192.168.0.0 netmask 255.255.0.0 {
  range 192.168.0.1 192.168.255.253;
  option broadcast-address 192.168.255.255;
  option routers 192.168.255.254;
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "local";
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Edit the file /etc/default/isc-dhcp-server. Replace everything with the following:

INTERFACES="wlan0"

Edit the file /etc/hostapd/hostapd.conf. Change the LEOFOREIO and leoforeio entries, if needed. Replace everything with the following:

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

Edit the file /etc/default/hostapd. Replace everything with:

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

Stop these services from booting during the boot sequence:

sudo update-rc.d -f hostapd remove
sudo update-rc.d -f isc-dhcp-server remove

Start the services:

sudo service networking restart
# or
#sudo ifconfig wlan0 192.168.255.254 netmask 255.255.0.0
sudo service isc-dhcp-server restart
sudo service hostapd restart

##Forward packets from the access point to the 3g module

Edit the file /etc/sysctl.conf. Change the line with net.ipv4.ip_forward to the following:

net.ipv4.ip_forward=1

The above will take affect after a reboot. In order to take affect immediately, run:

sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

In order to forward the packets, we use the iptables command:

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

##Make the above configuration start during boot.

Edit the file /etc/rc.local, or place the commands bellow in a file that will be run on startup. Add the lines:

sudo wvdial Defaults &
sudo ifconfig wlan0 192.168.255.254 netmask 255.255.0.0
sudo service isc-dhcp-server restart
sudo service hostapd restart
sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
sudo iptables -A FORWARD -i ppp0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i wlan0 -o ppp0 -j ACCEPT