Wednesday, December 26, 2018

3D Xmas Tree WiFi Activity Monitor

This simple script will use the LED's of the 3d-xmas-tree-for-raspberry-pi to indicate WiFi activity on the Raspberry Pi.

I have installed a default Raspbian with python.

Create the following script

from gpiozero import LEDBoard
from gpiozero.tools import random_values
from signal import pause
import time
import psutil
from decimal import *

tree = LEDBoard(*range(4,28),pwm=True)
star = LEDBoard(2,pwm=True)
netstats=psutil.net_io_counters(pernic=True)
totb = netstats['wlan0'].bytes_sent+netstats['wlan0'].bytes_recv
maxb = 0

def treelight(power):
  for led in tree:
    led.value=power
  time.sleep(0.02)
  for led in tree:
    led.off()

def calc_power(delb):
  getcontext().rounding = ROUND_CEILING
  global maxb
  p=float()
  if delb > maxb:
    maxb = delb
  p = float(delb) / float(maxb)
  if p > 0 and p < 0.1:
    p = 0.09
  return round(Decimal(p),1)


while True:
  netstats=psutil.net_io_counters(pernic=True)
  newb=netstats['wlan0'].bytes_sent+netstats['wlan0'].bytes_recv
  delb=newb-totb
  #print newb,totb,delb
  if newb > totb:
    treelight(calc_power(delb))
  totb=newb



and save it as a Python script (/home/pi/wlan_monitor.py)

run it by executing:

python /home/pi/wlan_monitor.py

You will not see any output but the lights of the tree will blink when there is WiFi activity.

The intensity of the lights will depend on the amount of bytes transferred.