-->
Page 1 of 2

HTTP POST ISSUES..

PostPosted: Thu Nov 17, 2016 9:35 am
by Mads Svendsen
Hello :D

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 allURL: 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 :D

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");
  }

Re: HTTP POST ISSUES..

PostPosted: Fri Nov 18, 2016 3:39 am
by Mads Svendsen
PROBLEM SOLVED :)

Re: HTTP POST ISSUES..

PostPosted: Fri Nov 18, 2016 4:18 am
by torntrousers
I don't see anything wrong with your ESP code and suspect its something about the message being sent that the server doesn't like, perhaps a missing header or something about the JSON format not being quite what the server is expecting. Can you get to the server logs to see if that has an error logged, or trace the message from a client that does work to see exactly what message that sends?

Re: HTTP POST ISSUES..

PostPosted: Fri Nov 18, 2016 8:53 am
by Mads Svendsen
Problem turned out to be a missign .json at the end of our URL.

Thanks for taking a look at it :)