Amazon Iot物联网终端之十四 基于javascript的AWS SDK物联网终端项目
时间:10-02
整理:3721RD
点击:
1. AWS基于amazon云服务的物联网开发有多种开发平台和实现方式,包括android, C++,embedded C, node.js, java, javascript, go, swift, iOS,python等多种方式,也配套提供了多种SDK。本项目基于myir的开发板,基于嵌入式实现是最适合的。结合以前的项目,提供了基于javascript和嵌入式c语音的两种实现方式作为对比。在此先提供javascript实现的范例和说明。
2. javascript的实现,基于browser实现是最为便捷的,因此,需要一个index.html文件作为入口,在head部分,引入各种需要的.js文件,针对页面提供对于传感器信号的采集和显示,并可以把数据写入云端和导出,具体的aws设置已经在上文完成,本部分就是提供如何使用这些功能。
3. 入口index.html文件如下。

源码如下:
HEXIWEAR sensors AWS
@import 'ui/css/evothings-app.css';
#connectButton {
width: 100%;
float: left;
}
#disconnectButton {
width: 100%;
float: right;
}
// Redirect console.log to Evothings Workbench.
if (window.hyper && window.hyper.log) { console.log = hyper.log }
HEXIWEAR sensors AWS
Not initialized
Connect
Disconnect
Write to AWS
Read from AWS
AWS status: Idle.
AWS value: No value read yet.
解析:
上面的4个.js文件,分别是aws的sdk文件,以及本地的config配置文件,以及用户定制的app.js实现对于aws的写入和导出。
4. aws-config.js解析
evothings.aws = evothings.aws || {}
// TODO: Fill in your AWS information here.
evothings.aws.config = {
accessKeyId: 'AKIAJ3JJ5WDFKXBF7HXQ',
secretAccessKey: 'pmKmDMzJt+lqXnQ+w+PyLOu1atisTlzvYW5p2txU',
region: 'eu-west-1',
params: { FunctionName: 'IOE2015-IoTAPI-1RG2VYH76BP8G' }
}
上面的配置是IoT2015的配置,其中accessKeyId指向当前配置的l应用编号,secretAccessKey是访问授权秘钥,region: 'eu-west-1'是选定的开发区域欧洲一区,爱尔兰地球,params是lambda函数的名称。
5. app.js解析
主要的程序行如下:
app.awsKey = 'hexiwear-accel';
app.writeValue = function() {
document.getElementById('aws-status').innerHTML = "Writing...";
evothings.aws.update(app.awsKey, app.value,
function() {
console.log(app.awsKey+" written.");
document.getElementById('aws-status').innerHTML = "Write success.";
},
function(error) {
console.log(app.awsKey+" write error: "+error);
document.getElementById('aws-status').innerHTML = "Write error: "+error;
});
}
app.readValue = function() {
document.getElementById('aws-status').innerHTML = "Reading...";
evothings.aws.query(app.awsKey,
function(items) {
var string = items[0].Value;
console.log(app.awsKey+" read: "+string);
document.getElementById('aws-status').innerHTML = "Read success.";
document.getElementById('aws-value').innerHTML = string;
},
function(error) {
console.log(app.awsKey+" read error: "+error);
document.getElementById('aws-status').innerHTML = "Read error: "+error;
});
}
以上分别实现写入和读出云端数据的功能。
6. 本项目在browser平台经测试成功,不过在myir开发板上实现,还需要一个可以提供brower的运行平台。随板提供的myir.rootfs看来还是精简到没有这样的功能。所以,还需要定制一个rootfs。
2. javascript的实现,基于browser实现是最为便捷的,因此,需要一个index.html文件作为入口,在head部分,引入各种需要的.js文件,针对页面提供对于传感器信号的采集和显示,并可以把数据写入云端和导出,具体的aws设置已经在上文完成,本部分就是提供如何使用这些功能。
3. 入口index.html文件如下。

源码如下:
HEXIWEAR sensors AWS
@import 'ui/css/evothings-app.css';
#connectButton {
width: 100%;
float: left;
}
#disconnectButton {
width: 100%;
float: right;
}
// Redirect console.log to Evothings Workbench.
if (window.hyper && window.hyper.log) { console.log = hyper.log }
HEXIWEAR sensors AWS
Not initialized
Connect
Disconnect
Write to AWS
Read from AWS
AWS status: Idle.
AWS value: No value read yet.
解析:
上面的4个.js文件,分别是aws的sdk文件,以及本地的config配置文件,以及用户定制的app.js实现对于aws的写入和导出。
4. aws-config.js解析
evothings.aws = evothings.aws || {}
// TODO: Fill in your AWS information here.
evothings.aws.config = {
accessKeyId: 'AKIAJ3JJ5WDFKXBF7HXQ',
secretAccessKey: 'pmKmDMzJt+lqXnQ+w+PyLOu1atisTlzvYW5p2txU',
region: 'eu-west-1',
params: { FunctionName: 'IOE2015-IoTAPI-1RG2VYH76BP8G' }
}
上面的配置是IoT2015的配置,其中accessKeyId指向当前配置的l应用编号,secretAccessKey是访问授权秘钥,region: 'eu-west-1'是选定的开发区域欧洲一区,爱尔兰地球,params是lambda函数的名称。
5. app.js解析
主要的程序行如下:
app.awsKey = 'hexiwear-accel';
app.writeValue = function() {
document.getElementById('aws-status').innerHTML = "Writing...";
evothings.aws.update(app.awsKey, app.value,
function() {
console.log(app.awsKey+" written.");
document.getElementById('aws-status').innerHTML = "Write success.";
},
function(error) {
console.log(app.awsKey+" write error: "+error);
document.getElementById('aws-status').innerHTML = "Write error: "+error;
});
}
app.readValue = function() {
document.getElementById('aws-status').innerHTML = "Reading...";
evothings.aws.query(app.awsKey,
function(items) {
var string = items[0].Value;
console.log(app.awsKey+" read: "+string);
document.getElementById('aws-status').innerHTML = "Read success.";
document.getElementById('aws-value').innerHTML = string;
},
function(error) {
console.log(app.awsKey+" read error: "+error);
document.getElementById('aws-status').innerHTML = "Read error: "+error;
});
}
以上分别实现写入和读出云端数据的功能。
6. 本项目在browser平台经测试成功,不过在myir开发板上实现,还需要一个可以提供brower的运行平台。随板提供的myir.rootfs看来还是精简到没有这样的功能。所以,还需要定制一个rootfs。
学习学习
学习学习。
