UPDATE: As of the newer versions of Raspbian, it is now much simpler to keep Wi-Fi from going to sleep:
sudo iw dev wlan0 set power_save off
Yup, that’s it.
It seemed like I couldn’t contact my Pi after a while (and my SSH session would end seemingly for no reason). I would start a ping and it would time out for the first few times before it would start responding. At first I thought the Pi itself was, or the USB ports were going to sleep, but the Pi has no power saving functions. I finally came across the answer. In my case it was the USB wifi adapter itself that was going to “sleep” or low power mode. This works with Realtek 8192cu chipset (very common).
First, see if this is the cause:
If it can’t find the file, you aren’t using the module, and your wifi probably uses a different chip.
If it’s 0, power management is disabled.
If it’s 1, power management is enabled.
To disable power management, create a new file:
sudo nano /etc/modprobe.d/8192cu.conf
Put the following in:
# Disable power management options 8192cu rtw_power_mgnt=0
Save, exit, and then reboot. All done.