Jump to content









Photo

retrieving data from carriots and display in jsp


  • Please log in to reply
8 replies to this topic

#1 pramod2181

pramod2181

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 02 September 2014 - 10:19 AM

Hi,

I am new for IoT platforms and exploring carriots for the learning purpose, now I am facing a problem. I am sending some data like temperature or humidity using the wizard provided in the carriots control panel and i want to display some last 10 data in a separate JSP, for this purpose I have to fetch data from the carriots cloud. Please guide me on how to do this. 



#2 dompablo

dompablo

    Master of Puppets

  • Members
  • PipPip
  • 14 posts

Posted 05 September 2014 - 08:21 AM

Hi Pramod,

 

To fetch data from Carriots you need to use the carriots api: https://www.carriots...cumentation/api.  In this case the api for streams management https://www.carriots...agement#streams.

 

You can use any library to perform a GET request and retrieve your streams. A valid url to perform the operation you want would be:

  • https://api.carriots.com/streams/?sort=at&order=desc&max=10

Don't forget to include the carriots header (carriots.apikey) with your apikey. Maybe you can try first with something like http://www.hurl.it/ and then with the library.

 

Hope it helps!



#3 pramod2181

pramod2181

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 07 September 2014 - 11:50 AM

Hi Dompablo,

 

Thanks for your reply. This way I will be able to fetch data , but due to firewall and proxy settings in my organization I'm unable to make any request through REST. Also, when I'm using my personal system to make REST requests, I'm getting this error-

 

java.io.IOException: Server returned HTTP response code: 401 for URL: https://api.carriots.com/streams

 

So, presently I'm using CORS for getting the data from carriots, now there is another problem with CORS I am able to fetch the data for just a single device, when I specify two device ids, it does not pass any value. Please guide me to resolve both the problems.



#4 dompablo

dompablo

    Master of Puppets

  • Members
  • PipPip
  • 14 posts

Posted 08 September 2014 - 07:29 AM

Hi Pramod,

 

Regarding the 401 message, did you include the carriots header in your java request?

 

You can only specify one device for filtering, there are two ways:

 

https://api.carriots.com/devices/id_dev_device/streams/?sort=at&order=desc&max=10
https://api.carriots.com/streams/?sort=at&order=desc&max=10&device=id_dev_device

 

So if you want filter by two or more devices you need to perform several requests.

 

Hope it helps!



#5 pramod2181

pramod2181

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 09 September 2014 - 06:39 AM

Hi Dompablo,

 

Thanks for your prompt reply.

 

Yes, I'm including the required headers, i.e. the apikey and the content type with my request.

 

And as far as CORS request is concerned, I tried to make two different requests for each of the devices, but the value returned was always of the last specified device. I'm sending my code, so that you may help me in a better manner.

 

<script>


var currentDate= new Date;

var now= parseInt(currentDate.getTime()/1000);

var limit=1409900100;

makeCorsRequest('GET', "http://api.carriots.com/devices/TempSensor@pramod2181.pramod2181/streams/?at_to="+now+"&at_from="+limit);


// Create the XHR object.

function createCORSRequest(method, url) {

var xhr = new XMLHttpRequest();

if ("withCredentials" in xhr) {

xhr.open(method, url, true);

} else if (typeof XDomainRequest != "undefined") {

xhr = new XDomainRequest();

xhr.open(method, url);

} else {

// CORS not supported.

xhr = null;

}

return xhr;

}


// Make the actual CORS request.

function makeCorsRequest(meth, url) {

// Create the XHR object.

var xhr = createCORSRequest(meth, url);

var apikey= 'b52c5c1...254c';


if (!xhr) {

alert('CORS not supported');

return;

}



xhr.setRequestHeader('Host', 'api.carriots.com');

xhr.setRequestHeader('carriots.apiKey', apikey);

xhr.setRequestHeader('Accept', 'application/json');

xhr.setRequestHeader('User-Agent', 'Carriots-client');

xhr.setRequestHeader('Content-Type', 'application/json');


// Response handlers.

xhr.onload = function() {

var text = xhr.responseText;

if(meth!='POST'){

decode(text);

}

};


xhr.onerror = function() {

alert('There was an error making the request.');

};


xhr.send();

}


