Merge pull request #15 from hjelev/wifi

add wifi signal dBm
This commit is contained in:
Masoko
2022-11-19 21:23:58 +02:00
committed by GitHub
4 changed files with 55 additions and 12 deletions

View File

@@ -23,7 +23,7 @@ masoko/rpi4
The csv message looks like this: The csv message looks like this:
```csv ```csv
9.0, 43.0, 25, 25, 0.85, 1500, False, False, False 9.0, 43.0, 25, 25, 0.85, 1500, False, False, 73, -60
``` ```
Disabled sensors are represented with False in the message. Disabled sensors are represented with False in the message.
@@ -74,6 +74,7 @@ swap = False
memory = False memory = False
uptime = True uptime = True
wifi_signal = True wifi_signal = True
wifi_signal_dbm = False
``` ```
If the ```discovery_messages``` is set to true, the script will send MQTT Discovery config messages which allows Home Assistant to automatically add the sensors without having to define them in configuration. Note, this setting is only available when ```group_messages``` is set to False. If the ```discovery_messages``` is set to true, the script will send MQTT Discovery config messages which allows Home Assistant to automatically add the sensors without having to define them in configuration. Note, this setting is only available when ```group_messages``` is set to False.
@@ -81,7 +82,7 @@ If the ```discovery_messages``` is set to true, the script will send MQTT Discov
If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format. If the ```group_messages``` is set to true the script will send just one message containing all values in CSV format.
The group message looks like this: The group message looks like this:
``` ```
1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12, 50.0 1.3, 47.1, 12, 1.2, 600, nan, 14.1, 12, 50, -60
``` ```
Test the script. Test the script.
@@ -158,6 +159,11 @@ This is the sensors configuration if ```group_messages = True``` assuming your s
name: rpi4 wifi signal name: rpi4 wifi signal
unit_of_measurement: "%" unit_of_measurement: "%"
- platform: mqtt
state_topic: 'masoko/rpi4'
value_template: '{{ value.split(",")[9] }}'
name: rpi4 wifi signal
unit_of_measurement: "dBm"
``` ```
This is the sensors configuration if ```group_messages = False``` assuming your sensors are separated in ```sensors.yaml``` file. This is the sensors configuration if ```group_messages = False``` assuming your sensors are separated in ```sensors.yaml``` file.
@@ -207,6 +213,11 @@ This is the sensors configuration if ```group_messages = False``` assuming your
name: rpi4 wifi signal name: rpi4 wifi signal
unit_of_measurement: "%" unit_of_measurement: "%"
- platform: mqtt
state_topic: "masoko/rpi4/wifi_signal_dbm"
name: rpi4 wifi signal
unit_of_measurement: "dBm"
``` ```
Add this to your ```customize.yaml``` file to change the icons of the sensors. Add this to your ```customize.yaml``` file to change the icons of the sensors.
@@ -247,6 +258,10 @@ entities:
- entity: sensor.rpi4_memory - entity: sensor.rpi4_memory
- entity: sensor.rpi4_uptime - entity: sensor.rpi4_uptime
- entity: sensor.rpi4_wifi_signal - entity: sensor.rpi4_wifi_signal
- entity: sensor.rpi4_wifi_signal_dbm
``` ```
# To Do # To Do
- maybe add network traffic monitoring via some third party software (for now I can't find a way to do it without additional software) - maybe add network traffic monitoring via some third party software (for now I can't find a way to do it without additional software)
# Feature request:
If you want to suggest a new feature or improvement don't hesitate to open an issue or pull request.

View File

