Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Added Tutorial & Sample Code for Intel Edison #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/hardware/IntelEdison/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 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:**

> 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
- 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 `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
/*********************************************************************
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 <Your Token>';
// Device ID
var deviceID = '<Device ID>';
// Message IDs
var outMessageID = '<Outbound Message ID>';
var inMessageID = '<Inbound Message ID>';
```

- 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_<Outbound Message ID>`.

### 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":"<Inbound Message ID>","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.
134 changes: 134 additions & 0 deletions src/hardware/IntelEdison/src/main.js
Original file line number Diff line number Diff line change
@@ -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 <Your Token>';
// Device ID
var deviceID = '<Device ID>';
// Message IDs
var outMessageID = '<Outbound Message ID>';
var inMessageID = '<Inbound Message ID>';


/*********************************************************************
Init Sensors & Pins
*********************************************************************/
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); // LED plugged into LED Socket at D2
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"));
}
15 changes: 15 additions & 0 deletions src/hardware/IntelEdison/src/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 3 additions & 2 deletions src/hardware/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down