function decode(text){

// Parse the text returned from the request into a JSON object.

obj = JSON.parse(text);

var temp=obj.result[obj.result.length-1].data.temp;

var temp1=obj.result[obj.result.length-2].data.temp;


{

var table = document.getElementById("myTable");

var row = table.insertRow(0);

var cell1 = row.insertCell(0);

cell1.innerHTML="Temperature";

var row1 = table.insertRow(1);

var r1cell1 = row1.insertCell(0);

r1cell1.innerHTML=temp;

var row2 = table.insertRow(2);

var r2cell1 = row2.insertCell(0);

r2cell1.innerHTML=temp1;


}

}

</script>


<%--For Humidity--%>


<script>


var currentDate= new Date;

var now= parseInt(currentDate.getTime()/1000);

var limit=1409900100;

makeCorsRequest1('GET', "http://api.carriots.com/devices/Humidity@pramod2181.pramod2181/streams/?at_to="+now+"&at_from="+limit);


// Create the XHR object.

function createCORSRequest1(method, url) {

var xhr1 = new XMLHttpRequest();

if ("withCredentials" in xhr1) {

xhr1.open(method, url, true);

} else if (typeof XDomainRequest != "undefined") {

xhr1 = new XDomainRequest();

xhr1.open(method, url);

} else {

// CORS not supported.

xhr1 = null;

}

return xhr1;

}


// Make the actual CORS request.

function makeCorsRequest1(meth, url) {

var xhr1 = createCORSRequest1(meth, url);

var apikey= 'b52c5c1...254c';

if (!xhr1) {

alert('CORS not supported');

return;

}



xhr1.setRequestHeader('Host', 'api.carriots.com');

xhr1.setRequestHeader('carriots.apiKey', apikey);

xhr1.setRequestHeader('Accept', 'application/json');

xhr1.setRequestHeader('User-Agent', 'Carriots-client');

xhr1.setRequestHeader('Content-Type', 'application/json');


// Response handlers.

xhr1.onload = function() {

var text1 = xhr1.responseText;

if(meth!='POST'){

value=decode(text1);

}

};


xhr1.onerror = function() {

alert('There was an error making the request.');

};


xhr1.send();

}



function decode(text1){

obj1 = JSON.parse(text1);

var hum=obj1.result[obj1.result.length-1].data.Humidity;

var hum1=obj1.result[obj1.result.length-2].data.Humidity;


{

var table1 = document.getElementById("myTable1");

var row = table1.insertRow(0);

var cell1 = row.insertCell(0);

cell1.innerHTML="Humidity";

var row1 = table1.insertRow(1);

var r1cell1 = row1.insertCell(0);

r1cell1.innerHTML=hum;

var row2 = table1.insertRow(2);

var r2cell1 = row2.insertCell(0);

r2cell1.innerHTML=hum1;

}

}

</script>


<html>

<body>
<


table id="myTable">

</table>


<table id="myTable1">

</table>

<p>


</body>

</html>

Here, I'm displaying the latest two results for both the devices.

 

And I took the help of the code mentioned for making CORS request for carriots meteo. So, the above mentioned code is derived from that only.

 

Looking forward for your reply. Thanks in advance.


Edited by alvaro, 09 September 2014 - 08:39 AM.
Apikey masked


#6 dompablo

dompablo

    Master of Puppets

  • Members
  • PipPip
  • 14 posts

Posted 09 September 2014 - 08:31 AM

Hi Pramod, your have two functions named 'decode' in your code.

 

Try this version:

 

<script>

// Create the XHR object.

