Keep track of your phone, tablet, laptop, and other wireless devices using a Raspberry Pi! The more Raspberry Pi’s you have, the better you can monitor your home for the movement of your devices. Once you have all the Pi’s set up, send that data to an IoT platform to record and visualize your house and the locations of all your wireless devices!
Setting up the PiMy Raspberry Pi’s used Raspbian burned onto an 8 GB micro SD card. Once Raspbian is downloaded, just follow these steps:
Step 1: Enable your Pi to support Monitor Mode:
- Make sure you’re in root
$ sudo su
- Change your directory
$ cd /usr/local/src
- Download the current version of Re4son kernel
$ wget -O re4son-kernel_current.tar.xz https://re4son-kernel.com/download/re4son-kernel-current/
- Use the tar command to extract the file
$ tar -xJf re4son-kernel_current.tar.xz
- Change directories
$ cd re4son-kernel_4*
- Install the firmware.
$ ./install.sh
Step 2: Test for Monitor Mode:
- Once again make sure you’re in root
$ sudo iw phy phy0 interface add mon0 type monitor
- Create a monitor mode interface
$ iw dev
- Check that the interface exists
$ ifconfig mon0 up
Step 3: Install the most recent version of scapy:
- In a separate folder clone the scapy repository
$ git clone https://github.com/secdev/scapy.git
- Change directory
$ cd scapy
- Install the setup.py file
$ sudo python3 setup.py install
Once these three steps are completed, you should be able to run the code with no problems!
Most of this setup of getting monitor mode to work on my RPi is thanks to these two websites:
- https://null-byte.wonderhowto.com/how-to/enable-monitor-mode-packet-injection-raspberry-pi-0189378/
- https://behindthesciences.com/cyber-security-hacks/how-to-enable-monitor-mode-in-raspberry-pi-3
First, we need to import all the necessary libraries.
from scapy.all import *
import json
import threading
import http.client
Next, you will need to insert your uBeac Gateway URL, which will be discussed later. Give your device a friendly name, and set the interval for how often you want your device to send data.
UBEAC_URL = 'hub.ubeac.io'
GATEWAY_URL = 'http://INSERT GATEWAY URL HERE'
DEVICE_FRIENDLY_NAME = 'RPi detector 4'
SENT_INTERVAL = 10 # Sent data interval in second
For tracking your own devices, you need to know their MAC address. Type them into the devices dictionary.
devices = {"00:00:00:00:00:00" : "DEVICE 1",
"00:00:00:00:00:00" : "DEVICE 2",
"00:00:00:00:00:00" : "DEVICE 3"}
The get_sensor function puts data into the uBeac sensor format.
def get_sensor(id, value, type=None, unit=None, prefix=None, dt=None):
sensor = {
'id': id,
'data': value
}
return sensor
The PacketHandler function is what tracks the Wi-Fi packets emitted from your wireless devices.
def PacketHandler(pkt):
if pkt.haslayer(Dot11):
dot11_layer = pkt.getlayer(Dot11)
if dot11_layer.addr2 and (dot11_layer.addr2 in devices) and (dot11_layer.addr2 not in check_devices):
check_devices.add(dot11_layer.addr2)
sensors_dbm.append(get_sensor(devices[dot11_layer.addr2], {"dBm Signal" : pkt[RadioTap].dBm_AntSignal}))
The main function takes the PacketHandler data and sends it to uBeac’s IoT service. It will reset every 10 seconds, but this can be changed
def main():
threading.Timer(SENT_INTERVAL, main).start()
sniff(iface = "mon0", prn = PacketHandler, timeout = SENT_INTERVAL)
device = [{
'id': DEVICE_FRIENDLY_NAME,
'sensors': sensors_dbm
}]
connection = http.client.HTTPSConnection(UBEAC_URL)
connection.request('POST', GATEWAY_URL, json.dumps(device))
response = connection.getresponse()
print(response.read().decode())
sensors_dbm.clear()
check_devices.clear()
Setup uBeacFor a intro to uBeac, just follow this tutorial on OS monitoring. In this tutorial, you will learn how to get your uBeac Gateway URL, ensure your devices are sending data, and how to make a dashboard!
To get the floor plan with the devices laid out on it, you will need to go to the Buildings module and create a Building by inputting an address, selecting it on a map, and providing your location a name. After that, you need to make a floor for your building. Select the Floor tab to get to the Floor modules and create a new floor. Find a floor plan, whether it be your own or one from the internet and insert it.
To add your devices to the floor plan, go to the Devices module, select on your specific device, and select settings. Find the buildings tab and select your building and floor. Drag your device to where it needs to be on the floor plan. Click submit once you’ve edited the icon image, size, and colour of your device.
If you go to the Dashboard module, make a dashboard, and insert the floor widget, you should see all of your added devices and their data! Below is an example of such a dashboard.
Note: the distance from the RaspberryPi and the wireless device is based on signal strength, which is a negative number. so the smaller the bar in the bar graph, the closer that object.
Get started with a free uBeac account here, check out our other projects and feel free to contact me if you have any questions. Happy Connecting!
Comments