Copy var TOKEN = ubidots.token;
var VARIABLE = 'variable-id';
function makeHttpRequest(url, method, headers, body) {
return new Promise(function(resolve, reject) {
fetch(url, {
method: method,
headers: headers,
body: body
})
.then(function(response) {
if (response.ok) {
return response.json();
} else {
throw new Error('Error making HTTP request');
}
})
.then(function(data) {
resolve(data);
})
.catch(function(error) {
reject(error);
});
});
}
async function getDataFromVariable(variable, token, callback) {
var url = 'https://industrial.api.ubidots.com/api/v1.6/variables/' + variable + '/values';
var headers = {
'X-Auth-Token': token,
'Content-Type': 'application/json'
};
makeHttpRequest(url, 'GET', headers)
.then(function(res) {
callback(res.results);
})
.catch(function(error) {
console.error(error);
});
}
var chart = Highcharts.chart('container', {
chart: {
type: 'line'
},
title: {
text: 'Bring data from Ubidots'
},
xAxis: {
type: 'datetime',
},
credits: {
enabled: false
},
series: [{
data: []
}]
});
getDataFromVariable(VARIABLE, TOKEN, function(values) {
var data = values.map(function(value) {
return [value.timestamp, value.value];
});
chart.series[0].setData(data);
});
async function makeRequest(url, method, token, payload) {
var headers = {
'X-Auth-Token': token,
'Content-Type': 'application/json'
};
var body = null;
if (payload) {
body = JSON.stringify(payload);
}
return makeHttpRequest(url, method, headers, body);
}
async function sendDataToDevice(sendDataDeviceLabel, token, payload) {
var url = 'https://industrial.api.ubidots.com/api/v1.6/devices/' + sendDataDeviceLabel + '/';
var method = 'POST';
return makeRequest(url, method, token, payload);
}
async function getDeviceVariables(sendDataDeviceID, token) {
var url = 'https://industrial.ubidots.com/api/v2.0/devices/' + sendDataDeviceID + '/variables';
var method = 'GET';
return makeRequest(url, method, token);
}
async function getDashboardDevices(sendDataDashboardID, ubidotsToken) {
var url = 'https://industrial.ubidots.com/api/-/dashboards/' + sendDataDashboardID + '/devices?sort_by=name&page_size=500';
var method = 'GET';
return makeRequest(url, method, ubidotsToken);
}
async function getAllDashboards(ubidotsToken) {
var url = 'https://industrial.api.ubidots.com/api/v2.0/dashboards/';
var method = 'GET';
return makeRequest(url, method, ubidotsToken);
}
async function getAllDevices(token) {
var url = 'https://industrial.api.ubidots.com/api/v2.0/devices/';
var method = 'GET';
return makeRequest(url, method, token);
}