Wifi monitor on a Raspberry Pi

Wifi monitor on a Raspberry Pi

Aug 1, 2015
Categories: Hacks
Tags: raspi, linux

I had a Raspberry Pi and an old analog volt meter lying around. To finally do something “useful” with it, it was decided that our CoWorking Space needs a WiFi detector.

Functionality in short: It captures WiFi packets from the air, and counts them. This then gets mapped to a scale from 0 to 10, and displayed on a rainbow-colored LED chart.

Hardware #

This runs on a Raspberry Pi Model B, although all the other models should work too.

I am using a $8 WiFi dongle from Dealextreme. It just works™ after plugging into the RasPi. lsusb reports it as Ralink Technology, Corp. RT5370 Wireless Adapter with ID 148f:5370.

Be sure to have an ethernet connection to your Raspberry Pi! During setup we will change the WiFi device to monitor mode and thus lose the wifi connection. So you could lock out yourself if you SSH into it using WiFi (although a restart will fix it).

System setup #

sudo apt-get -y update
sudo apt-get -y install iw python-scapy tcpdumppython-dev
sudo pip install rpio

# add a monitor device

# test if monitoring works
sudo iw phy phy0 interface add mon0 type monitor
sudo iw dev wlan0 del
sudo ifconfig mon0 up
sudo iw dev mon0 set channel 6

# tcpdump should continuously list captured packages. Kill it with Ctrl-C.
sudo tcpdump -i mon0 -n

# teardown, back to normal wifi operation
sudo iw dev mon0 del
sudo iw phy phy0 interface add wlan0 type managed

Install our script #

scp -r raspi-mon pi@<ip_of_your_pi>

On your Pi, add the following line to /etc/rc.local (to enable autostart of the script):

sudo python /home/pi/raspi-mon/server.py &

LEDs #

Now, you have to wire up some LEDs from the Raspberry Pi GPIO pins. The RasPi has no problem outputting 10x 20mA, so you can connect them directly. Don’t forget to add a resistor to each LED although, except you want to destroy them in short time. I used ports 2, 3, 4, 17, 27, 22, 10, 9, 11, 7. If you use different ports, you should change this line .

I used 2 LEDs per color, red, orange, green, blue, white.

Wiring LEDs

Wiring and LEDs

Software explanation #

Used software:

  • Python
  • ScaPy for capturing packets
  • iw for managing the WiFi device
  • SQLite for data storage and simple analytics

I decided to use SQLite. It serves for multiple things simultaneously:

  • Thread-safe communication between LED and capturing thread
  • Easy data analytics (just SQL)
  • Cheap storage on disk (well… SD-card)

There are some things running in parallel.

  • One thread runs Scapy and sniffs. It logs all the beacon and data packets to the database.
  • Another thread (ChannelHopper) changes the wifi channel every 0.3 seconds. This is because we want to listen on all available channels, not just one..
  • The third and last thread (main thread) pulls the data from the SQLite database and maps the captured activity to the LEDs. It then sleeps for one second, and repeats.

Conclusion #

It works, although only on the 2.4GHZ band (the cheap adapter does not provide 5GHZ access). When I download something on my phone using a 2.4GHZ channel, I can see the LED bar going up! The whole thing is deployed in a cardboard box. It just runs after powering on (plugging in) the RasPi.

It works!

It works!

Code on Github

← Telegram CLI on Raspberry PI Javascript 3D waterfall plot →