HTTP Response 404, when trying to post to IBM Cloud
Posted: Wed Apr 11, 2018 9:06 am
Hello,
I am trying to send data to IBM using the code explained here
When I try the same code, I was getting an error. So I have slightly modified the payload, but I am getting an HTTP POST Response 404. Could you please check what am I doing wrong?
My code is:
[code#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//-------- Customise these values -----------
const char* ssid = "Connect";
//#define ORG "u7y3d9" // your organization or "quickstart"
//#define DEVICE_TYPE "ESP8266-YourDeviceName" // use this default for quickstart or customize to your registered device type
//#define DEVICE_ID "ESP8266" // use this default for quickstart or customize to your registered device id
#define ORG "quickstart" // your organization or "quickstart"
#define DEVICE_TYPE "ESP8266" // use this default for quickstart or customize to your registered device type
#define DEVICE_ID "Test1" // use this default for quickstart or customize to your registered device id
#define TOKEN "test-token" // not used with "quickstart"
#define EVENT "myEvent" // use this default or customize to your event type
//-------- Customise the above values --------
String url = "http://"ORG".internetofthings.ibmcloud.com/api/v0002/device/types/"DEVICE_TYPE"/devices/"DEVICE_ID"/events/"EVENT;
void setup()
{
Serial.begin(115200); Serial.println();
if (ORG != "quickstart") { // for POST URL doc see: https://docs.internetofthings.ibmcloud. ... evice.html
url.replace("http://", String("https://use-token-auth:") + TOKEN + "@");
}
Serial.print("IoT Foundation URL: "); Serial.println(url);
Serial.print("Connecting to: "); Serial.print(ssid);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
void loop()
{
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
// a simple payload, for doc on payload format see: https://docs.internetofthings.ibmcloud. ... yload.html
// String payload = String("{ \"d\": {\"aMessage\": \") + millis()/1000 + \"} }\");
String payload = "{ \"d\": {\"aMessage\": \"4\"} }";
Serial.print("POST payload: "); Serial.println(payload);
int httpCode = http.POST(payload);
Serial.print("HTTP POST Response: ");
Serial.println(httpCode); // HTTP code 200 means ok
http.end();
delay(10000);
}][/code]
I am getting the same response of HTTP RESPONSE 404 whether using "quickstart" or not.
WiFi is getting connected but I am not able to send data. Can someone please share what am I doing wrong?
I am trying to send data to IBM using the code explained here
When I try the same code, I was getting an error. So I have slightly modified the payload, but I am getting an HTTP POST Response 404. Could you please check what am I doing wrong?
My code is:
[code#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//-------- Customise these values -----------
const char* ssid = "Connect";
//#define ORG "u7y3d9" // your organization or "quickstart"
//#define DEVICE_TYPE "ESP8266-YourDeviceName" // use this default for quickstart or customize to your registered device type
//#define DEVICE_ID "ESP8266" // use this default for quickstart or customize to your registered device id
#define ORG "quickstart" // your organization or "quickstart"
#define DEVICE_TYPE "ESP8266" // use this default for quickstart or customize to your registered device type
#define DEVICE_ID "Test1" // use this default for quickstart or customize to your registered device id
#define TOKEN "test-token" // not used with "quickstart"
#define EVENT "myEvent" // use this default or customize to your event type
//-------- Customise the above values --------
String url = "http://"ORG".internetofthings.ibmcloud.com/api/v0002/device/types/"DEVICE_TYPE"/devices/"DEVICE_ID"/events/"EVENT;
void setup()
{
Serial.begin(115200); Serial.println();
if (ORG != "quickstart") { // for POST URL doc see: https://docs.internetofthings.ibmcloud. ... evice.html
url.replace("http://", String("https://use-token-auth:") + TOKEN + "@");
}
Serial.print("IoT Foundation URL: "); Serial.println(url);
Serial.print("Connecting to: "); Serial.print(ssid);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
void loop()
{
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
// a simple payload, for doc on payload format see: https://docs.internetofthings.ibmcloud. ... yload.html
// String payload = String("{ \"d\": {\"aMessage\": \") + millis()/1000 + \"} }\");
String payload = "{ \"d\": {\"aMessage\": \"4\"} }";
Serial.print("POST payload: "); Serial.println(payload);
int httpCode = http.POST(payload);
Serial.print("HTTP POST Response: ");
Serial.println(httpCode); // HTTP code 200 means ok
http.end();
delay(10000);
}][/code]
I am getting the same response of HTTP RESPONSE 404 whether using "quickstart" or not.
WiFi is getting connected but I am not able to send data. Can someone please share what am I doing wrong?