Overview

Skill Level: Any Skill Level

Easy

Ingredients

An ESP8266

Step-by-step

  1. Why use HTTP instead of MQTT?

    HTTP can be useful in some environments, for example, if a firewall blocks non-HTTP port 80 traffic, or perhaps an MQTT client isn’t available for your device, or maybe you just want to get started in IoT with something you’re already familiar with like plain old HTTP.

  2. First you’ll need an ESP8266

    The ESP8266 is a powerful microcontroller with built in WiFi support. Its really cheap and is Arduino compatable making it really easy to use and a great platform for IoT devices. There are a variety of types of ESP8266 to choose from, all of them should work with this recipe, here i’m using a NodeMCU style board which is easy to prototype with because it has USB support built in for programming and powering the ESP8266.

  3. Next, install the Arduino IDE

    Get the Arduino IDE here. Using a recent version of the IDE – 1.6.5 or later – makes it easy to add the ESP8266 support to the IDE. With the Arduino IDE installed use the Board Manager function to add support for the ESP8266, see here for how to do that. https://github.com/esp8266/Arduino#installing-with-boards-manager

  4. Upload and run the sketch

    In the Arduino IDE enter the following sketch:

     /**
    * IBM IoT Foundation using HTTP
    *
    * Author: Ant Elder
    * License: Apache License v2
    */
    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>

    //-------- Customise these values -----------
    const char* ssid = "<yourWiFiSSID>";
    const char* password = "<yourWiFiPassword>";

    #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 "<yourDeviceToken>" // 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.com/messaging/HTTPSDevice.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, password);
    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.com/messaging/payload.html
    String payload = String("{ "d": {"aMessage": ") + millis()/1000 + "} }";
    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);
    }

    Customise the lines after “//——– Customise these values ———–” with your WiFi network SSID and with the IoT device details. To get going quickly you can use the provided defaults which use the IBM Watson IoT Platform’s “quickstart” service, or for a registered device customize the ORG, DEVICE_TYPE, DEVICE_ID, and TOKEN values.

    Upload the sketch to you ESP8266 and then open the Serial Monitor and you should see the values being published to the IBM Watson IoT Platform.

  5. View the published data on the IBM Watson IoT Platform Dashboard

    If you used the quickstart service you can see the posted data at:

    https://quickstart.internetofthings.ibmcloud.com/#/device/Test1/sensor/

    or for a registered device:

    https://<YOUR_ORG>.internetofthings.ibmcloud.com/dashboard/#/devices/browse/drilldown/<YOUR_DEVICE_TYPE>/<YOUR_DEVICE_ID>

  6. Thats it!

    Hope you found this a useful introduction to using the HTTP with an ESP8266 and the IBM Watson IoT Platform.

    For more information there is documentation on the IBM Watson IoT Platform HTTP support at: https://docs.internetofthings.ibmcloud.com/messaging/HTTPSDevice.html.

    As a comparison, a recipe for using MQTT on the ESP8266 is: https://developer.ibm.com/recipes/tutorials/connect-an-esp8266-with-the-arduino-sdk-to-the-ibm-iot-foundation/.

11 comments on"Use HTTP to send data to the IBM Watson IoT Platform from an ESP8266"

  1. KiranShashi March 26, 2016

    Found this recipe helpful – I was able to get going by following the instructions listed here. Thanks.

  2. KiranShashi March 27, 2016

    To send a random number between 1-100, I just replaced the payload line
    String payload = String(“{ \”d\”: {\”aMessage\”: “) + millis()/1000 + “} }”;
    with
    int randNumber = random ( 1,100);
    String payload = String(“{ \”d\”: {\”aMessage\”: “) + randNumber + “} }”;

  3. The URL used to send data to the IBM Watson IoT Platform over HTTP is going to be replaced with the new one.
    Updated information can be found at https://console.ng.bluemix.net/docs/services/IoT/applications/api.html and https://console.ng.bluemix.net/docs/services/IoT/devices/api.html

  4. FernandoG.Souza November 06, 2016

    If someone have compiling error on loop use this line:
    String payload = String(“{ \”d\”: {\”aMessage\”: “) + millis()/1000 + “} }”;

  5. Several people have asked about using secure connections from the ESP8266 to Watson, so there is an version of this sketch updated to use HTTPS in the Watson Github repo here: https://github.com/ibm-watson-iot/device-arduino/blob/master/samples/ESP8266HttpSecure/ESP8266HttpSecure.ino

  6. electrodude@1995 June 17, 2017

    great tutorial! works great with the quickstart functionality. but i am not able to get the data at the place where i have registered my device. https://.internetofthings.ibmcloud.com/dashboard/#/devices/browse/drilldown//.
    I put my credentials in correctly but these data arnt coming here. I was getting the code 403 every time i uploaded the code. It works perfectly with quickstart though.
    Please assist me!

    • What release of the ESP8266 Arduino SDK are you using? You do need a very recent version for this to work – 2.3.0 came out in June 2017, that one should work.

  7. Hi antelder,

    I am trying to get data from IBM Watson using HTTP GET, but always fails with error code 405 method not allowed.
    Any help on how to get HTTP GET working would be appreciated.

    Thanks,
    Satish.

  8. I tried to connect my esp8266-01 to my org and the HTTP POST Response is -1. Why is that so? Thanks in advance!

  9. Hello guys!
    I’m a begginer at programming with ESP8266, Arduino IDE and C++.
    I’ve made small progress by connecting the ESP8266 to my home wifi, however I’d like to send some data to my uncle web server through a POST request and I’m not making any progress.
    I’ve already tried to send the data using the WiFiClient library and the ESP8266HTTPClient library but none of them worked. I’m starting to think that maybe the web server data that my uncle gave me are not correct.
    Here is the data he gave me:

    POST /rods/airlo/firstmodule/ HTTP/1.1

    Host: iotsystem.synology.me:314

    Content-Type: application/json

    Cache-Control: no-cache

    Postman-Token: (it’s a long code with numbers and letters but I won’t poste here for security reasons)

    The way I see there’s only two reasons why I can’t send the data: I’m not puting these data correctly in my code or he gave me wrong data.
    Can anyone help me?

    Thanks a lot!!

  10. Hi xirb77
    Did you find a resolution to the -1 response from “I tried to connect my esp8266-01 to my org and the HTTP POST Response is -1. Why is that so?”
    I’m also having this issue

Join The Discussion