So you're a Noob? Post your questions here until you graduate! Don't be shy.
User avatar
By Fatfenders
#69765 When I place my "WiFi.begin" in "setup" it works just fine.
When I move it to "loop" it never connects.
I am trying to put together a project that will alert me if a hot water heater starts leaking. It could be years before it would send me a text so I don't want to connect until water is detected.

#include <ESP8266WiFi.h>
.
.
.
void setup()
.
.
.
void loop()
.
.
.
WiFi.begin("ssid","password");
User avatar
By philbowles
#69920 loop gets called about 50,000 times a second. You simply cannot expect that to work when it is getting called so fast that it has no time to do anything. Also, are you checking the return code to see when it's connected?

You need to structure you code so that it only tries to connect when the once-in-lifetime event occurs and does so while suspending / avoiding all other main loop activity until the connection is made. There's dozens of different ways to do that, but you need to read up on callbacks, asyncronous events and a few other things.

The "correct" answer is beyond the scope of a short forum post, I'm afraid!