mathworks / thingspeak-arduino

ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
https://thingspeak.com
431 stars 231 forks source link

WiFi.begin(ssid, pass); location in code #62

Closed petrb8 closed 4 years ago

petrb8 commented 4 years ago

Hello, I'm having problems with wifi connection using your example code "WriteMultipleFields". Sometimes ESP module can't connect to wifi and just keep printing dots over serial line. I think the problem is with location of WiFi.begin. In your example code in located inside while loop, but in offical wifi example code it's located outside while loop. As soon as I move to outside of while loop, wifi is connecting correctly.

` // Connect or reconnect to WiFi

WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network if(WiFi.status() != WL_CONNECTED){ Serial.print("Attempting to connect to SSID: "); Serial.println(SECRET_SSID); while(WiFi.status() != WL_CONNECTED){ Serial.print("."); delay(5000);
} Serial.println("\nConnected."); } `

v-c commented 4 years ago

The WiFi.begin is in the loop is to ensure a connection is reestablished if the connection is dropped after the device is initially powered on.

Consider the use case of a device connected to a WiFi signal generated by a cellphone. At some point the WiFi signal drops (perhaps because you turned off your hot spot). If the WiFi.begin is not inside the loop, you would need to reset your ESP after the WiFi was turned back on. With the current example, the connection would be re-established and the example would work without needing your ESP to be reset.

As for why you may be seeing dots on the serial line, your WiFi network may be configured to not allocate an IP address if it detects that a device is connected at that IP address. You'd probably want to look at your router and check what is happening when your device is continuously printing dots.