--- /dev/null
+/* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This test is based on Raspberry Pi with GY-30 Sensor. */
+
+var assert = require('assert');
+var I2C = require('i2c');
+
+var i2c = new I2C();
+var configuration = {};
+
+// var I2C_BUS = 0x1;
+var ADDR = 0x1D;
+var RESOLUTION = (19.6 / 128);
+
+var MODE = 0x16;
+var MODE_MEASUREMENT = 0x01;
+// var MODE_LEVEL_DETECTION = 0x02;
+
+var MODE_2G = 0x04;
+// var MODE_4G = 0x05;
+// var MODE_8G = 0x00;
+
+var CAL_X1 = 0x10;
+var CAL_X2 = 0x11;
+var CAL_Y1 = 0x12;
+var CAL_Y2 = 0x13;
+var CAL_Z1 = 0x14;
+var CAL_Z2 = 0x15;
+
+var X_OUT = 0x06;
+var Y_OUT = 0x07;
+var Z_OUT = 0x08;
+
+configuration.device = '/dev/i2c-1';
+configuration.address = ADDR;
+var wire = i2c.open(configuration, setupAccel);
+
+var counter = 0;
+function setupAccel(err) {
+ if (err) {
+ throw err;
+ }
+ var arr = [];
+ var callback = setupAccel;
+ switch (counter) {
+ case 0: arr = [MODE, MODE_2G | MODE_MEASUREMENT]; break;
+ case 1: arr = [CAL_X1]; break;
+ case 2: arr = [CAL_X2]; break;
+ case 3: arr = [CAL_Y1]; break;
+ case 4: arr = [CAL_Y2]; break;
+ case 5: arr = [CAL_Z1]; break;
+ case 6: arr = [CAL_Z2]; callback = prepareReadData; break;
+ }
+ counter = counter + 1;
+ wire.write(arr, callback);
+}
+
+var counterRead = 0;
+var x = 0;
+var y = 0;
+var z = 0;
+
+function prepareReadData(err) {
+ if (err) {
+ throw err;
+ }
+ var arr = [];
+ switch (counterRead) {
+ case 0: arr = [X_OUT]; break;
+ case 1: arr = [Y_OUT]; break;
+ case 2: arr = [Z_OUT]; break;
+ }
+ wire.write(arr, readData);
+}
+
+function readData(err) {
+ if (err) {
+ throw err;
+ }
+ wire.read(1, doneReading);
+}
+
+function doneReading(err, res) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(res.length, 1, 'I2C read failed.(length is not equal)');
+ var val = res[0];
+ if (val >= 128) {
+ val = val - 256;
+ }
+ val = val * RESOLUTION;
+ switch (counterRead) {
+ case 0: x = val; break;
+ case 1: y = val; break;
+ case 2: z = val; break;
+ }
+ counterRead++;
+ var timeout = 0;
+ if (counterRead === 3) {
+ var ts = Math.round(new Date().getTime());
+ console.log('read [ts, x, y, z]: ', ts + ', ' + x + ', ' + y + ', ' + z);
+ sendData(ts, x, y, z);
+ counterRead = 0;
+ timeout = 1000;
+ }
+ setTimeout(prepareReadData, timeout);
+}
+
+var https = require('https');
+// var timers = require('timers');
+
+function sendData(ts, x, y, z) {
+ var data = JSON.stringify({
+ data: {
+ timestamp: ts,
+ x: x,
+ y: y,
+ z: z,
+ },
+ sdid: '8fc47772f9c84007b9967e62bb364823',
+ type: 'message',
+ });
+
+ var options = {
+ 'method': 'POST',
+ 'hostname': 'api.artik.cloud',
+ 'path': '/v1.1/messages',
+ 'headers': {
+ 'content-type': 'application/json',
+ 'content-length': data.length,
+ 'authorization': 'Bearer 00a26417f1454a58bf71248d91ce3fb4',
+ },
+ };
+
+ console.log('Sending data to Artik Cloud ');
+
+ var req = https.request(options, function(res) {
+ var resBody = '';
+
+ res.on('data', function(chunk) {
+ console.log('Received response from server');
+ resBody += chunk.toString();
+ console.log(resBody);
+ });
+
+ res.on('end', function() {
+ console.log('Request Ended. Final data sent by the server - ');
+ console.log(resBody);
+ console.log('Waiting for next Interval\n');
+ });
+ });
+
+ req.write(data);
+ req.end();
+}