Before I start to write about this project let me ask some questions that you might have or might be facing as a maker, engineer or hobbiest.
Do you have broadband internet connection for your home or office and want to extend it's wifi range?
Have ever wanted to create an open source router using Raspberry Pi?
Do you wonder how to use official Raspberry Pi PoE(Power over ethernet) Hat?
Do you want a camera option with your router?
If you have any of these questions and wonder how then they are going to be answered in this project. I am going to use Raspberry Pi 4 for this project as an example but one can use any similar single board computer available in the market.
There are many advantages of using Raspberry Pi 4 for this project as it comes with many add on boards and official HaT such as PoE(Power over Ethernet) and official camera module.
Raspberry Pi PoE(Power over Ethernet) HaT is a official HaT from Raspberry Pi that provides power plus data using single Ethernet cable. This will reduce the need for separate power supply for the device and it also provides high speed wired internet connection from the router cable as well.
For creating a Router this will be a perfect choice of hardware as it can extend the connectivity to around 10 to 20 meters away from the existing router. It also can extend the WiFi range inside a building flore where there is no internet.
The official Raspberry Pi camera module is optional for this project but it will increase the scope of the device if camera can be added for taking images or having a video stream. As you see above the PoE hat has dedicated a slot to inset the DIP camera cable. Also the are many open source software options such as motion
to create a working video monitoring/streaming application.
As you know, Raspberry Pi boots from SD card. Hence, we need to flash right operating system to SD card.
There are many possible operating systems options that can run on Raspberry Pi. The Raspberry Pi has ARM 64 bit processor and official Raspberry Pi OS is one of the best and easiest option. While for this project I am going to flash Ubuntu Server 64 bit operating system on the SD card. Ubuntu has official support for Raspberry Pi and we can boot 64-bit version of it on Raspberry Pi.
I am using the official Raspberry Pi imager tool to flash the operating system. It makes easier for user to pre-configure the ssh connection, users and WiFi connection as it has option to flash these settings in the tool itself.
Also, I choose server image of Ubuntu as it comes already with openssh installed and there is no requirement to have desktop, keyboard and mouse to make initial setup. It is light wait compared to desktop version and has many required packages already installed.
Once SD card is flashed we need to put everything together. First, fix the PoE hat on 40-pin GPIO and insert the SD card into the slot. Then I have used Ethernet cable to connect the home router to the Raspberry Pi Ethernet connector. As soon as you plug the cable, Raspberry Pi will start booting. It will take some time to on first boot to setup everything.
If your router do not support PoE function you can use PoE adaptor to create PoE connection. The underlying standard for PoE is 802.11af.
The next task is to ssh into the Raspberry Pi. You can use the following commands to do that. (For the first time booting you might need to create public/private keys and share these keys between your devices). SSH documentation explains everything about how to do this. Also you can just use password authentication only to connect to your Pi. In that case, no need to exchange keys.
If there is success in connection you can follow along.
$ ssh user@ipaddressofraspberrypi
I am going to use netplan.io for configuring the network. Netplan is a utility for network configuration. It accepts.YAML format files along with commands which can configure the network and various interfaces.
My idea was to use these networking files to setup and share the internet connection that the Raspberry Pi has on Ethernet interface(iface eth0) with WiFi interface(iface wlan0). For that a bridge needs to be created between these two interfaces either manually or automatically. The last thing is configuring the wlan0 interface as AP(Access point) which allow to share it's internet with other devices.
Change the existing Netplan files
It is time to update the Netplan files for creating a proper router setup. Netplan utility is preinstalled on Ubuntu image and the file is
/etc/netplan/50-cloud-init.yaml
We now need to edit this file using text editor.
$ sudo nano /etc/netplan/50-cloud-init.yaml
Copy and paste the following contents in above file. Remember to change the password and set the SSID of the AP(Access point) that you want your Raspberry Pi to create for you to connect. Also note the indentations in the file as they may through error if not correctly done.
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlan0:
dhcp4: yes
optional: true
access-points:
"Ground_flore":
auth:
key-management: psk
password: "password"
mode: ap
Now, hit the command to apply the changes. Be careful to apply these changes as errors or wrong settings might make your Raspberry Pi not accessible also on ssh connection.
$ sudo netplan apply
Note:
you can also use the above Netplan to configure static IP for your Pi to have same IP every time you boot. This will make task easier to debug and find issues. Also, DHCP router reserves that IP for your Pi for some time. This will make tasks easier for things such as running bash scripts when the Raspberry Pi boots and sharing files between your raspberry pi and Linux or Windows PC automatically.
(optional) Camera interface
The camera is optional for this project. One can skip it but it can add more interesting features to the project. Hence, I will explain how to interface camera and use basic functions for taking pictures or having a video feed.
Ubuntu server 64-bit os can run the official raspberry Pi camera module or a USB camera. But we need to change settings in some files to make it work. Open the /config.txt file and put start_x=1
and gpu_mem=128
at the last line. This will enable camera with required settings
$ sudo nano /boot/firmware/config.txt
Make sure that you have installed raspi-config
package and have enabled camera from the interfacing options-> camera.
$ sudo apt install raspi-config
For taking pictures using camera raspistill
is used on official Raspberry Pi OS. But for Ubuntu we need to use other software that can access the /dev/videox
device. I am using opencv Python package to access the camera module. If python3 and pip is installed the following commands will install cv2 python package.
$ sudo apt update
$ sudo apt upgrade
$ pip install opencv-python
Once the packages are installed and files are setup we can write a simple python code to test if the camera is working properly. I have also attached a USB camera module, that I can use to capture images.
$ sudo nano capture.py
import cv2
camera0 = cv2.VideoCapture('/dev/video0', cv2.CAP_V4L2)
camera1 = cv2.VideoCapture('/dev/video2')
returnval0, image0 = camera0.read()
returnval1, image1 = camera1.read()
cv2.imwrite("image0.jpg", image0)
cv2.imwrite("image1.jpg", image1)
camera0.release()
camera1.release()
finally, when I will run the above code, using python3 it will capture and save two images for me inside a working directory. I can then use scp
command to get both the images to my PC.
$ python3 capture.py
In this project I have descried how I created a Wireless router for my home with optional camera features. The router is working as expected and I can stream high quality video online without any issues. This will be enough for basic internet usages. Of course it may not match the performance provided my commercial products but it will be quite helpful to someone for home use. The cameras are working and we can get images and videos on local network easily. The more work needs to be done if I want to access my home camera from remote location. For that I am still working on VPN connection and client server ports, request handling tasks. Once I am done, I will edit this project.
Comments