I'm attempting to use an ESP32 with Arduino IDE as a RFID door lock. I have everything working as it should, however it is not using both cores of the esp32. This means that my code will continually loop listening for RFID. process as follows:
Poll for RFID card -> If RFID detected pass to Rest API -> If user allowed then open solenoid -> Update Screen to say welcome to username passed from Rest API
Because I am using a delay to hold the mosfet open, it won't update the screen until after the lock has finished. I did experiment putting the update display code above the lock, but this just delayed opening the lock. To do both at the same time I believe I need to use both cores? Unless there is another way?
All of the multicore examples I can find online are using loops. So I can push the polling for RFID card into another task, but how can I call the update screen script? I'm getting pretty confused.
Please find some code extracts below.
This is the loop, just waits for card to be presented
{
if (nfc.tagPresent())
{
readString = "";
NfcTag tag = nfc.read();
String scannedUID = tag.getUidString();
scannedUID.replace(" ", "-");
Serial.println(scannedUID);
restlookup(scannedUID);
}
}
If tag is present, then passed to this procedure:
void restlookup(String UID){
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
String getReq = "GET /webapi/api/EmpAuth?id=" + UID;
Serial.println(getReq);
client.println(getReq);
client.println();
}
else {
Serial.println("connection failed");
}
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.println(c)
}
}
}
display.clearDisplay();
readString.replace("\"", "");
if (readString == "fail"){
// tone(buzzPin, errorfreq);
// delay(timeOn);
// noTone(buzzPin);
// tone(buzzPin, errorfreq);
// delay(timeOn);
// noTone(buzzPin);
display.setCursor(0,30);
display.clearDisplay();
displaytextcent("Not Authorised");
display.display();
delay(2000);
restdisplay();
}
else{
digitalWrite(13, HIGH); //activate mosfet
delay(timeOpen);
digitalWrite(13, LOW); //deactivate mosfet
display.setCursor(0,30);
display.clearDisplay();
displaytextcent("Welcome");
displaytextcent(readString);
display.display();
delay(2000);
restdisplay();
}
}
}
So, my problem is that because everything is sequential, it's not quite as polished as I would like. It works.... but isn't the best.
If anyone has any suggestions I would really appreciate it. I don't have to use two cores, I just can't find a way to get everything to work at the same time.
Thanks
Andrew