Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By diffstorm
#34005 Hello friends,

I have written a state machine code which has WiFi.mode, WiFi.softAP, WiFi.begin, WiFi.softAPConfig, WiFi.disconnect and WiFi.hostname functions in loop(). These functions are kind of write functions and sometimes I think that when another process tries to access to the same target with my code (for ex: disconnect writes somewhere at the same time the wifi thread writes there too) and the other process gets exception and restarts the ESP chip. Sometimes it can be unstoppable infinitive resets of the chip.

Here is the output:
Code: Select allFatal exception (0):<CR><LF>
epc1=0x402022b0, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000000, depc=0x00000000<CR><LF>
<CR><LF>
Exception (0):<CR><LF>
epc1=0x402022b0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000<CR><LF>
<CR><LF>
ctx: cont <CR><LF>
sp: 3ffeccf0 end: 3ffed120 offset: 01a0<CR><LF>


I want to learn is there any mutex-like protection method to avoid this issue?
If I do the connection in setup() there is no problem, but in loop() it causes periodic resets..

Also another question:
I've connected the ESP to my wifi one time and after that I've reflashed the sketch without any connection code (deleted the wifi codes to leave it on its own) and I saw that it goes to connect to my wifi again. So it has written the last config in non volatile place and uses that data to connect if nobody calls wifi functions.
Where it is written this config? Can I read this to compare with needed data to decide that I should change the config or not?
How can I delete that config data? I've tried WiFi.disconnect() but it didn't delete the config.

So long question, sorry for details. I hope the answer is useful for others too.

Thanks in advance
User avatar
By martinayotte
#34064
diffstorm wrote:If I do the connection in setup() there is no problem, but in loop() it causes periodic resets..

Why do you want to place those calls into loop() instead of setup() ?
Maybe you should share you code to allows people giving you hints.
User avatar
By diffstorm
#34193 Hello,

Thanks for the answer.

I simply want to change wifi mode at runtime. For example I have my own wifi network and I've powered the esp device and it goes to connect to my network, BUT if my network is not available temporarily, I want it to be an AP first that I can connect to it's network, after a specific time it should re-try to connect my own network as STA, if it cannot - again it will be AP.

I just call that peace of code in loop() and "mostly" it gets crashed with fatal exception (0) and restarting.
Code: Select all       WiFi.mode(WIFI_STA);
        if(strlen(psk) == 0)
          WiFi.begin(ssid);
        else
          WiFi.begin(ssid, psk);


My test case is to power down my home router and start up the esp, esp goes AP, after that I power up again my home router. So esp retries after 1 min to be a STA and I face that problem.
I've added Serial.print codes on the lines and understood that it crashes on the WiFi.mode(WIFI_STA); line of code.
(I tried disconnect routines before wifi.mode, but no chance)

I need to learn why.

Sincerely.