Clarifications first
Did you try to keep a ping command running on the Raspberry (ping your router IP for example)? Does it stop after awhile? Or does it work but you can't connect to SSH somehow?
Disable WiFi power saving
Are you using power saving mode on the WiFi dongle? Check it:
$ iw wlan0 get power_save
Power save: on
If it is ON, perhaps try to set it to OFF: iw wlan0 set power_save off
.
To make this change persistent, if you are using NetworkManager you can follow this gist to disable WiFi power saving. If you are not using NetworkManager, on Raspbian/Debian you can edit the file /etc/network/interfaces
and add a post-up
entry for your interface, example:
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
post-up iw wlan0 set power_save off
Check the power supply
As I said in a comment, try to put your USB WiFi dongle on a external USB hub with its own power. It is possible that for example when the USB dongle needs to increase the Tx Power of the transmission because of for exemple some more noise in your radio environment, the RPi can't give enough power and the USB dongle behaviour can be undefined (e.g. it is turned off). You could also use a more powerful USB power supply for your RPi. What is the power of yours? Can it deliver up to 2A at 5V? (check on the power supply itself, it is often written in really small).
Check your logs
Check your system logs (e.g. /var/log/messages) does it tell you about some problems related to your WiFi hardware or about the network?
Check your hardware
When you start the RPi and that you have WiFi working, do you see the USB dongle in the list reported by lsusb -v
? And after you lost the wifi, is it still listed by the same command?
Check the connection info
When you have the WiFi up and running, what are the output of iw wlan0 link
and iw wlan0 station dump
?
Then once the wifi is lost, what are the output of both commands?
And once you have lost wifi, are you still able to view your wifi network?
$ sudo iw wlan0 scan | less
Run the above on your RPi when it has lost the connection, and see in the output (might take several seconds to be displayed) if there is your wifi network name.
iwconfig wireless-power off
and I tried find anything out what driver my Dongle uses but I have no idea