Hi couple of days I'am trying to start working simple project with Arduino Uno Wifi rev2. The scope is to read rfid data from card or chip, send rfid code to webserver and read response from it. Response will contain name of user which is assigned to sent rfid code. I'm trying to achieve this with sending POST request from Arduino to laravel API - and after that from laravel will be send response with name of the rfid user. But I didn't even start with reading rfid data because I'm stuck just with sending POST request to webserver and to read response from server.
This is the Arduino code:
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
char ssid[] = "SSID_SECRET";
char pass[] = "PASS_SECRET";
int port = 80;
const char serverAddress[] = "https://www.schedy.sk"; // server name
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
IPAddress gateway = WiFi.gatewayIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
Serial.println("making POST request");
String postData = "rfid=abcde&test=12";
Serial.print("Post Data Length: ");
Serial.println(postData.length());
client.beginRequest();
client.post("/api/rfids");
client.sendHeader("Content-Type", "application/x-www-form-urlencoded");
client.sendHeader("Content-Length", postData.length());
//client.sendHeader("X-Custom-Header", "custom-header-value");
client.beginBody();
client.print(postData);
client.endRequest();
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
Laravel API. Just for testing purposes I've defined simple routes in /routes/api.php:
Route::middleware('auth:api')->get('/user', function (Request $request)) {
return $request->user();
});
Route::get('/rfids', 'RfidController@index');
Route::post('/rfids', 'RfidController@store');
And in RfidController.php is:
public function index()
{
return "get test";
}
public function store(Request $request)
{
return response("post test")
}
I've tried post and get requests with https://reqbin.com/ for url: https://www.schedy.sk/api/rfids. Everything looks fine but with Arduino I'm still getting status code 400:
making POST request
Post Data Length: 17
Status code: 400
Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at kmbtwo.vps.websupport.sk Port 80</address>
</body></html>
Wait five seconds
I'm quite desperate with this problem. I tried several libraries and procedures but still getting 400 status code or -3. In server - apache2 logs I cannot get more information about the problem ... just that that there is attempt to make POST request to /api/rfids but answer is 400. Dont know the reason. Can somebody help me?