Greetings,
I’m going through your “Smart Home with Raspberry Pi … etc.” book.
So far so good, but now I’m doing this ESP32_Publish_Subscribe.ino exercise, and I have some questions.
(The WiFi scan program worked just fine).
When I ran the sketch, it appeared that it couldn’t connect: I kept getting “WiFi lost connection” messages. (My SSID and password are absolutely correct!)
I just let it run all day, and when I checked at some point, I noticed it had connected, and now was publishing MQTT packets just fine.
I checked the serial port log, and noticed it took 2 hours to connect. I also noticed some weird characters, which made me wonder if the module had reset itself at some point.
Here’s my question though:
Looking at the code, it looks like in setup() there is call to connectToWifi(),
If that fails (event 5), the WiFi reconnect one-shot timer is triggered (with a 2 second period).
So, what I would EXPECT is a pause of 2 seconds, and then a “Connecting to Wi-Fi…” message again.
What I instead DO see is endlessly repeating “WiFi lost connection” messages, every 1 second!
But NEVER a “Connecting to Wi-Fi…” message again.
As if that timer never triggers after 2 seconds, and as if “WiFi connect” keeps, itself, retrying every one second, which I find hard to believe, but who knows! (And if it does, maybe that should be stopped first?)
So what’s going on here? Why don’t I see the WiFi reconnecting message, and why do I keep seeing those Event 5 messages every 1 second? Why don’t I see a new “Connecting to WiFi ,,,” message?
21:57:25.325 -> [WiFi-event] event: 5 21:57:25.325 -> WiFi lost connection 21:57:26.337 -> [WiFi-event] event: 5 21:57:26.337 -> WiFi lost connection 21:57:27.346 -> [WiFi-event] event: 5 21:57:27.346 -> WiFi lost connection 21:57:28.375 -> [WiFi-event] event: 5 21:57:28.375 -> WiFi lost connection 21:57:29.414 -> [WiFi-event] event: 5 21:57:29.414 -> WiFi lost connection 21:57:30.443 -> [WiFi-event] event: 5 21:57:30.443 -> WiFi lost connection 21:57:31.454 -> [WiFi-event] event: 5 21:57:31.454 -> WiFi lost connection 21:57:32.446 -> Publishing on topic counter at QoS 1, packetId: 0[WiFi-event] event: 5
Hi.
I’ve taken a look at the code again and it seems everything is set up with the correct logic. So, I’m not quite sure why you’re always getting the WiFi lost connection.
Supposedly, the wifiReconnectTimer should run right after it notices the connection is lost.
xTimerStart(wifiReconnectTimer, 0);
So, it would call the connectToWifi() function right after.
I suggest replacing the connectToWifi() function with the following (it will attempt to connect to Wi-Fi 10 times before “giving up”. Maybe this gives it more time before the WiFi event is fired.
void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); const int maxAttempts = 10; int attempts = 0; while (attempts < maxAttempts) { WiFi.begin(WIFI_SSID, WIFI_PASSWORD); delay(500); // Add a small delay for connection to initiate if (WiFi.status() == WL_CONNECTED) { Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); return; // Exit the function if connected successfully } else { Serial.println("WiFi connection failed. Retrying..."); } attempts++; delay(1000); } Serial.println("Unable to connect to Wi-Fi after multiple attempts. Proceeding..."); }
Let me know the results.
Regards,
Sara
Thanks for the quick response.
I will try your suggestion later today and report back.
In the meantime I have two suggestions:
1. IF you happen to have an ESP32 board set up for this to test, could you run it with the original code, but with an invalid SSID, so as to force it into a situation like mine, where it fails to connect, and see if you can duplicate my observation? (WHY mine fails is a different story: probably hardware: 1 out of the three ESP32s I have works fine all the time).
2. Just a minor code enhancement for future releases:
The last two statements in loop():
// Publish an MQTT message on topic esp/bme680/temperature uint16_t packetIdPub1 = mqttClient.publish("counter", 1, true, String(random(0,20)).c_str()); Serial.printf("Publishing on topic %s at QoS 1, packetId: %i", "counter", packetIdPub1);
should probably be dependent on a “connected status”, to be initialized as false, set to true when WiFi connects, and back to false if WiFi disconnects. ONLY execute the above statements when the “connected status” is true, else display something like “Waiting for WiFi connection”.
Also, you probably want to end that above Serial.printf statement with a \n to avoid concatenating the next Serial.println, or Serial.printf 😉
Here are the results of your modified code:
I STILL don’t see the new “Connecting to Wi-Fi…” message, after WiFiEvent() starts a new timer upon a disconnect, in order to trigger a new connectToWiFi() call. So that’s still a bit of a mystery.
However, when the connectToWifi() function now completes after those 10 (failed) attempts, I THEN see a new “Connecting to Wi-Fi…” message.
Which is a very vague clue as to what’s going on. I think I’m beginning to see what the issue is, but I need to do some more thinking and testing.
As soon as I have more information, I will post it here.
Here’s the output of your modified version:
12:36:29.523 -> WiFi connection failed. Retrying... 12:36:30.121 -> [WiFi-event] event: 5 12:36:30.121 -> WiFi lost connection 12:36:30.547 -> [WiFi-event] event: 5 12:36:30.547 -> WiFi lost connection 12:36:31.050 -> WiFi connection failed. Retrying... 12:36:31.650 -> [WiFi-event] event: 5 12:36:31.650 -> WiFi lost connection 12:36:32.044 -> [WiFi-event] event: 5 12:36:32.044 -> WiFi lost connection 12:36:32.562 -> WiFi connection failed. Retrying... 12:36:33.082 -> [WiFi-event] event: 5 12:36:33.082 -> WiFi lost connection 12:36:33.571 -> [WiFi-event] event: 5 12:36:33.571 -> WiFi lost connection 12:36:34.044 -> WiFi connection failed. Retrying... 12:36:34.596 -> [WiFi-event] event: 5 12:36:34.596 -> WiFi lost connection 12:36:35.066 -> [WiFi-event] event: 5 12:36:35.066 -> WiFi lost connection 12:36:35.585 -> WiFi connection failed. Retrying... 12:36:36.154 -> [WiFi-event] event: 5 12:36:36.154 -> WiFi lost connection 12:36:36.578 -> Unable to connect to Wi-Fi after multiple attempts. Proceeding... 12:36:36.579 -> Connecting to Wi-Fi... << !!!! =========================== 12:36:36.579 -> [WiFi-event] event: 5 12:36:36.579 -> WiFi lost connection 12:36:37.067 -> WiFi connection failed. Retrying... 12:36:37.605 -> [WiFi-event] event: 5 12:36:37.605 -> WiFi lost connection 12:36:38.091 -> [WiFi-event] event: 5 12:36:38.091 -> WiFi lost connection 12:36:38.582 -> WiFi connection failed. Retrying...
Hi.
I tested on my side with wrong password and SSID and I get the “Connecting to Wi-Fi” message.
WiFi lost connection
Connecting to Wi-Fi...
[WiFi-event] event: 5
WiFi lost connection
Connecting to Wi-Fi...
Publishing on topic counter at QoS 1, packetId: 0[WiFi-event] event: 5
WiFi lost connection
Connecting to Wi-Fi...
And yes, you are right about the last two lines of code. I’ll fix those.
So, in clonclusion… I have no idea why you don’t get the “Connecting to Wi-Fi .. message, which is very weird…
Regards,
Sara