1. Home
  2. Knowledge Base
  3. Cloud API
  4. Getting Sensor Data

Getting Sensor Data

IoT-Ignite provides REST APIs to get latest or historical sensor data for using in web services or mobile applications. With the power of this APIs custom dashboards and reports can be generated.

Sensor

In IoT-Ignite perspective sensor is a device or application which detects(presence of an object, fire etc.), measures(amount of liquid, temperature of room, number of people, etc.) or feels(fall detection, sleep tracking, etc.) things. Each sensor should connect to a node. This node can be simple device that transfers sensor data to cloud connected device (gateway) via serial connection or wireless connection or it also can be cloud connected device itself. If sensor directly connected to gateway in this case its node named as built-in sensors.

Getting Latest Sensor Data

IoT-Ignite provides latest data from a sensor with “sensor-data” API. API returns latest data from sensor in JSON format.

https://api.ardich.com/device/{DEVICE-ID}/sensor-data?nodeId={NODE-ID}&sensorId={SENSOR-ID}

Parameters:

  • DEVICE-ID: Gateway Id which node connected to
  • NODE-ID: Node unique id which sensor connected to
  • SENSOR-ID: Sensor unique id

Response:

Let’s get token with Javascript and JQuery:

{
  "code": null,
  "subCode": null,
  "status": "OK",
  "description": null,
  "data": {
    "deviceId": "DEVICE-ID",
    "command": "SensorData",
    "data": "[\"XXX\"]",
    "createDate": 1476711243326,
    "nodeId": "NODE-ID",
    "sensorId": "SENSOR-ID",
    "cloudDate": 1476711243354
  },
  "ok": true
}

Parameters:

  • data.data: JSON array which includes latest data of sensor. (Single value for temperature, humidity, etc. or multiple value for complex sensors, gps, etc.)
  • data.createDate: Date of data created in unix date format
  • data.cloudDate: Date of data received by IoT-Ignite service in unix date format

Using API in JavaScript

Example below gets latest sensor data from IoT-Ignite.

//import jquery
var apiUrl = 'https://api.ardich.com/api/v3';

function getCurrentTemp() {
	apiGet('/device/{DEVICE-ID}/sensor-data?nodeId={NODE-ID}&sensorId={SENSOR-ID}', successCurrentTemperature, genericErrorFunction);
}

function getAccessToken() {
	return window.localStorage.getItem("access_token");
}

function apiGet(url, success, error){
	$.ajax({
		url : apiUrl + url,
		type : 'GET',
		data: '',
		dataType : 'json',
		headers : {'Authorization': 'Bearer ' + getAccessToken(), 'Content-Type': 'application/json'}
	}).done(success).fail(error);
}
function genericErrorFunction(error) {

	if(error.status == 401) {
		console.log("Authentication error");
		return;
	}
	else if(error.status == 404) {
		console.log("No Data Found");
	}
	else {
		console.log("ERROR");
	}
}
function successCurrentTemperature(object) {
	if(data.data.data) {
		var currentTemperatureJson = JSON.parse(object.data.data);
		currentTemperature = parseInt(currentTemperatureJson);
		$('.currentTemperature').text(currentTemperature);

	}
}
$(document).ready(function(){ getCurrentTemp(); });

Get Sensor Data History

Sensor data history also provided by IoT-Ignite APIs. Specified sensor’s historical data provided in JSON array.

https://api.ardich.com/api/v3/device/{DEVICE-ID}/sensor-data-history?nodeId={NODE-ID}&sensorId={SENSOR-ID}&startDate={START-DATE}&endDate={END-DATE}&pageSize=PAGE-SIZE

Parameters

  • DEVICE-ID: Gateway Unique Id which node connected
  • NODE-ID: Node unique id which sensor connected
  • SENSOR-ID: Specified sensor’s unique id
  • START-DATE: Start date of history in unix date format
  • END-DATE: End date of history in unix date format
  • PAGE-SIZE: Maximum number of values

Response

{
  "list": [
    {
      "deviceId": "DEVICE-ID",
      "command": "SensorData",
      "data": "[\"xxx\"]",
      "createDate": 1476711243326,
      "nodeId": "NODE-ID",
      "sensorId": "SENSOR-ID",
      "cloudDate": 1476711243354
    },
    {
      "deviceId": "DEVICE-ID",
      "command": "SensorData",
      "data": "[\"xxy\"]",
      "createDate": 1476711214149,
      "nodeId": "NODE-ID",
      "sensorId": "SENSOR-ID",
      "cloudDate": 1476711214403
    }
  ],
  "count": -1,
  "lastId": "1476711214149"
}

Parameters

  • list: Value list of specified sensor value. Array items specified as same as sensor-data API “data” object.
  • lastId: Last data receive date in unix time format

Using API in JavaScript

Example below gets latest sensor data history array from IoT-Ignite.

//import jQuery

var apiUrl = 'https://api.ardich.com/api/v3';
var deviceId = '{DEVICE-ID}';
var nodeId = '{NODE-ID}';
var sensorId = '{SENSOR-ID}';
function getTempHistory() {
  apiGet('/device/'+deviceId+'/sensor-data-history?nodeId='+nodeId+'&sensorId='+sensorId+'&startDate=' + (new Date().getTime() - graphDuration) + '&endDate=' + new Date().getTime() + '&pageSize=' + maxCount, successTemperatureHistory, genericErrorFunction);
}

function getAccessToken() {
  return window.localStorage.getItem("access_token");
}

function apiGet(url, success, error){
  $.ajax({
    url : apiUrl + url,
    type : 'GET',
    data: '',
    dataType : 'json',
    headers : {'Authorization': 'Bearer ' + getAccessToken(), 'Content-Type': 'application/json'}
  }).done(success).fail(error);
}

function successTemperatureHistory(data) {
  if(data.data.data) {
    console.log(data.list);
  }
}


function genericErrorFunction(error) {

  if(error.status == 401) {
    console.log("Authentication error");
    return;
  }
  else if(error.status == 404) {
    console.log("No Data Found");
  }
  else {
    console.log("ERROR");
  }
}

$(document).ready(function(){
  getTempHistory();
});

Was this article helpful?

Related Articles