My project involves reading an RFID tag, searching for the UID in my member list via a GET request, then passing the corresponding memberID to the next function. The next function sets various parameters via a POST request using the previously passed memberID.
The function to search for the UID and pass the memberID is working fine, but the next function refuses to connect to the server (HTTP response -1). However, if I call the second function first, it can establish a connection (HTTP response 201).
Here's my code snippet:
String generateBearerToken() {
Serial.println("Generating Bearer token...");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected. Cannot generate token.");
return "";
}
HTTPClient http;
WiFiClientSecure client;
client.setInsecure();
http.begin(client, TOKEN_ENDPOINT);
http.addHeader("Accept", "application/json");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "grant_type=client_credentials&client_id=" + String(clientSecretId) + "&client_secret=" + String(clientSecret);
int httpResponseCode = http.POST(payload);
String accessToken = "";
if (httpResponseCode == HTTP_CODE_OK) {
String response = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, response);
accessToken = doc["access_token"].as<String>();
Serial.println("Bearer token generated successfully.");
Serial.println(accessToken);
} else {
Serial.print("Failed to generate Bearer token. Error code: ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
}
http.end();
return accessToken;
}
void sendGetMember(String bearerToken, String uid) {
Serial.println("Sending GET Member...");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected. Cannot send GET Member.");
return;
}
HTTPClient http;
WiFiClientSecure client;
client.setInsecure();
http.begin(client, MEMBER_ENDPOINT);
http.addHeader("Authorization", "Bearer " + bearerToken);
int httpResponseCode = http.GET();
if (httpResponseCode == HTTP_CODE_OK) {
String response = http.getString();
Serial.println("GET Member successful");
Serial.println("Member:");
Serial.println(response);
DynamicJsonDocument doc(4096);
deserializeJson(doc, response);
JsonArray members = doc["value"];
for (JsonObject member : members) {
String memberId = member["id"];
String memberCode = member["code"];
if (memberCode == uid) {
Serial.println("Matching member found:");
Serial.println("Member ID: " + memberId);
sendPOST_Zeitwert(bearerToken, memberId);
return;
}
}
Serial.println("No matching member found for UID: " + uid);
} else {
Serial.print("Failed to send GET Member. Error code: ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
}
http.end();
}
void sendPOST_Zeitwert(String bearerToken, String memberId) {
Serial.println("Sending POST Zeitwert...");
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected. Cannot send POST Zeitwert.");
return;
}
HTTPClient http;
WiFiClientSecure client;
client.setInsecure();
http.begin(client, POST_Zeitwert);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", "Bearer " + bearerToken);
DynamicJsonDocument payload(256);
//payload["id"] = uid;
payload["personId"] = "2c502f6a-b0ee-467f-be59-93693b828e5a";
payload["type"] = "In";
payload["clientType"] = "Web";
String jsonString;
serializeJson(payload, jsonString);
int httpResponseCode = http.POST(jsonString);
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
if (httpResponseCode == HTTP_CODE_OK) {
Serial.println("POST_Zeitwert sent successfully");
} else {
Serial.print("Failed to send POST_Zeitwert. Error code: ");
Serial.println(httpResponseCode);
Serial.println(http.getString());
}
http.end();
}
void loop() {
if (millis() - lastNTPUpdate > NTP_UPDATE_INTERVAL) {
timeClient.update();
lastNTPUpdate = millis();
}
if (rfid.PICC_IsNewCardPresent()) {
if (rfid.PICC_ReadCardSerial()) {
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.print("RFID/NFC Tag Type: ");
Serial.println(rfid.PICC_GetTypeName(piccType));
Serial.print("UID:");
String uid = "";
for (int i = 0; i < rfid.uid.size; i++) {
uid += (rfid.uid.uidByte[i] < 0x10 ? "0" : "");
uid += String(rfid.uid.uidByte[i], HEX);
}
Serial.println(uid);
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
String bearerToken = generateBearerToken();
if (!bearerToken.isEmpty()) {
sendGetMember(bearerToken, uid);
}
}
}
delay(1000);
}
I tried swapping the order of sendGetMember and SendPost_TimeValue. Now sendGetMember is getting an HTTP response of -1.
I also tried to send the second request as an HTTP request. Now the HTTP response Code is 400:
"The plain HTTP request was sent to HTTPS port".