Go Down

Topic: ESP8266: yield() (Read 774 times) previous topic - next topic

deloarts

Good morning everyone,

there is something I'm struggeling with but I can't figure out what causes the bug.
For those who are interested in what my code is doing:
  The user can use his RFID chip and two buttons to log his work time.
  Those data can be viewed via a neat web interface.
  Or you just view it on GitHub.


Lets hit you with my code snippet:

Code: [Select]

...

if (enableLog)
{
    while (logTimer > millis())
    {
        yield();

        if (digitalRead(BUTTON_LOGIN) || digitalRead(BUTTON_LOGOUT))
        {
            logTimer = millis() - 1;

            StaticJsonBuffer<250> jsonBuffer;
            String json = postRfid(digitalRead(BUTTON_LOGIN) ? F("/login") : F("/logout"), getUid());
            JsonObject& root = jsonBuffer.parseObject(json);

            long status = root[F("status")];
            String lcdLine1 = root[F("lcdLine1")];
            String lcdLine2 = root[F("lcdLine2")];

            lcdMessage(lcdLine1, lcdLine2, 0, true);
            status == 0 ? delay(4000) : delay(2000);         

            while (digitalRead(BUTTON_LOGIN) || digitalRead(BUTTON_LOGOUT))
            delay(50);
        }
    }

    lcdClear();
    digitalWrite(LED_LOGIN, LOW);
    digitalWrite(LED_LOGOUT, LOW);
    enableLog = false;
}

...

String postRfid(String path, String data)
{
    int response = 0, attempt = 0;
    char jsonMsgBuffer[100];
    String payload = "", url = "http://" + String(serverAddress) + ":" + String(serverPort) + path;

    StaticJsonBuffer<100> buffer;
    JsonObject& json = buffer.createObject();

    json["client"] = "controller";
    json["rfid_id"] = data;
    json.prettyPrintTo(jsonMsgBuffer, sizeof(jsonMsgBuffer));

    client.begin(url);
    client.addHeader("Content-Type", "application/json");

    while (response != 200)
    {
        if (attemptTimer < millis())
        {
            response = client.POST(jsonMsgBuffer);
            payload = client.getString();
            attempt++;
            attemptTimer = millis() + 1000;
        }
        else if (attempt == 2)
        {
            lcdMessage("Server: " + String(response), "Attempt: " + String(attempt), 2000, false);
        }
        else if (attempt > 2)
        {
            lcdError("Server: " + String(response), "Attempt: " + String(attempt), 2000, false);
        }
    }

    client.end();

    digitalWrite(LED_LOGIN, LOW);
    digitalWrite(LED_LOGOUT, LOW);

    return payload;
}



Problem

The problem here is yield();. I need it here because as long as the user is allowed to log his time (a four seconds window after he held his RFID chip to the device) the system would crash without the yield(); because the watchdog would bark in.

Now what's the problem? It's the line where I post to the server.
In 90% of every case it works just fine, but sometimes the ESP posts to the url .../logout even if the login-button is pressed (and vice versa). I really have no clue why this happens.

One thing is for certain, it is because of yield(). I now use a delay of 10ms instead of it and it works like a charm. I could go with it, I really don't care if I use yield() or delay(), but I want to know why this happens.


If anybody knows what causes the problem: Hit me!
And thanks for any help or suggestions on my code!

6v6gt

#1
May 02, 2018, 10:13 am Last Edit: May 02, 2018, 10:18 am by 6v6gt
I guess you'll have solved it by now, butI think your problem is a button bounce here:

Code: [Select]

// first button read
if (digitalRead(BUTTON_LOGIN) || digitalRead(BUTTON_LOGOUT))
. . .
. . .
// second digital read
String json = postRfid(digitalRead(BUTTON_LOGIN) ? F("/login") : F("/logout"), getUid());  


If the first read get a HIGH for the pin BUTTON_LOGIN but it is LOW at the time of the second digital read, it will be treated by your logic a log out.
The prolonged delay() instead of a yield() probably minimises the bounce effect.
Maybe try to capture the values of the button pins on the first read instead of re-reading them.





Go Up