Raspberrypi 3B Wifi Power Management Failures

The latest version of Raspbiean lite comes with wifi power management enabled and this causes disconnections when not used a client device. You need to run the command iwconfig wlan0 power off to fix the problem and you can so with systemd to work around the well known issue.

I was trying to get some SGP40 Volatile organic compound sensors setup on a few raspberry pi 3Bs for monitoring 3D printing air quality. PLA+ has been a constant source of throut irritation and its been a challenge capturing all of the offgas and getting it filtered. I had some raspberry pi’s 3B’s so a couple of SGP40 sensors bought and I could build what I needed. I thought the simple part of this was going to be getting the raspbian lite up and working. The challenge was going to be interfacing the sensors since I seemed to have some issues with the software (sensiron has made a bit of a mess of things code wise, another story for when I get it working!). Getting the wifi to work reliably was by far the biggest issue and not one I had run into before. The connections seemed to appear and disappear and sometimes just not work at all once they had failed and since it was occasionally usable and had triple checked all the settings it must be something flaky.

Some reboots I never seemed to ever get wifi, others it seemed to have it but maybe for a few seconds or minutes. If I kept it alive when I caught it there I could potentially use it for as long as I wanted but once I disconnected and stopped transferring bytes it would drop off to never come back and there seemed to be no commands I could run on the device to make it wake the wifi back up.

Initially I went down the rabbit hole of configuration of wpa_supplicant configuration and country code setting to ensure all this was perfect. I hand picked all the settings to match my wifi network setup precisely so as to avoid oddness but it didn’t improve anything. Then I went through all the wpa tooling to ensure the network could be seen and all was well, it could see the wifi network it just wasn’t interested in getting an IP and staying connected, but the router always reported that the dhcp lease was given. Maybe it was something in the dhcp client but nothing notable appeared in the logs at all. I hard coded the wifi configuration in network/interfaces and that seemed to initially work. But I got two wifi networks with different IPs, then one and finally none a few minutes later.

A random diagnostics post somewhere on the internet suggested running iwconfig to check the settings. ThenI realised what could be responsible for all my problems. power management: on. Decades of power saving techniques we haven’t yet worked out how to do this without causing weird and wonderful bugs, power management causes issues regularlyand if things have a tendency to disappear and not come back then that smells like a classic power management problem. I ran iwconfig wlan0 power off and that was the magic pill, the wifi started working correctly from that point on and is still solid 24 hours later.

I wanted that run everytime the device booted. So I needed a script, the systemd configuration and a couple of commands to enable it.

/root/wifipwr.sh
#!/bin/sh
iwconfig wlan0 power off
/etc/systemd/system/wifipwr.service
[Unit]
Description=Remove wifi power management
After=network.target

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/root/wifipwr.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
Commands to enable
$ systemctl daemon-reload
$ systemctl enable wifipwr.service

That was all it needed. Now the wifi is nice and reliable for unattended use. Given the bug appears to trace back to 2016 I am not sure why I never run into it before but this sort of power management isn’t appropriate for Raspberrypi’s running the lite operating system as more often than not that operating system is being used to act as a server or service not as a client. Many will utilise ethernet for reliability concerns when it comes to servers but in this case its a remote sensor and some software to read it and wifi is a lot easier than running cable everywhere. It may very well be a bug in the driver but I think in the meantime it would be nice if the default was changed for the lite variant of the OS due to its more likely use as a service device not a client computer.