HTTP POST ISSUES..
Posted: Thu Nov 17, 2016 9:35 am
Hello
I'm working on a IoT project trying to improve working environment in offices and do need to upload a lot of data to an IBM Bluemix Server which has been set up for us. I have received the following information regarding the server and JSON object to submit:
However, I can't get it to work. I'm trying using the following code and I just keep getting a Bad request (400) back. Hope some of you can spot the mistake, as I really have been trawling to find an answer.
Thanks a lot - Best regards Mads Svendsen
Code below
I'm working on a IoT project trying to improve working environment in offices and do need to upload a lot of data to an IBM Bluemix Server which has been set up for us. I have received the following information regarding the server and JSON object to submit:
Code: Select all
URL: http://sentar:horseraddish@analytics-server.eu-gb.mybluemix.net/api/measurements
HTTP Method: POST
Header: "Content-Type: application/json"
JSON format:
{
"device_id": "abc123",
"state_code": 000,
"trigger": "event",
"amb_temp": 0.00,
"obj_temp": 0.00,
"ir_read": 0.00,
"us_read": 0.00,
"mag_m_z": 0.00,
"mag_m_x": 0.00,
"mag_m_y": 0.00
}
However, I can't get it to work. I'm trying using the following code and I just keep getting a Bad request (400) back. Hope some of you can spot the mistake, as I really have been trawling to find an answer.
Thanks a lot - Best regards Mads Svendsen
Code below
Code: Select all
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
const char* ssid = "UnderStregen";
const char* password = "Standerlampe44";
void setup(){
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
}
}
void loop(){
StaticJsonBuffer<500> jsonBuffer;
//create json object handler
JsonObject& root = jsonBuffer.createObject();
root["device_id"]= 1000; //device id should be a static-
root["state_code"]= 10;
root["trigger"]= "trigger";
root["amb_temp"]= 24.0;
root["obj_temp"]= 27.0;
root["ir_read"]= 10.0;
root["us_read"]= 70.2;
root["mag_m_z"]= 3000.0;
root["mag_m_x"]= 100.0;
root["mag_m_y"]= 200.0;
String data;
root.printTo(data);
Serial.print("Data ");
Serial.println(data);
HTTPClient http;
http.begin("http://sentar:horseraddish@analytics-server.eu-gb.mybluemix.net/api/measurements");
http.addHeader("Content-Type", "application/json");
http.POST(data);
http.writeToStream(&Serial);
http.end();
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(2000);
Serial.println("Done");
}