Tuesday, March 24, 2015

Plotting WiFi signal strength variation

Strength of WiFi signals from an access point varies with time due to different reasons. Today I used a simple python script to plot WiFi signal strength variation over time and it showed me how quickly signal strength changes. My script basically uses the iwconfig command to get the details of the currently connected access point and then I grep it to filer the signal level which is indicated in dBm (decibel miliwatts). These values are continuously used to plot in real time using matplotlib.

WiFi signal strength variation in dBm
Source code of the python script is given below. To run this code, we should have  installed matplotlib for python. That can be installed by issuing the following command in terminal.

sudo apt-get install python-matplotlib


import matplotlib.pyplot as plt
import time
import random
from collections import deque
import numpy as np
import os

def read_wifi():
    while True:
 f=os.popen('sudo iwconfig eth1 | grep -e "Signal level"')
 line = f.read()
 splitted_line = line.split()
 level = splitted_line[3].split('=')
 print level[1]
 val = level[1]
        yield val
        time.sleep(0.1)


a1 = deque([0]*100)
ax = plt.axes(xlim=(0, 100), ylim=(0, 10))
d = read_wifi()

line, = plt.plot(a1)
plt.ion()
plt.ylim([-90,-60])
plt.show()

for i in range(0,10000):
    a1.appendleft(next(d))
    datatoplot = a1.pop()
    line.set_ydata(a1)
    plt.draw()
    print a1[0]
    i += 1
    time.sleep(0.1)
    plt.pause(0.0001)

No comments:

Post a Comment