Find Out Who's at Home or in the Office With the Raspberry Pi Who's Home Server

by AndrewB581 in Circuits > Raspberry Pi

880 Views, 3 Favorites, 0 Comments

Find Out Who's at Home or in the Office With the Raspberry Pi Who's Home Server

DALL·E 2024-02-01 12.07.33 - A Raspberry Pi computer setup with a Kali Linux theme. The scene includes a Raspberry Pi model with various cables connected to it, a small screen dis.png

In this project I will show you how to make a 'who's home server' out of a Raspberry Pi

You can use this at home or in the office to see who is in, as long as their device or phone is connected to the network. (Please ensure you have permission from the people whose devices you are tracking before continuing)

I will also show you how you can access the server over the internet from outside your home network so you can see who is in from outside your home.

Supplies

vishnu-mohanan-rZKdS0wI8Ks-unsplash.jpg

For this project we will only need a Raspberry Pi and SD card for the OS

Install Raspberry Pi OS on Your SD Card

RPI_intro-e1583228263677.png

We will use Raspberry Pi OS for this project. There are lots of tutorials out there on how To prepare the SD card with the OS. using the Raspberry pi imager is the easiest way https://www.raspberrypi.com/software/

Once you have booted up your raspberry pi you need to connect to whichever wifi network you wish to monitor.

Install Arp-scan

Once you have got your raspberry pi up and running, open up a terminal window.

We need to install a tool that will scan the network for devices connected to it. We will use arp-scan.

this should come preinstalled on most versions of raspberry pi os.

To install arp-scan run the following command

sudo apt-get install arp-scan

Install Nginx Web Server

01-Nginx-default-screen.png

Once we have scanned our network and determined who is home, we want to output this information to a webpage which will be hosted on a webserver on the raspberry pi. We will install the nginx web server for this.

please enter the following command to install

sudo apt-get install nginx

After install we need to run the following command to start the nginx service

sudo systemctl start nginx

Now we can browse to our pi's ip address to check if the web server is running

to find your pi's ip address type:

hostname -I

now type that ip address into the address bar of your web browser and you should get the default nginx page to show that everything is working ok

Get the MAC Addresses

Screenshot 2021-11-06 112705.png

In order to check who is in we will need to know which MAC address belongs to which person.

You can find MAC addresses for devices like phones in the network settings and all network devices will have their MAC addresses printed on them somewhere.

You can also log into your home router and see all the clients connected to your network this sometimes shows the manufacturer beside them which can make it easier to figure out.

Make a list of the mac addresses and the person they are associated with

Let's Write Some Code

Now we will write a script which will do the following:

  • Execute an arp scan and output it to a text file
  • Use the grep command to check the file for the specified MAC addresses
  • Assign the name of whoever's MAC address is present to the occupant variable
  • Write some HTML code including the correct occupants names to a file which will then be served in the pi's webserver

copy the code below into a text file on your raspberry pi and name it whosHome.sh

#!/bin/bash
#We will enclose our code in an infinite loop so that the scan can repeat periodically
while true
do
#This runs an arp scan and lists all of the mac addresses on my network
sudo arp-scan -l > arpScan.txt

#We then have a series of if statements to check for my housmates Mac addresses
if grep -q 82:af:0b:d7:c9:65 arpScan.txt
then

occupant1="Paul"
else
#If the MAC address is not found then we will write and empty string to the variable
occupant1=""
fi
if grep -q b2:60:77:57:c7:3e arpScan.txt
then
occupant2="James"
else
occupant2=""
fi
if grep -q e4:a7:c5:e4:85:ca arpScan.txt
then


occupant3="Tim"
else
occupant3=""
fi
#you can edit the code to add as many if else statements as you need to check for every mac address you have
#I have added some extra lines below that you can add more addresses with. Just uncomment them if you are using the

#if grep -q <xx:xx:xx:xx:xx:xx> arpScan.txt
#then


# occupant4="<occupant name>"
#else
# occupant4=""
#fi
#if grep -q <xx:xx:xx:xx:xx:xx> arpScan.txt
#then


# occupant5="<occupant name>"
#else
# occupant5=""
#fi
#if grep -q <xx:xx:xx:xx:xx:xx> arpScan.txt
#then


# occupant6="<occupant name>"
#else
# occupant6=""
#fi



echo "Occupant 1 ${occupant1}"
echo "Occupant 2 ${occupant2}"
echo "Occupant 3 ${occupant3}"

#echo "Occupant 4 ${occupant4}"
#echo "Occupant 5 ${occupant5}"
#echo "Occupant 6 ${occupant6}"

#We then output some html code to a file to be served on the webserver on the pi
echo "<link rel="stylesheet" href=style.css>
<h1 class="title">Who's Home?<img src="https://img.icons8.com/fluency/48/000000/home.png"/></h1><h1>$occupant1 <br>$occupant2<br> $occupant3</h1>" > /var/www/html/index.nginx-debian.html

#We then want the program to sleep for 5 minutes before scanning the network again.
#You can adjust this time but beware of flooding the network with traffic if repeated too frequently
sleep 5m
done

Once you have saved the file we need to make it executable. using the terminal type the following code:

nb* you will need to be in the same directory as the file to execute this command

sudo chmod +x whosHome.sh

Now we need to change the permissions on the html file that the web server is showing so that we can write new code to it.

navigate to /var/www/html and type the following command:

sudo chown pi index.nginx-debian.html

We can now run the whosHome script

Navigate back to the directory containing the script and execute the following command

./whosHome.sh

Once the script has executed you should be able to browse to your pi's ip address and view the who's home webpage!

You should now be able to View the webpage and see the names of the occupants that are home just like the pic above.

Great work!

We have now created an application that scans the local network and and checks for certain credentials and then outputs the results to a webpage. And all done on the Raspberry Pi!

So now we can log into that webpage and see who is at home whenever we want. Awesome!

But as it stands, we can only view the webpage when we are on our home network. What if we wanted to access the who's home server from outside of our home over the internet so that we could see who is home when we are not there.

In order to do this we will need to find out our public ip address and enable port forwarding on our home router so that we can access the Pi's webserver.

Head to the next step and I'll show you how!

Access Your Who's Home Server Over the Internet (remotely)

dynv6.png
dynv6CreateZone.png

In order to be be able to access your Pi via the internet you will need to know your public ip address.

head over to Google and type "whats my ip"

With most home ip addresses they will be dynamic which means they could change over time. In order to access the pi all the time even if the ip address changes, we will use a dynamic DNS service.

Head to https://dynv6.com and sign up for an account.

once you have signed up, you need to create your zone:

enter the name that you wish to have as the web address for accessing the server. Then enter your public ip address.

Once that has saved, you will be able to access your raspberry pi web server at that address, over the internet.

Please note that you may need to set up port forwarding on your router to be able to allow traffic from the internet through to your raspberry pi. Instructions for this will vary depending on the make and model of your router. search for documentation on your router to help with this.

You will need to forward traffic on port 80(http) to the raspberry pi's ip address, nb this is the private ip of the raspberry pi and not your public ip address.

Once you have enabled port forwarding you can check if its working by browsing to the dynamic host name you created.

Webpage Output

wh-v2.PNG

When you browse to the server you should see a webpage like the one pictured above.

The script is set to run every 5 minutes so when it runs it may change the output to the webpage and you may need to refresh the page to view the most up to date info.

And that's it! We have created an application using front and back end development and also some networking skills too!

This application can be used in a number of different ways as it is very useful to be able to scan a network and output results to an easily accessible webserver and we've done it all on one dedicated computer with no charges for hosting.