0% found this document useful (0 votes)
163 views

Use HTTP To Send Data To The IBM Watson IoT Platform From An ESP8266 - DeveloperWorks Recipes

This document provides a tutorial for using HTTP instead of MQTT to send sensor data from an ESP8266 microcontroller to the IBM Watson IoT Platform. It describes how to set up the ESP8266, write an Arduino sketch to send HTTP POST requests to the IBM cloud at regular intervals containing sensor readings, and view the data on the IBM cloud dashboard. The tutorial is intended for developers who want to learn how to connect basic IoT devices to the cloud using HTTP as an alternative to MQTT.

Uploaded by

baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views

Use HTTP To Send Data To The IBM Watson IoT Platform From An ESP8266 - DeveloperWorks Recipes

This document provides a tutorial for using HTTP instead of MQTT to send sensor data from an ESP8266 microcontroller to the IBM Watson IoT Platform. It describes how to set up the ESP8266, write an Arduino sketch to send HTTP POST requests to the IBM cloud at regular intervals containing sensor readings, and view the data on the IBM cloud dashboard. The tutorial is intended for developers who want to learn how to connect basic IoT devices to the cloud using HTTP as an alternative to MQTT.

Uploaded by

baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

antelder
(https://developer.ibm.com/recipes/author/antelder/)

UseHTTPtosenddatatotheIBM
WatsonIoTPlatformfroman
ESP8266
MostIBMWatsonIoTPlatformexamplesshowusing
MQTTtopublishdata,howeveryoucanalsouse
HTTPinstead.Thisrecipeshowsyouhow...
2941

Requirements
AnESP8266

https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

1/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

Skilllevel

(https://www.i
referingURL=
http
(http://www.fa
to
u=https://dev
send
http
data
(https://twitter
to
to
send
HTTPcanbeusefulinsomeenvironments,forexample,ifafirewallblocksnon the
data
HTTPport80traffic,orperhapsanMQTTclientisntavailableforyourdevice,or ibm
to
maybeyoujustwanttogetstartedinIoTwithsomethingyourealreadyfamiliarwith
iot
the
likeplainoldHTTP.
foundation
ibm
from
iot
an
foundation
esp8266)
from
an
esp8266/)
TheESP8266isapowerfulmicrocontrollerwithbuiltinWiFisupport.Itsreallycheap
andisArduinocompatablemakingitreallyeasytouseandagreatplatformforIoT
devices.ThereareavarietyoftypesofESP8266tochoosefrom,allofthemshould
workwiththisrecipe,hereimusingaNodeMCUstyleboardwhichiseasyto
prototypewithbecauseithasUSBsupportbuiltinforprogrammingandpowering
theESP8266.
Easy

WhyuseHTTPinsteadofMQTT?

Firstyou'llneedanESP8266

https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

2/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

Next,installtheArduinoIDE
GettheArduinoIDEhere(https://www.arduino.cc/en/Main/Software).Usingarecent
versionoftheIDE1.6.5orlatermakesiteasytoaddtheESP8266supporttothe
IDE.WiththeArduinoIDEinstalledusetheBoardManagerfunctiontoaddsupport
fortheESP8266,seehereforhowtodothat.
https://github.com/esp8266/Arduino#installingwithboardsmanager

Uploadandrunthesketch
IntheArduinoIDEenterthefollowingsketch:

https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

3/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

/**
*IBMIoTFoundationusingHTTP
*
*Author:AntElder
*License:ApacheLicensev2
*/
#include<ESP8266WiFi.h>
#include<ESP8266HTTPClient.h>

//Customisethesevalues
constchar*ssid="<yourWiFiSSID>";
constchar*password="<yourWiFiPassword>";

#defineORG"quickstart"//yourorganizationor"quickstart"
#defineDEVICE_TYPE"ESP8266"//usethisdefaultforquickstartorcustomizeto
#defineDEVICE_ID"Test1"//usethisdefaultforquickstartorcustomizetoyour
#defineTOKEN"<yourDeviceToken>"//notusedwith"quickstart"
#defineEVENT"myEvent"//usethisdefaultorcustomizetoyoureventtype
//Customisetheabovevalues

Stringurl="http://"ORG".internetofthings.ibmcloud.com/api/v0002/device/types

voidsetup(){

Serial.begin(115200);Serial.println();

if(ORG!="quickstart"){//forPOSTURLdocsee:https://docs.internetofthi
url.replace("http://",String("https://usetokenauth:")+TOKEN+
}
Serial.print("IoTFoundationURL:");Serial.println(url);

Serial.print("Connectingto:");Serial.print(ssid);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(500);
Serial.print(".");
}

Serial.print("nWiFiconnected,IPaddress:");Serial.println(WiFi.localIP
}

voidloop(){
HTTPClienthttp;
http.begin(url);
http.addHeader("ContentType","application/json");
//asimplepayload,fordoconpayloadformatsee:https://docs.internetofthi
https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

4/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

Stringpayload=String("{\"d\":{\"aMessage\":")+millis()/1000+
Serial.print("POSTpayload:");Serial.println(payload);
inthttpCode=http.POST(payload);
Serial.print("HTTPPOSTResponse:");Serial.println(httpCode);//HTTPcode2
http.end();

delay(10000);
}

Customisethelinesafter//CustomisethesevalueswithyourWiFi
networkSSIDandwiththeIoTdevicedetails.Togetgoingquicklyyoucanusethe
provideddefaultswhichusetheIBMWatsonIoTPlatformsquickstartservice,or
foraregistereddevicecustomizetheORG,DEVICE_TYPE,DEVICE_ID,and
TOKENvalues.
UploadthesketchtoyouESP8266andthenopentheSerialMonitorandyoushould
seethevaluesbeingpublishedtotheIBMWatsonIoTPlatform.

IBMdeveloperWorks(http://www.ibm.com/developerworks/)/DeveloperCenters(/centers/)

RECIPE

CreateNewRecipe (https://developer.ibm.com/recipes/insert)

Learnmore(https://www.youtube.com/watch?v=wE6Yo8RWJlY)

ViewthepublisheddataontheIBM
WatsonIoTPlatformDashboard
Ifyouusedthequickstartserviceyoucanseetheposteddataat:
https://quickstart.internetofthings.ibmcloud.com/#/device/Test1/sensor/
https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

5/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

orforaregistereddevice:

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

RecipePalette

Thatsit!
HopeyoufoundthisausefulintroductiontousingtheHTTPwithanESP8266and
theIBMWatsonIoTPlatform.
FormoreinformationthereisdocumentationontheIBMWatsonIoTPlatformHTTP
supportat:
https://docs.internetofthings.ibmcloud.com/messaging/HTTPSDevice.html
(https://docs.internetofthings.ibmcloud.com/messaging/HTTPSDevice.html).
Asacomparison,arecipeforusingMQTTontheESP8266
is:https://developer.ibm.com/recipes/tutorials/connectanesp8266withthearduino
sdktotheibmiotfoundation/.

https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

6/7

2/5/2016

UseHTTPtosenddatatotheIBMWatsonIoTPlatformfromanESP8266developerWorksRecipes

TutorialTags
arduino(https://developer.ibm.com/recipes/tag/arduino/)
esp8266(https://developer.ibm.com/recipes/tag/esp8266/)
http(https://developer.ibm.com/recipes/tag/http/)
iot(https://developer.ibm.com/recipes/tag/iot/)
iotf(https://developer.ibm.com/recipes/tag/iotf/)

ReportAbuse(https://www.ibm.com/developerworks/secure/reportabuse?referingURL=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fdeveloper.ibm.com%2F
TermsofUse(/termsofuse/)

ThirdPartyNotice(/thirdpartynotice/)

IBMPrivacy(http://www.ibm.com/privacy/)

https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/

7/7

You might also like