I have a microcontroller whose primary job is going to be to let me know if we have a power outage during the night since my backup generator is not an autoswitching arrangement. I know there are plug-in the wall devices to perform this type of notification but this processor will also do other functions in my master bedroom.
I am using an IF statement that looks at the vDC input level from the battery switchover module to determine if my utility service has been lost; the processor will reset due to the slow switchover time of the battery module unless I add a Farad cap, which I am not currently wanting to do. When the processor restarts during the power outage I do not not want it to be concerned with making a WiFi connection. Is the IF statement the best way to bypass things and just go on to the void loop so it can turn on my sonAlert and LED and wake me up? I am using an ESP32 for this application.
...thanks...
```cpp
void setup() {
Serial.begin(115200);
startup_Millis = millis();
pinMode(ONBOARD_LED, OUTPUT);
pinMode(sonAlert, OUTPUT); // audible alarm warning
pinMode(waterLeakLED, OUTPUT); // visual waterbed leak warning
pinMode(powerOutageLED, OUTPUT); // visual power outage warning
pinMode(Vdc_PwrModule, INPUT); // DC voltage from switchover module
pinMode(Vdc_WtrSensor, INPUT); // DC voltage from water detector module
pinMode(Silence_Reset, INPUT); // external reset/silence switch
delay(3000); // give Vdc_PwrModule input time to stabilize
if (Vdc_PwrModule >= 400) {
while (WiFi.status() != WL_CONNECTED) {
Initial_Connect_Attempts++;
Serial.println(F("Attempting to Connect To Local LAN: "));
Serial.println(wifissid);
WiFi.begin(wifissid, wifipass);
delay(5000);
if (Initial_Connect_Attempts == 8) {
Serial.println(F("5 LAN connect attempts unsuccessful; going to connectToWifi()"));
connectToWiFi();
}
Serial.print(".");
Serial.print(".");
}
Serial.println("");
Serial.print(F(" Connected to WiFi network with IP Address: "));
Serial.println(WiFi.localIP());
Serial.print(F(" Attempting to connect to WPA network..."));
// initTime("CST6CDT,M3.2.0,M11.1.0");
server.begin();
Serial.print("WiFi Status: ");
Serial.println(WiFi.status());
Serial.println();
Serial.print("ESP Board MAC Address: ");
Serial.println(WiFi.macAddress());
}
}
void loop(){
Generally correct, but you have 3 more wifi statements that will fail now, I don't know how they will fail but it could be a crash. You should consider a construct like the following
if wifi status is not connected AND retries is less than Y then
retry x times (check your code, 8 one place 5 another)
else
turn on sonAlert and led
Your code executes WiFi-begin over and over again inside the while-loop.
Then your code prints "5 LAN connect attempts unsuccessful;going to connectToWifi()"
and calls a function connectToWiFi()
Does this text really make sense?
Does your ESP32 really try to connect to a wire-based LAN-Port?
And how does your connectToWiFi() function look like?
I guess this is a big code-design-flaw trying to make a WiFi-connection in function loop() and in case it does not connect calling a function connectToWiFi();
I understand your point about WiFi.begin. I copied this code years ago and it always worked but moving the begin command to the outside of the while loop is easy enough to do.
Not sure what you mean by this question? Where is LAN-Port mentioned? Are you talking about the "Attempting to connect to WPA network" possibly? If so that probably needs to be removed.
With my sketches "big design flaws" are baked in.
If I get them to work I consider myself fortunate.
I do have code in the void loop() to ensure a wifi connect present to display webpage data.
Here's the connectToWiFi() function you asked about:
If I understand the requirements correctly, I'd probably make the complete WLAN connection activity a separate RTOS task. That is, it monitors the state of the WLAN connection and attempts to rebuild the connection if required and does this in parallel with other activities in the loop(). The task is initiated from setup() but setup() does not wait for it.
That means, of course, that in the loop(), any code section which relies on the availability of the WLAN must first test if the connection is available and skip that section if there is no connection.
Looking at the bits of code you supplied, function connectToWiFi() looks like like it would be a good candidate for running as a separate RTOS task. Remove any code from setup() which attempts to build the initial connection and create the task to execute connectToWiFi().
I don't think you need an extra RTOS-task for the WiFi-Connection.
You should just post your complete sketch from the very first line of code to the very last line of code.
This will make it possible that your potential helpers can get an overview about what your code is all doing in summary. And then to decide what kind of concept works good for your application.
Do you understand me? Complete sketch. A single post here can take 120.000 characters. This would be a code with 3000 lines each and every line containing 40 characters.
So in reality as you use empty lines and many lines have less than 30 characters your code could have 5000 or 6000 lines of code.
I guess you are using functions.
So to get the overview over your code will be pretty easy.
Just in case you have a function loop() with 1000 lines it is really time to show you how to devide code into functional functions. (that is where the name for the "thing" comes from)
I think you have to make up your mind whether you need to see the whole code or not before either making your own suggestions or condemning other people's suggestions.
Where as it is possible to suggest conceptual solution models based on an overview of the user's problem it is not so easy to categorically dismiss such suggestions without a clear reason.
The OP's problem appears to be one of a class of problems where the code must not block in the absence of a network and must proceed to do the activities it is capable of pending the restoration of the network connection. Using a separate RTOS task on an ESP32 is a potentiality valid solution for consideration in such a case.
Serial.println(F("5 LAN connect attempts unsuccessful; going to connectToWifi()"));
which mentions the abbreviation "LAN" which is normally used for wired connections.
Is this just a type and you mean WLAN?
If you take into account that user @edthewino describes himself as
Sounds like having a rather low knowledge. So fiddling around with tasks is more likely to increase problems instead of reducing them.
If you can confirm that it will be harmless to setup such a RTOS-task for the WiFi-connection: post the code that does it.
Still the whole code must be revised where in the code are parts that rely on a established Wifi-connection and would fail or behave strange if these parts of the code try to do something with WiFi in case the WiFi-connection is not established.