Skip to content

Network

Stefano Zenaro edited this page Apr 19, 2019 · 1 revision

Network


We are going to install hostapd and setting it up to create a standalone network where the node MCUs are going to connect to. We decided to not use the dhcp functionality, we are going to set a static IP on each Node MCU

These instructions are taken from the official guide on the raspberrypi.org website. We skipped the dhcp part

Preliminary Fases

Update raspbian


It is important to be sure that we are using the latest updates of raspbian. To do all the updates, execute this commands on the terminal:

sudo apt-get update
sudo apt-get upgrade -y

Install Hostapd


Install hostapd and confirm the installation with -y:

sudo apt-get install hostapd -y

Since we have to change hostapd's configuration file, we need to stop the hostapd service:

sudo systemctl stop hostapd

Reboot


Reboot to make sure that everything is running up to date

sudo reboot

Set a static IP


Since the raspberry is going to be a server we need to set a static ip on its wireless interface (wlan0).

In this example the network is going to have the address "192.168.4.0", this means that the raspberry is going to have the address "192.168.4.1" and its connected devices are going to have the address "192.168.4.x" (x is a number between 2 and 254)

Open the "/etc/dhcpcd.conf" file with the "nano" text editor:

sudo nano /etc/dhcpcd.conf

Go to the end of the file and add these lines:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Change the second line according to the ip address you want to set

"/24" is the CIDR number that indicates how many bits define the network, it corresponds to the “255.255.255.0” subnet mask

If at the end of this guide you still don't have a static ip, check the end of this page

Restart the dhcpcd service to use the new ip address:

sudo service dhcpcd restart

If the terminal tells you that you have to execute a command because some settings have changed, type the command:

sudo systemctl daemon-reload

Set hostapd


Open the ‘/etc/hostapd/hostapd.conf’ file with the "nano" editor:

sudo nano /etc/hostapd/hostapd.conf

and paste these lines to set the ssid and passphrase of the network and more:

interface=wlan0
driver=nl80211
ssid=< write SSID, name of the network, here >
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=< write password here>
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Replace "< write SSID, name of the network, here >" with the SSID you want to give to the network (no "<", ">" and spaces after "=")

Replace "< write password here>" with the password need to connect to the network (no "<", ">" and spaces after "=")

The passphrase MUST be between 8 and 64 characters long

Open the ‘/etc/default/hostapd’ file using the "nano" editor:

sudo nano /etc/default/hostapd

and set were you put the hostapd settings file ("/etc/hostapd/hostapd.conf") Modify the line that starts with “#DAEMON_CONF” with this:

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

Start Hostapd and set routing and masquerade


Open the ‘/etc/sysctl.conf’ file using the "nano" editor:

sudo nano /etc/sysctl.conf

Uncomment (remove "#") this line:

net.ipv4.ip_forward=1

Set a mask for the traffic that goes out of the eth0 interface

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Save the iptables (firewall rules that manage the incoming and outgoing traffic) to the ‘/etc/iptables.ipv4.nat’ file:

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

Open the "/etc/rc.local" file:

sudo nano /etc/rc.local

And paste this BEFORE the line "exit 0":

iptables-restore < /etc/iptables.ipv4.nat

The "/etc/rc.local" file contains commands that are executed on boot

Conclusion


Reboot for the last time:

sudo reboot

Now the network should be visible and accessible from all the devices that can connect to wireless networks, like smartphones and tablets

Attention


With this guide the raspberry doesn't have a DHCP server that can distribute dynamicly ip addresses. This means that to connect to the network you have to set under advanced settings of your device these settings:

IP: static
IP Address: 192.168.4.x
Gateway: 192.168.4.1
Subnet mask: 24
DNS 1: 8.8.8.8
DNS 2: 8.8.4.4

where "x" is a number between 2 and 254

It might be not necessary yo set all these parameters, like the DNS, while other might be automatically configured after setting up the IP address

Troubleshooting


If there is an error or the network doesn't show up, check all the instructions. If it still doesn't work check if the problem is listed here

Static IP is not set

If by entering this command:

ifconfig

you notice that the IP of the wlan0 interface is not set, try disabling and enabling again the interface:

sudo ifdown wlan0
sudo ifup wlan0

Try to reboot:

sudo reboot