From 1a605aac7806af21922ebac8b81cc0ff98a6fdd9 Mon Sep 17 00:00:00 2001 From: Suyash Lakhotia Date: Wed, 5 Oct 2016 15:19:45 +0800 Subject: [PATCH 1/3] Added Tutorial & Sample Code for Intel Edison --- src/hardware/IntelEdison/README.md | 84 ++++++++++++++ src/hardware/IntelEdison/src/main.js | 134 ++++++++++++++++++++++ src/hardware/IntelEdison/src/package.json | 15 +++ src/hardware/README.md | 5 +- 4 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 src/hardware/IntelEdison/README.md create mode 100644 src/hardware/IntelEdison/src/main.js create mode 100644 src/hardware/IntelEdison/src/package.json diff --git a/src/hardware/IntelEdison/README.md b/src/hardware/IntelEdison/README.md new file mode 100644 index 0000000..df89ae6 --- /dev/null +++ b/src/hardware/IntelEdison/README.md @@ -0,0 +1,84 @@ +# Intel Edison + SAP HCP IoT Services + +## Prerequisites + +Follow the instructions [here](https://github.com/SAP/iot-starterkit/tree/master/src/prerequisites/account) to set up your SAP HCP account, enable IoT Services and set up the message types, device type & device needed for this tutorial. Make sure to note down the following while following the instructions: + +- Device ID +- Outbound Message ID +- Inbound Message ID +- OAuth Access Token + +## Setting Up the Edison + +Follow the steps below after following [Intel's instructions](https://software.intel.com/en-us/iot/library/edison-getting-started) on setting up the board to work with Intel XDK. + +### Hardware + +**Hardware Required:** + +- Intel Edison Compute Module +- Arduino Expansion Board +- Grove Base Shield +- Grove Cable (x4) +- Grove Temperature Sensor +- Grove Light Sensor +- Grove Rotary Angle Sensor +- Grove LED Socket +- LED + +**Setup:** + +1. Connect the Grove Base Shield to the Arduino Expansion Board. +2. Connect the LED Socket to `D2` on the Base Shield and plug an LED into the socket. +3. Connect the Temperature Sensor to `A0` on the Base Shield. +4. Connect the Light Sensor to `A1` on the Base Shield. +5. Connect the Rotary Angle Sensor to `A2` on the Base Shield. + +## Running the Project on Edison + +- Start a new blank project on Intel XDK. +- Copy over the code from `src/main.js` & `src/package.json` to your new project with the same file names. +- In `main.js`, set up the variables from Line 13 - Line 26 according to your own HCP account. You can get these values from your IoT Services Cockpit. + +```js +/********************************************************************* + Set Below Variables Accordingly +*********************************************************************/ +// IoT Server Host (Message Management Service URL) +var hostIoT = 'iotmms<...>.hanatrial.ondemand.com'; +// Path to Websocket Endpoint +var pathIoT = '/com.sap.iotservices.mms/v1/api/ws/data/'; +// OAuth Token +var authStrIoT = 'Bearer '; +// Device ID +var deviceID = ''; +// Message IDs +var outMessageID = ''; +var inMessageID = ''; +``` + +- Upload the project onto your Edison & run it. If set up correctly, you should see output like this: + +``` +Websocket Connected. +Data #1 Sent +Data #2 Sent +Data #3 Sent +... +``` + +### Accessing the Data + +1. Open up Message Management Service (MMS). +2. Click the "Display stored messages" tile. +3. The table with the data from the Edison will be named `T_IOT_`. + +### Turning the LED On/Off + +1. Open up Message Management Service (MMS). +2. Click the "Push messages to device" tile. +3. Enter your Device ID (from IoT Services Cockpit). +4. Select "ws" as the Method from the dropdown. +5. The message should be: `{"messageType":"","messages":[{"opcode":"LED Switch","operand":"<1 or 0>"}]}`. +6. Push the message. The LED connected to the Edison should switch on/off according to the operand value. diff --git a/src/hardware/IntelEdison/src/main.js b/src/hardware/IntelEdison/src/main.js new file mode 100644 index 0000000..a8175d0 --- /dev/null +++ b/src/hardware/IntelEdison/src/main.js @@ -0,0 +1,134 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true*/ +/*jshint unused:true*/ + +/********************************************************************* + Require Node Modules +*********************************************************************/ +var groveSensor = require('jsupm_grove'); +var mraa = require('mraa'); +var WebSocket = require('ws'); +var request = require('request'); + + +/********************************************************************* + Set Below Variables Accordingly +*********************************************************************/ +// IoT Server Host (Message Management Service URL) +var hostIoT = 'iotmms<...>.hanatrial.ondemand.com'; +// Path to Websocket Endpoint +var pathIoT = '/com.sap.iotservices.mms/v1/api/ws/data/'; +// OAuth Token +var authStrIoT = 'Bearer '; +// Device ID +var deviceID = ''; +// Message IDs +var outMessageID = ''; +var inMessageID = ''; + + +/********************************************************************* + Init Sensors & Pins +*********************************************************************/ +var tempSensor = new groveSensor.GroveTemp(0); +var lightSensor = new groveSensor.GroveLight(1); +var rotarySensor = new groveSensor.GroveRotary(2); + +var LEDPin = new mraa.Gpio(2); +LEDPin.dir(mraa.DIR_OUT); + + +/********************************************************************* + Init Websocket +*********************************************************************/ +var options = { + headers: { + Authorization: authStrIoT + } +}; + +var ws = new WebSocket('wss://' + hostIoT + pathIoT + deviceID, options); + +ws.on('open', function () { + console.log("Websocket Connected"); + + // On WebSocket connection, start sending data every two seconds. + startTempWatch(); + startLightWatch(); + startRotaryWatch(); +}); +ws.on('close', function () { + console.log("Websocket Disconnected"); +}); +ws.on('error', function (error) { + console.log("ERROR: " + error); +}); +ws.on('message', setLED); // On receiving a message, switch on/off the LED. + + +/********************************************************************* + Main Functions +*********************************************************************/ +var dataNum = 0; // Data Counter + +function setLED(data) { + data = JSON.parse(data); + + // Verify the inbound message ID & opcode --> Switch on/off LED according + // to the operand. + if (data.messageType == inMessageID) { + if (data.messages[0].opcode === 'LED Switch') { + if (data.messages[0].operand == 1) { + LEDPin.write(1); + console.log("LED Switched On"); + } else if (data.messages[0].operand == 0) { + LEDPin.write(0); + console.log("LED Switched Off"); + } + } + } +} + +function startTempWatch() { + setInterval(function () { + var a = tempSensor.raw_value(); + var resistance = (1023 - a) * 10000 / a; + var celsius_temperature = 1 / (Math.log(resistance / 10000) / 3975 + 1 / 298.15) - 273.15; + + updateIoT('TempSensor', celsius_temperature); + }, 2000); +} + +function startLightWatch() { + setInterval(function () { + updateIoT('LightSensor', lightSensor.value()); + }, 2000); +} + +function startRotaryWatch() { + setInterval(function () { + updateIoT('RotarySensor', rotarySensor.abs_deg()); + }, 2000); +} + +function updateIoT(sensor, value) { + dataNum++; + + // Generate timestamp. + var date = new Date(); + var time = Math.round(date.getTime() / 1000); + + // JSON data format for MMS messages. + var jsonData = { + mode: 'async', + messageType: outMessageID, + messages: [{ + timestamp: time, + sensor: sensor, + value: value + }] + }; + var strData = JSON.stringify(jsonData); + + // Send message to MMS through the WebSocket. + ws.send(strData, console.log("Data #" + dataNum + " Sent")); +} diff --git a/src/hardware/IntelEdison/src/package.json b/src/hardware/IntelEdison/src/package.json new file mode 100644 index 0000000..cce7496 --- /dev/null +++ b/src/hardware/IntelEdison/src/package.json @@ -0,0 +1,15 @@ +{ + "name": "inteledison-sapiot-sample", + "description": "", + "version": "1.0.0", + "main": "main.js", + "engines": { + "node": ">=0.10.0" + }, + "dependencies": { + "mraa": "latest", + "jsupm_grove": "latest", + "request": "^2.74.0", + "ws": "latest" + } +} diff --git a/src/hardware/README.md b/src/hardware/README.md index 01c7ea7..f272d78 100644 --- a/src/hardware/README.md +++ b/src/hardware/README.md @@ -1,9 +1,10 @@ We have successfully used the HCP IoT Services on * Desktop Systems * Smart Phones -* [iOS] (./iOS) +* [iOS](./iOS) * [Raspberry Pi](./raspberry-pi) -* Intel Galileo or Edison +* [Intel Edison](./IntelEdison) +* Intel Galileo * Beaglebone * UDOO * Arduino Yun From 8a9b0944f2b42da388333c5f27bc3fa43bdae65c Mon Sep 17 00:00:00 2001 From: Suyash Lakhotia Date: Wed, 5 Oct 2016 15:34:55 +0800 Subject: [PATCH 2/3] Updated README --- src/hardware/IntelEdison/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hardware/IntelEdison/README.md b/src/hardware/IntelEdison/README.md index df89ae6..9f74b28 100644 --- a/src/hardware/IntelEdison/README.md +++ b/src/hardware/IntelEdison/README.md @@ -17,6 +17,8 @@ Follow the steps below after following [Intel's instructions](https://software.i **Hardware Required:** +> If you do not have a particular sensor, please omit the relevant JS function from `src/main.js` when uploading the project onto your board. + - Intel Edison Compute Module - Arduino Expansion Board - Grove Base Shield @@ -38,7 +40,7 @@ Follow the steps below after following [Intel's instructions](https://software.i ## Running the Project on Edison - Start a new blank project on Intel XDK. -- Copy over the code from `src/main.js` & `src/package.json` to your new project with the same file names. +- Copy over the code from `src/main.js` & `src/package.json` to `main.js` & `package.json` respectively in your new XDK project. - In `main.js`, set up the variables from Line 13 - Line 26 according to your own HCP account. You can get these values from your IoT Services Cockpit. ```js From 2b96d99925aa2f62247c215b5ec8a4c131605988 Mon Sep 17 00:00:00 2001 From: Suyash Lakhotia Date: Wed, 5 Oct 2016 15:35:06 +0800 Subject: [PATCH 3/3] Added Comments for Sensors/Pins --- src/hardware/IntelEdison/src/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hardware/IntelEdison/src/main.js b/src/hardware/IntelEdison/src/main.js index a8175d0..a4e6d4b 100644 --- a/src/hardware/IntelEdison/src/main.js +++ b/src/hardware/IntelEdison/src/main.js @@ -29,11 +29,11 @@ var inMessageID = ''; /********************************************************************* Init Sensors & Pins *********************************************************************/ -var tempSensor = new groveSensor.GroveTemp(0); -var lightSensor = new groveSensor.GroveLight(1); -var rotarySensor = new groveSensor.GroveRotary(2); +var tempSensor = new groveSensor.GroveTemp(0); // Temp Sensor at A0 +var lightSensor = new groveSensor.GroveLight(1); // Light Sensor at A1 +var rotarySensor = new groveSensor.GroveRotary(2); // Rotary Angle Sensor at A2 -var LEDPin = new mraa.Gpio(2); +var LEDPin = new mraa.Gpio(2); // LED plugged into LED Socket at D2 LEDPin.dir(mraa.DIR_OUT);