Prompted by a desire to improve Wifi coverage & performance within our house, I set about producing a Wifi application that would graph the key parameters; Bit Rate, Link Quality & Signal Level.
Researching the murky world of wifi drivers revealed that they do not all display the same data in the same format. For example the better devices have drivers that provide noise level in dBm, while others show this as a fraction, but some (like mine) do not provide a noise figure at all.
This simple wifi class uses "iwconfig" to harvest the data, making it available as object properties. I thought it might be of interest or as a building block for others.
To play with this class, just start a new Gambas project, add the clsWifi file to the project, then add a timer and a hand-full of labels.
Basic code for main form:-
'Declare a new instance of the wifi object
PUBLIC myWifi AS NEW clsWifi
'Enable the timer and set a sensible interval (say 1000ms)
PUBLIC SUB tmrMain_Timer()
'Read wifi data
IF myWiFi.Read() THEN 'Success!
'Display data
Me.Text = "ESSID: " & myWifi.strESSID
lblRate.Text = myWiFi.sngBitRate & "Mb/s"
'Because of wifi driver differences, you may have to test the Signal Level data...
IF myWiFi.lngSignalScale > 0 THEN 'level is probably a fraction
lblLevel.Text = CStr(100 * (myWiFi.lngSignalLevel / myWiFi.lngSignalScale)) & "%"
ELSE 'level is probably in dBm
lblLevel.Text = CStr(myWiFi.lngSignalLevel) & "dBm"
ENDIF
ELSE
Me.Text="Uh, Oh!"
ENDIF
END
...and here is the WiFi class:-
' Gambas class file
' clsWiFi
'********************************************************************************
'This simple class reads data collected by iwconfig
'
'Use the method "Read" to read WiFi settings into a temp file, and
' then use individual properties; strBitRate, strLinkQuality & so on
'
'
'Steve Davis
'October 2009
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PROPERTY READ strInterface AS String
PRIVATE strEth AS String
PROPERTY READ strESSID AS String
PRIVATE strID AS String
PROPERTY READ strNickName AS String
PRIVATE strNick AS String
PROPERTY READ sngBitRate AS Single
PRIVATE strRate AS String
PROPERTY READ lngLinkQuality AS Long
PRIVATE strQuality AS String
PROPERTY READ lngLinkQualityScale AS Long
PRIVATE strLQScale AS String
PROPERTY READ lngSignalLevel AS Long
PRIVATE strLevel AS String
PROPERTY READ lngSignalScale AS Long
PRIVATE strSignalScale AS String
PROPERTY READ lngNoiseLevel AS Long
PRIVATE strNoise AS String
'=====================================================================
PUBLIC FUNCTION Read() AS Boolean
'This method copies the Wifi settings into a hidden file in the
'users Home directory, then writes the settings to class properties.
'---------------------------------------------------------------------
DIM strFilePath AS String 'where to save file
DIM strFile AS String 'contents of file
DIM strCommand AS String 'Linux command string
DIM hFile AS File
DIM strLinkQ AS String 'temp Link Quality string
strFilePath = User.Home & "/.wifi.tmp"
IF Exist(strFilePath) THEN
KILL strFilePath
ENDIF
strCommand = "iwconfig > " & strFilePath
SHELL strCommand WAIT
IF Exist(strFilePath) THEN
hFile = OPEN strFilePath FOR READ
READ #hFile, strFile, -512
CLOSE #hFile
strEth = Mid(strfile, 1, InStr(strfile, (" "))) 'isplate the interface (i.e. ethx, wlanx)
strID = ReadValue(strfile, "ESSID")
strNick = ReadValue(strfile, "Nickname")
strRate = ReadValue(strFile, "Bit Rate")
strLinkQ = ReadValue(strFile, "Link Quality")
strQuality = Mid(strLinkQ, 1, InStr(strLinkQ, "/") - 1)
strLQScale = Mid(strLinkQ, InStr(strLinkQ, "/") + 1)
strLevel = ReadValue(strFile, "Signal level")
IF InStr(strLevel, "/") > 0 THEN
strSignalScale = Mid(strLevel, InStr(strLevel, "/") + 1)
strLevel = Mid(strLevel, 1, InStr(strLevel, "/") - 1)
ENDIF
strNoise = ReadValue(strfile, "Noise level")
RETURN TRUE
ENDIF
CATCH
RETURN FALSE
END
'=====================================================================
PRIVATE FUNCTION ReadValue(strFile AS String, strParam AS String) AS String
'Find a specified parameter setting from the specified file.
'---------------------------------------------------------------------
DIM strText AS String
IF InStr(strfile, strParam) > 0 THEN
strText = Mid(strfile, InStr(strfile, strParam))
strText = Replace(strText, ":", "=")
strText = Mid(strText, InStr(strText, "=") + 1)
IF Left(strText) = "\"" THEN
strText = Mid(strText, 2, InStr(strText, "\"", 2) - 2)
ELSE
strText = Mid(strText, 1, InStr(strText, " ") - 1)
ENDIF
ENDIF
RETURN Trim(strText)
CATCH
RETURN "0"
END
'CLASS PROPERTIES---------------------------------------------------------------
PRIVATE FUNCTION sngBitRate_Read() AS Single
TRY CSng(strRate)
IF ERROR THEN strRate = "0"
RETURN CSng(strRate)
END
PRIVATE FUNCTION lngLinkQuality_Read() AS Long
TRY CLng(strQuality)
IF ERROR THEN strQuality = "0"
RETURN CLng(strQuality)
END
PRIVATE FUNCTION lngLinkQualityScale_Read() AS Long
TRY CLng(strLQScale)
IF ERROR THEN strLQScale = "0"
RETURN CLng(strLQScale)
END
PRIVATE FUNCTION lngSignalLevel_Read() AS Long
TRY CLng(strLevel)
IF ERROR THEN strLevel = "0"
RETURN CLng(strLevel)
END
PRIVATE FUNCTION strESSID_Read() AS String
RETURN strID
END
PRIVATE FUNCTION strNickName_Read() AS String
RETURN strNick
END
PRIVATE FUNCTION strInterface_Read() AS String
RETURN strEth
END
PRIVATE FUNCTION lngSignalScale_Read() AS Long
TRY CLng(strSignalScale)
IF ERROR THEN strSignalScale = "0"
RETURN CLng(strSignalScale)
END
PRIVATE FUNCTION lngNoiseLevel_Read() AS Long
TRY CLng(strNoise)
IF ERROR THEN strNoise = "0"
RETURN CLng(strNoise)
END