How can I automatically re-connect to a WiFi network? In this question I am asking about how to stop my WiFi from disconnecting.

If I won't find an answer to that, the next best thing is to get my phone to re-connect automatically. The disconnect reason is: "Class 3 frame received from nonassociated STA"

I am on a rooted device running cyanogen mod on Android 5.

I'm using AFWall+ and OpenVPN, if that is relevant. OpenVPN is set to re-connect automatically already, which works.

Due to AFWall+ blocking non-vpn traffic, whenever I connect to WiFi it says "There is no Internet connectivity, do you still want to connect? [Yes/No]", ticking the "Don't ask again" box has no effect.
This might be relevant and if the phone does automatically reconnect, I obviously don't want this pop-up to prevent an automated re-connect.
Your issue is caused by Captive Portal¹ Check kicking in and not permitted through by AFWall+. So if you can live without that auto-detection, you could simply disable it:



# check what the original setting is. Should give "1" for enabled, "0" for disabled
settings get global captive_portal_detection_enabled
# now let's disable it
settings put global captive_portal_detection_enabled 0
# if you want to re-activate it
settings put global captive_portal_detection_enabled 1


Implications:


no more pop-ups when connecting to e.g. a hotel network using Captive Portal. Easy work-around: just open a web browser and navigate to any url, the portal should "capture" you for login
no more x on the WiFi symbol – so you might miss the fact of not reaching any network until you try manually (e.g. by visiting some web page and ending up on the Captive Portal's login page)
automatic re-connect to "known WiFis" should simply work.


I've used that trick myself when having set up AFWall+ in a quite restricting mode, and it worked for me. As you (OP) just confirmed in a comment on the question, it did the trick for you as well – despite the additional complication of a VPN.

See Captive Portal parameters for more details on manipulation.



¹ for those who don't know what a "Captive Portal" is, please see Wikipedia