Raspberry Pi: disable Wi-Fi powersave, and stay connected all the time

Posted by ads' corner on Monday, 2020-01-13
Posted in [Ansible][Linux][Raspberry-Pi][Software]

Recently I installed a Raspberry Pi with Raspbian, and attached a touchscreen to it. The device is supposed to work as a display for our openHAB home automation system. All nice and shiny, except the Pi occasionally disconnects from the Wi-Fi - apparently in order to safe power. Once the network device is needed, the Wi-Fi is reconnected. That is the default setting.

That is quite annoying, because the HABpanel will lose it’s connection to the openHAB server, and no longer show widget updates. Some widgets like “time” will just continue to work, but other widgets like status of the washing machine, or the current outside temperature just show the last state, and never update.

The power saving is a function of the WLAN device, therefore the wlan0 must be updated:

1
/sbin/iw dev wlan0 set power_save off

This works until the next reboot. And since I have all my devices setup using Ansible, I added two steps to the setup. First a small file:

1
2
3
4
5
#!/bin/sh

/sbin/iw dev wlan0 set power_save off

exit 0

Let’s say the name is wlan0-power. I place it in my Playbook directory, under files/, among with some other files for that Pi.

The next step is telling Ansible to deploy that file:

1
2
3
4
5
6
7
- name: Copy WLAN powersave script
  copy:
    src: '{{ playbook_dir }}/files/wlan0-power'
    dest: '/etc/network/if-up.d/wlan0-power'
    owner: root
    group: root
    mode: 0755

The if-up.d directory holds scripts which will be executed when a (any) device comes up. Could check for the coccrect device name first, but I don’t bother much. This script will only execute once the device comes up. If the device is already up, nothing happens. Either run the script manually once, or reboot the Pi. Since it’s part of my initial deployment, there will be a reboot later in that Playbook - so I don’t have to reboot just for this file.


Categories: [Ansible] [Linux] [Raspberry-Pi] [Software]