B4R Code Snippet ESP8266 yield() function

If it might have occured also to you to get a "Soft WDT Reset" error in an ESP8266 project then here is why this had happened and how to correct it:

ESP8266 runs several background tasks that check the state of the integrated circuit (f.e. maintain WiFi connection and other stuff) and has two watchdogs one software based and one hardware based that want to give priority to the state checking. For more information please check this URL:

In order to avoid triggering the WDT errors you will need to interrupt slightly (and repeatedly in each loop) long running procedures either with a Delay(1) command or with the yield() function.
I have not found this function in the B4R available commands so I created an InLine function that can be called this way:


yield() function in B4R:
RunNative("yld", Null)

#if C
void yld(B4R::Object* o){
    yield();
}
#End If
 

hatzisn

Well-Known Member
Licensed User
I am not sure I understood what you wrote to me... Can you provide an example? I suppose your answer is the following but I would like to understand better what you wrote:

B4X:
'Either write:
Delay(1)

'Or
RunNative("yld", Null)
 

candide

Active Member
Licensed User
sorry if i am not clear

i was thinking at something like that to make a not blocking Delay(xx) :
B4X:
Sub pause(ms As UInt)  'no blocking delay
    Dim start As ULong = Micros
    Do While (start +ms > Millis)
       RunNative("yield",Null)
    Loop
End Sub
#if C
 void yield (B4R::Object* u) {yield();}  
#End If
 

hatzisn

Well-Known Member
Licensed User
after test, it don't help on B4R side, it is a blocking sub :mad:


....The non blocking though is not for your code execution which keeps looping but for the ESP8266 background tasks...

This is the expected behavior because as far as my knowledge goes MCUs do not support multithreading directly but only though interrupts.
See here:

 
Top