ESP32 delay() doesn't work

Hi all,

I am using ESP32 to do a project by using delay() function. It seems to me that the delay() function never works!

I tried using the millis() as well and it doesn't work so well neither.

unsigned long ini= 0 ;

void setup()
{
    Serial.begin(115200);
    delay(10);

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

    // wait 5 seconds for connection:
    delay(5000);
  }

  Serial.println("Connected to WiFi"); 
  
  
  //c8yclient.subscriptionCreate(username,password,host, device_id,"mo" ,SubscriptionName, APIs,typeFilter,fragmentsToCopy);
  unsigned long endtime=millis();
  if((endtime-ini)>5000)
  Serial.println(endtime-ini);
  //c8yclient.tokenCreate(host, SubscriptionName, SubscriberName, expiretime);
  c8yclient.tokenCreatePassword(username,password,host, SubscriptionName, SubscriberName, expiretime);

The result is

Attempting to connect to SSID: MagentaWLAN-MYHU
Attempting to connect to SSID: MagentaWLAN-MYHU
Connected to WiFi
10152


But I can tell you the truth is that it doesn't wait 5s as I indicated.

BRs

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

Is this simulator code?

Please describe the problem better then you just did.

are these prints coming out more quickly than every 5 seconds?

since "ini" is set to zero and there is a 5 sec delay in the code earlier, it's guaranteed that millis() will return a value > 5000



value of millis = unsigned long ini= 0 ;
value of millis = 
value of millis = 0000void setup()
value of millis = {
value of millis = 0004    Serial.begin(115200);
value of millis = 0007    delay(10);
value of millis = 0017
value of millis = 0021 wifisecure.setInsecure();
value of millis = 0025 while (status != WL_CONNECTED) {
value of millis = 0027    Serial.print("Attempting to connect to SSID: ");
value of millis = 0029    Serial.println(ssid);
value of millis =         // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
value of millis = 0031    status = WiFi.begin(ssid, pass);
value of millis = 
value of millis =         // wait 5 seconds for connection:
value of millis = 5031    delay(5000);
value of millis =   }
value of millis = 
value of millis = 10031  Serial.println("Connected to WiFi"); 
value of millis =   
value of millis =   
value of millis =   //c8yclient.subscriptionCreate(username,password,host, device_id,"mo" ,SubscriptionName, APIs,typeFilter,fragmentsToCopy);
value of millis = 10033  unsigned long endtime=millis();
value of millis = 10034  if((endtime-ini)>5000)
// as ini has value 0 and endtime has value 10034 
// the result of the calulcation is 10034 - 0 = 10034 which is greater than 5000

// The if-condition is instantly true
value of millis =   Serial.println(endtime-ini);
value of millis =   c8yclient.tokenCreate(host, SubscriptionName, SubscriberName, expiretime);
value of millis =   c8yclient.tokenCreatePassword(username,password,host, SubscriptionName, SubscriberName, expiretime);

additionally your if-condition is outside the while-loop.

If you expected the code to wait this does not happen.
The if-condition

if((endtime-ini)>5000)

is executed once and then code-execution goes on
with

c8yclient.tokenCreate(host, SubscriptionName, SubscriberName, expiretime);

You should post your complete sketch using this method

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.