Professional Documents
Culture Documents
Use HTTP To Send Data To The IBM Watson IoT Platform From An ESP8266 - DeveloperWorks Recipes
Use HTTP To Send Data To The IBM Watson IoT Platform From An ESP8266 - DeveloperWorks Recipes
Use HTTP To Send Data To The IBM Watson IoT Platform From An ESP8266 - DeveloperWorks Recipes
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%2Fdeveloper.ibm.com%2F
TermsofUse(/termsofuse/)
ThirdPartyNotice(/thirdpartynotice/)
IBMPrivacy(http://www.ibm.com/privacy/)
https://developer.ibm.com/recipes/tutorials/usehttptosenddatatotheibmiotfoundationfromanesp8266/
7/7