Open
Description
Hi,
I'm new to Arduino programming so this might be a stupid question. I'm trying to post the readings of a temperature sensor from my Arduino MKR1000 to my REST API. This sometimes works, however the Arduino is not always posting to the same URL. It seems random when it posts to the defined URL, and when to just /.
Code:
#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Constants
const char ssid[] = "MY_SSID";
const char pass[] = "MY_PASSWORD";
const char serverAddress[] = "192.168.178.206";
const int port = 8000;
const char endpoint[] = "/api/log_entries";
const char authHeader[] = "Bearer 1|YMAmi7TcLgar9GouMxV8LODhf1zRtcFY7XZnUg7SlLf5BwyCWxJHMZt3E3vDXil9JbhuhqurMBWWg02Z";
// Wifi and Http client
WiFiClient wifi;
int status = WL_IDLE_STATUS;
HttpClient client = HttpClient(wifi, serverAddress, port);
// Sensor related
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
}
Serial.print("Connected to SSID: ");
Serial.println(WiFi.SSID());
// Start the sensor library
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("POST data: ");
String postData = "name=Arduino&temperature=";
postData += temperature;
Serial.println(postData);
Serial.println("Making POST request");
client.flush(); // Doesn't seem to help
client.beginRequest();
Serial.println(endpoint); // Always correct
client.post(endpoint);
client.sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
client.sendHeader("Accept", "application/json");
client.sendHeader("Authorization", authHeader);
client.endRequest();
client.print(postData);
Serial.println("Request finished");
int statusCode = client.responseStatusCode();
Serial.print("Status code: ");
Serial.println(statusCode);
String response = client.responseBody();
Serial.print("Response: ");
Serial.println(response);
client.stop();
Serial.println("Wait five seconds");
delay(5000);
}
Example serial output:
17:54:22.384 -> Connected to SSID: MY_SSID
17:54:23.148 -> Temperature: 25.06
17:54:23.148 -> POST data: name=Arduino&temperature=25.06
17:54:23.148 -> Making POST request
17:54:23.148 -> /api/log_entries
17:54:25.367 -> Request finished
17:54:39.393 -> Status code: 405
17:54:41.341 -> Response: {
............ -> ...
17:54:41.376 -> }
17:54:41.376 -> Wait five seconds
17:54:47.106 -> Temperature: 25.06
17:54:47.106 -> POST data: name=Arduino&temperature=25.06
17:54:47.106 -> Making POST request
17:54:47.106 -> /api/log_entries
17:54:47.329 -> Request finished
17:55:01.308 -> Status code: 201
17:55:02.309 -> Response: {"name":"Arduino","temperature":25.06,"updated_at":"2020-06-01T15:54:47.000000Z","created_at":"2020-06-01T15:54:47.000000Z","id":79}
17:55:02.309 -> Wait five seconds
17:55:08.028 -> Temperature: 25.06
17:55:08.028 -> POST data: name=Arduino&temperature=25.06
17:55:08.028 -> Making POST request
17:55:08.028 -> /api/log_entries
17:55:08.302 -> Request finished
17:55:23.318 -> Status code: 405
17:55:25.267 -> Response: {
............ -> ...
17:55:25.305 -> }
17:55:25.305 -> Wait five seconds
The status codes 405 are because the library is posting to /
instead of /api/log_entries
. If I let it run for a while, I think 60% of the requests are to the correct endpoint and successful, while the rest POSTs to /
and fails.
Any ideas?