function createCORSRequest(method, url) {

    var xhr = new XMLHttpRequest();

    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        // CORS not supported.
        xhr = null;
    }

    return xhr;
}


// Make the actual CORS request.
function makeCorsRequest(meth, url, device_type) {

    // Create the XHR object.

    var xhr = createCORSRequest(meth, url);
    var apikey= 'YOUT-APIKEY-HERE';

	if (!xhr) {
	    alert('CORS not supported');
	    return;
    }

	xhr.setRequestHeader('Host', 'api.carriots.com');
	xhr.setRequestHeader('carriots.apikey', apikey);
	xhr.setRequestHeader('Accept', 'application/json');
	xhr.setRequestHeader('User-Agent', 'Carriots-client');

    // Response handlers.

    xhr.onload = function() {
	    var text = xhr.responseText;
		if (meth!='POST') {
		    if ("temp" == device_type){
			 decodeTemp(text);
		    } else if ("hum" == device_type) {
			 decodeHum(text);
			}
		}
	};

	xhr.onerror = function() {
		   alert('There was an error making the request.');
	};

	xhr.send();
}


</script>


<%--For Humidity--%>


<script>

var currentDate= new Date;
var now= parseInt(currentDate.getTime()/1000);
var limit=1409900100;

makeCorsRequest('GET', "http://api.carriots.com/devices/TempSensor@pramod2181.pramod2181/streams/?at_to="+now+"&at_from="+limit, "temp");
makeCorsRequest('GET', "http://api.carriots.com/devices/Humidity@pramod2181.pramod2181/streams/?at_to="+now+"&at_from="+limit, "hum");

function decodeTemp(text) {
    // Parse the text returned from the request into a JSON object.
    obj = JSON.parse(text);
    var temp=obj.result[obj.result.length-1].data.temp;
    var temp1=obj.result[obj.result.length-2].data.temp;
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    cell1.innerHTML="Temperature";
    var row1 = table.insertRow(1);
    var r1cell1 = row1.insertCell(0);
    r1cell1.innerHTML=temp;
    var row2 = table.insertRow(2);
    var r2cell1 = row2.insertCell(0);
    r2cell1.innerHTML=temp1;
}

function decodeHum(text1) {
	// Parse the text returned from the request into a JSON object.
    obj1 = JSON.parse(text1);
    var hum=obj1.result[obj1.result.length-1].data.Humidity;
    var hum1=obj1.result[obj1.result.length-2].data.Humidity;
    var table1 = document.getElementById("myTable1");
    var row = table1.insertRow(0);
    var cell1 = row.insertCell(0);
    cell1.innerHTML="Humidity";
    var row1 = table1.insertRow(1);
    var r1cell1 = row1.insertCell(0);
    r1cell1.innerHTML=hum;
    var row2 = table1.insertRow(2);
    var r2cell1 = row2.insertCell(0);
    r2cell1.innerHTML=hum1;
}

</script>
<html>
<body>
<table id="myTable">
</table>
<table id="myTable1">
</table>
<p>
</body>
</html>

 



#7 pramod2181

pramod2181

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 09 September 2014 - 10:13 AM

Hi Dompablo,

 

It worked...thanks a lot for your prompt reply... :)



#8 pramod2181

pramod2181

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 17 September 2014 - 10:27 AM

Hi Dompablo,

 

I have one more query...is there any restriction on the number of requests made through CORS, because today I tried with a continuous stream of data from one of the sensor, but after sometime the values in the table which was displayed in JSP didn't get updated, while the values in the carriots portal were...also when I changed the device, the values started getting updated in the table.    



#9 dompablo

dompablo

    Master of Puppets

  • Members
  • PipPip
  • 14 posts

Posted 23 September 2014 - 09:33 AM

Hi Pramod,
 
There is not a specific restricition for CORS requests and the restrictions are per user, not per device.
 
Does it happen to you every day?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users