The local ESP32 client only uses HTTP client to communicate with the server. I chose not to use WebSocket due to some early include errors. The error has been resolved later, but the code for both the HTTP server and client has already been completed.
To establish an HTTP client on ESP32, the HTTPClient.h library will be needed. One can also use the lower level esp_http_client.h. It offers more flexibility but also more complexity.
// Just use one of them #include #include "esp_http_client.h"
Initializing an HTTP client only takes one line of code with the HTTPClient library. Simply declare an HTTPClient class and that is it.
HTTPClient http;
When trying to use the client, call HTTPClient.begin() first to parse the server URL for future use. HTTPClient.GET() and HTTPClient.POST() function is very straightforward to use as well. POST() function takes either a string or a pointer to a binary value and its length as input and returns the HTTP code of the POST request. GET() function takes no parameters and returns the HTTP code of the GET request. To get the payload from the server, HTTPClient.getString() should be used. After the desired request has been made, HTTPClient.end() function should be called to free up resources.
// POST Request example http.begin(post_url); http.addHeader("Content-Type", "image/jpg"); int httpResponseCode = http.POST(fb->buf,fb->len); //Send the actual POST request http.end(); // GET Request example http.begin(post_url); int httpcode = http.GET(); if (httpcode > 0) { //Check for the returning code String payload = http.getString(); const char * p = payload.c_str(); Serial.println("HTTP RESPONSE CODE:"); Serial.println(httpcode); Serial.println(payload); if (strstr(p, "UnmatchedFaces")) { Serial.println("Unmatching Face Detected, Entry Denied."); } else if(strstr(p, "FaceMatches")) { Serial.println("Matching Face Detected, Entry Approved."); turnLock(); } else { Serial.println("No Face Detected, Please Try Again."); } } else { Serial.println("Error on HTTP GET request"); Serial.println(httpcode); } http.end();
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.