sap iot services tutorial 5

5
SAP HANA CLOUD PLATFORM – IOT SERVICES Using Tessel as the Hardware Aaron Williams / PM SAP HCP & SAP IOT Twitter: @aarondonw, SCN: Aaron Williams Description This show you how you can post data to your HCP IoT Services account, a Tessel as your hardware device. This is part of a series, to see the other items follow this link to SCN. System prerequisites Have an SAP HCP Trial Account You have a Tessel- www.tessel.io You have done the other tutorials in this series, or have an application that uses the IoT Services. Go here for some examples. Target group Application developers People interested in the SAP HANA Cloud Platform- IoT Services Target group requirements Basic programming language, ideally in Java, JavaScript/NodeJS, and/or Python.

Upload: sap-hana-cloud-platform

Post on 03-Aug-2015

178 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: SAP IoT Services tutorial 5

SAP HANA CLOUD PLATFORM – IOT SERVICES Using Tessel as the Hardware

Aaron Williams / PM SAP HCP & SAP IOT Twitter: @aarondonw, SCN: Aaron Williams Description This show you how you can post data to your HCP IoT Services account, a Tessel as your hardware device. This is part of a series, to see the other items follow this link to SCN. System prerequisites • Have an SAP HCP Trial Account • You have a Tessel- www.tessel.io • You have done the other tutorials in this series, or have an application that uses the IoT Services.

Go here for some examples. Target group • Application developers • People interested in the SAP HANA Cloud

Platform- IoT Services

Target group requirements • Basic programming language, ideally in Java,

JavaScript/NodeJS, and/or Python.

Page 2: SAP IoT Services tutorial 5

Using Tessel as the Hardware

2

Step 1: Set up- Tessel

Explanation Screenshot

USE Google Chrome or Firefox browser

This assumes that you have a tessel to use as your hardware device. If not, you can take the code and figure out how to hook up other devices.

1. Go to www.tessel.io and install the needed drivers. Also go through a number of the examples there.

2. Make sure that you have gone through the wifi part and have connected your tessel to the internet.

3. Connect your tessel with the climate module.

4. Create a file named tessleIoT.js, and save it. You will need to navigate to this, so put in a place where you can easily get to it.

5. Copy and paste the code on the right to the file and save it. There are a lot of comments in the code. You will need to go in and customize it for your account. Namely, you need to change the hostIoT (approx.. line 14), authStrIoT (approx. line 18), and deviceID (approx.. line 19).

/* * By Aaron Williams - Product Manager SAP HCP/IoT * Original by Philip Mugglestone * Copyright SAP Labs, LLC */ var tessel = require('tessel'); var climatelib = require('climate-si7020'); var climate = climatelib.use(tessel.port['A']); var hostIoT = 'iotmms<userID>trial.hanatrial.ondemand.com'; var portIoT = 443; var pathIoT = '/com.sap.iotservices.mms/v1/api/http/data/'; var authStrIoT = 'Bearer <auth string>'; var deviceId = '<deviceId>'; // which device are we? var stationId = 1; var xTemp; var xHumid; var xbright; var xtimestamp; var date = new Date(); var time = date.getTime (); climate.on('ready', function () { console.log('Connected to si7020'); setImmediate(function loop () {

Page 3: SAP IoT Services tutorial 5

Using Tessel as the Hardware

3

climate.readTemperature('f', function (err, temp) { climate.readHumidity(function (err, humid) { temp = Math.round(temp); humid = Math.round(humid); if (temp != xTemp && humid != xHumid) { setTimeout(function(){ if (xTemp != null) { var led1 = tessel.led[1].output(1); updateIoT(temp,humid,27); } xTemp = temp; xHumid = humid; }, 300); } led1 = tessel.led[1].output(0); setTimeout(loop, 1000); }); }); }); }); function updateIoT(temp, humid,brightness) { var http = require('https'); var options = { host: hostIoT, port: portIoT, path: pathIoT + deviceId, agent: false, headers: { 'Authorization': authStrIoT, 'Content-Type': 'application/json;charset=utf-8' }, method: 'POST', }; options.agent = new http.Agent(options); callback = function(response) { var body = ''; response.on('data', function (data) { body += data; }); response.on('end', function () { //console.log("END:", response.statusCode, JSON.parse(body).msg); }); response.on('error', function(e) { console.error(e); }); } var req = http.request(options, callback); req.on('error', function(e) { console.error(e); }); console.log ("time was:"); console.log (time); date = new Date(); time =date.getTime(); console.log(time); req.shouldKeepAlive = false; var jsonData = { "mode":"sync", "messageType":"1", "messages": [{ "Humidity": humid, "Temperature": temp, "Brightness": brightness, "timestamp": time }] } var strData = JSON.stringify(jsonData); console.log("POST jsonData:" + strData); req.write(strData); req.end(); } climate.on('error', function(err) { console.log('error connecting module', err); });

6. Go to your cockpit and click on the JAVA Applications, and click on your Java application, make sure that it is running.

7. In the command window (Windows) or terminal (Mac), navigate to where your tessel code is.

8. Start the tessel application by typing à

$ tessel run tessleIoT.js

Page 4: SAP IoT Services tutorial 5

Using Tessel as the Hardware

4

You will now see data pumping to your HCP instance. The code will post data whenever there is a change. To do this, just hold the temp sensor in your fingers.

If you get a compile error, make sure that there wasn’t a copy/paste error. Some times, copying can cause issues. If you get other errors, make sure that your JAVA Application is up and running.

9. Go back to the HCP Cockpit and click on the link to your Application URLs

You should see your graph with the new data coming in.

10. To stop the node server, press control-c

It is that easy.

Next Steps. From looking at the code you should be able to add in posting data to any RPi, Beagle Bone, or Intel Edision project that you have. If you prefer python, look at the

Page 5: SAP IoT Services tutorial 5

Using Tessel as the Hardware

5

Citations/ Acknowledgements This was based upon a post by Michael Ameling and the IoT Services Starter Kit. I also got a lot of help from Philip Mugglestone of the Hana Academy team.

© 2014 SAP SE or an SAP affiliate company. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company. SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Please see http://www.sap.com/corporate-en/legal/copyright/index.epx#trademark for additional trademark information and notices.