3

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?

Johny
  • 31
  • 2
  • Hello @Johny, Just for a sanity check, when you add in the `auth:api` middleware, you might require a token to get passed up with your request. Also, is schedy.sk a live domain? Are you sure you shouldn't be using a test URL there? I recommend checking out Postman if you haven't already, it will make this much easier for you. Additionally, check storage/logs/laravel.log and see if there are any clues. – plushyObject Jan 20 '20 at 20:59
  • Hi @plushyObject, just for sure I've commented the auth:api route but without any change. Still getting 400 error. Looks like that apache server will stop the post request even before reaching laravel api so there is nothing in laravel log. – Johny Jan 20 '20 at 21:39
  • Is the URL correct? Your error shows kmbtwo.vps.websupport.sk but your code shows the server address as https://www.schedy.sk – plushyObject Jan 20 '20 at 22:43
  • The URL should be alright. Error shows kmbtwo ... because its just name of the server where is multiple domains. I have tried also GETrequest but the result is still the same - 400 status code. Also tried to change port number to 443 (https) and change https to http but without any positive change. When I test GET and POST request with https://reqbin.com/ with same url - https://www.schedy.sk/api/rfids everything work well - 200 status code and response body from server. – Johny Jan 21 '20 at 21:32
  • Interesting. I am not super adept with Arduino. Is there a way that you can check request/response headers to maybe compare them to reqbin? – plushyObject Jan 22 '20 at 18:48

1 Answers1

0

The problem was with the library - ArduinoHttpClient.h. With this library its not possible to make https request. I've solved it with wifinina.h whith which I was able to get success answer from server.

Johny
  • 31
  • 2