如何在此处的代码中添加另一个Cloudbit?我试过运行这些代码,但它仍然无法运行。或者是我的cloudbit有问题吗?
JS代码
var authToken = "d31ad307a04a7e545e4a6ebdcf95ae2ce670aba107f5e2f7ef50df8e6a32188e";
var deviceIdA = "00e04c034276";
var deviceIdB= "00e04c037cdd";
setInterval(checkInputA,1000);
setInterval(checkInputB,1000);
function activateOutputA(percentValue,durationMs)
{
//call the function postOutput to post output
postOutputA(percentValue,durationMs);
}
function activateOutputB(percentValue,durationMs)
{
//call the function postOutput to post output
postOutputB(percentValue,durationMs);
}
function start()
{
//call the functions getInput
getInputA();
getInputB();
}
function checkInputA()
{
if(percentA>50)
{
activateOutputA(100,3000);
}
}
function checkInputB()
{
if(percentB>50)
{
activateOutputB(100,3000);
}
}
HTTP代码
var percentA=0;
var percentB=0;
function postOutputA(percentValue,durationMs) {
var req = new XMLHttpRequest();
req.open("POST", "https://api-http.littlebitscloud.cc/devices/"+deviceIdA+"/output", true);
req.setRequestHeader("Authorization","Bearer "+authToken);
req.setRequestHeader("Accept","application/vnd.littlebits.v2+json");
req.setRequestHeader("Content-Type","application/json");
var params = JSON.stringify({percentA: percentValueA, duration_msA: durationMsA});
req.send(params);
}
function postOutputB(percentValue,durationMs) {
var req = new XMLHttpRequest();
req.open("POST", "https://api-http.littlebitscloud.cc/devices/"+deviceIdB+"/output", true);
req.setRequestHeader("Authorization","Bearer "+authToken);
req.setRequestHeader("Accept","application/vnd.littlebits.v2+json");
req.setRequestHeader("Content-Type","application/json");
var params = JSON.stringify({percentB: percentValueB, duration_msB: durationMsB});
req.send(params);
}
function getInputA() {
var req = new XMLHttpRequest();
req.onprogress = function(progress) {
var splittedData = progress.target.responseText.split("data:");
var splitLength = splittedData.length;
var latestResponse = splittedData[splitLength-1];
var jsonObj = JSON.parse(latestResponse);
percentA = jsonObj.payload.percent;
}
req.open("GET", "https://api-http.littlebitscloud.cc/devices/"+deviceIdA+"/input", true);
req.setRequestHeader("Authorization","Bearer "+authToken);
req.setRequestHeader("Accept","application/vnd.littlebits.v2+json");
req.setRequestHeader("Content-Type","application/json");
req.send();
}
function getInputB() {
var req = new XMLHttpRequest();
req.onprogress = function(progress) {
var splittedData = progress.target.responseText.split("data:");
var splitLength = splittedData.length;
var latestResponse = splittedData[splitLength-1];
var jsonObj = JSON.parse(latestResponse);
percentB = jsonObj.payload.percent;
}
req.open("GET", "https://api-http.littlebitscloud.cc/devices/"+deviceIdB+"/input", true);
req.setRequestHeader("Authorization","Bearer "+authToken);
req.setRequestHeader("Accept","application/vnd.littlebits.v2+json");
req.setRequestHeader("Content-Type","application/json");
req.send();
}
PS:我是一名学习这些代码的新生。也是StackOverFlow的新手。遗憾。