@@ -13,9 +13,11 @@ find_python(){
if [[ $(python --version) ]]; then if [[ $(python --version) ]]; then
python=$(which python) python=$(which python)
pip="python-pip" pip="python-pip"
pip_run='pip'
else else
python=$(which python3) python=$(which python3)
pip="python3-pip" pip="python3-pip"
pip_run='pip3'
fi fi
if [[ "$python" == *"python"* ]]; then if [[ "$python" == *"python"* ]]; then
@@ -59,8 +61,7 @@ check_and_install_pip(){
install_requirements(){ install_requirements(){
printm "Installing requirements" printm "Installing requirements"
pip install -r requirements.txt $pip_run install -r requirements.txt
sudo pip3 install -r requirements.txt
} }
update_config(){ update_config(){

View File

@@ -31,4 +31,5 @@ sys_clock_speed = True
swap = True swap = True
memory = True memory = True
uptime = True uptime = True
wifi_signal = True wifi_signal = True
wifi_signal_dbm = True

View File

@@ -16,9 +16,10 @@ import os
# get device host name - used in mqtt topic # get device host name - used in mqtt topic
hostname = socket.gethostname() hostname = socket.gethostname()
def check_wifi_signal(): def check_wifi_signal():
try: try:
full_cmd = "iwconfig wlan0 | grep -i --color quality" full_cmd = "/sbin/iwconfig wlan0 | grep -i quality"
wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0] wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0] wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[1].split('=')[1].split('/')[0]
wifi_signal_calc = round((int(wifi_signal) / 70)* 100) wifi_signal_calc = round((int(wifi_signal) / 70)* 100)
@@ -27,6 +28,16 @@ def check_wifi_signal():
return wifi_signal_calc return wifi_signal_calc
def check_wifi_signal_dbm():
try:
full_cmd = "/sbin/iwconfig wlan0 | grep -i quality"
wifi_signal = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
wifi_signal = wifi_signal.decode("utf-8").strip().split(' ')[4].split('=')[1]
except Exception:
wifi_signal = 'NA'
return wifi_signal
def check_used_space(path): def check_used_space(path):
st = os.statvfs(path) st = os.statvfs(path)
free_space = st.f_bavail * st.f_frsize free_space = st.f_bavail * st.f_frsize
@@ -147,6 +158,10 @@ def config_json(what_config):
data["icon"] = "mdi:wifi" data["icon"] = "mdi:wifi"
data["name"] = hostname + " Wifi Signal" data["name"] = hostname + " Wifi Signal"
data["unit_of_measurement"] = "%" data["unit_of_measurement"] = "%"
elif what_config == "wifi_signal_dbm":
data["icon"] = "mdi:wifi"
data["name"] = hostname + " Wifi Signal"
data["unit_of_measurement"] = "dBm"
else: else:
return "" return ""
# Return our built discovery config # Return our built discovery config
@@ -154,7 +169,7 @@ def config_json(what_config):
def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0, def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
uptime_days=0, wifi_signal=0): uptime_days=0, wifi_signal=0, wifi_signal_dbm=0):
# connect to mqtt server # connect to mqtt server
client = paho.Client() client = paho.Client()
client.username_pw_set(config.mqtt_user, config.mqtt_password) client.username_pw_set(config.mqtt_user, config.mqtt_password)
@@ -225,15 +240,24 @@ def publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_s
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, qos=1) client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal", wifi_signal, qos=1)
time.sleep(config.sleep_time) time.sleep(config.sleep_time)
if config.wifi_signal_dbm:
if config.discovery_messages:
client.publish("homeassistant/sensor/" + config.mqtt_topic_prefix + "/" + hostname + "_wifi_signal_dbm/config",
config_json('wifi_signal_dbm'), qos=0)
time.sleep(config.sleep_time)
client.publish(config.mqtt_topic_prefix + "/" + hostname + "/wifi_signal_dbm", wifi_signal_dbm, qos=1)
time.sleep(config.sleep_time)
# disconnect from mqtt server # disconnect from mqtt server
client.disconnect() client.disconnect()
def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0, def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_clock_speed=0, swap=0, memory=0,
uptime_days=0, wifi_signal=0): uptime_days=0, wifi_signal=0, wifi_signal_dbm=0):
# compose the CSV message containing the measured values # compose the CSV message containing the measured values
values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal values = cpu_load, float(cpu_temp), used_space, float(voltage), int(sys_clock_speed), swap, memory, uptime_days, wifi_signal, wifi_signal_dbm
values = str(values)[1:-1] values = str(values)[1:-1]
# connect to mqtt server # connect to mqtt server
@@ -250,7 +274,7 @@ def bulk_publish_to_mqtt(cpu_load=0, cpu_temp=0, used_space=0, voltage=0, sys_cl
if __name__ == '__main__': if __name__ == '__main__':
# set all monitored values to False in case they are turned off in the config # set all monitored values to False in case they are turned off in the config
cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = False cpu_load = cpu_temp = used_space = voltage = sys_clock_speed = swap = memory = uptime_days = wifi_signal = wifi_signal_dbm = False
# delay the execution of the script # delay the execution of the script
time.sleep(config.random_delay) time.sleep(config.random_delay)
@@ -274,8 +298,10 @@ if __name__ == '__main__':
uptime_days = check_uptime() uptime_days = check_uptime()
if config.wifi_signal: if config.wifi_signal:
wifi_signal = check_wifi_signal() wifi_signal = check_wifi_signal()
if config.wifi_signal_dbm:
wifi_signal_dbm = check_wifi_signal_dbm()
# Publish messages to MQTT # Publish messages to MQTT
if config.group_messages: if config.group_messages:
bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal) bulk_publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm)
else: else:
publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal) publish_to_mqtt(cpu_load, cpu_temp, used_space, voltage, sys_clock_speed, swap, memory, uptime_days, wifi_signal, wifi_signal_dbm)