本指南将介绍如何支持 Orbotix Sphero* 查看 Twitter,并更改其灯光颜色,以响应使用 hashtag #intelmaker 发布的新推文。
要求
您必须有:
本部分将介绍如何在英特尔 XDK IoT Edition 中创建新项目,并使用相应代码更新您的 Sphero。
1、在英特尔 XDK IoT Edition 中点击“Projects”选项卡。点击“Start a New Project”。

2、在“Internet of Things Embedded Application”下方点击“Templates”。

3、点击“Blank Template”,然后点击“Continue”。
4、在“Project Name”字段中输入您的项目名称,然后点击“Create”。

5、新项目将打开,并显示 main.js 文件。删除 main.js 的内容,并将以下代码粘贴至文件:
var twitter = require('mtwitter');
var colors = require('colors');
var moment = require('moment');
var spheron = require('spheron');
var sphero = spheron.sphero();
var spheroPort = '/dev/rfcomm0';
var COLORS = spheron.toolbelt.COLORS;
var newMessage = false;
var lastTweet = '21:35:5';
var twit = new twitter({
consumer_key : 'xxxxxxxxxxxxxxxxxxxxx',
consumer_secret : 'xxxxxxxxxxxxxxxxxxxxx',
access_token_key : 'xxxxxxxxxxxxxxxxxxxxx',
access_token_secret : 'xxxxxxxxxxxxxxxxxxxxx'
});
console.log('Starting'.cyan);
setInterval(function() {
twit.get('search/tweets', {q: '#intelmaker'}, function(err, item) {
console.log(item.statuses[0].created_at.substring(11, 18).cyan)
console.log(lastTweet);
console.log("From isNew(): ", newMessage);
if(item.statuses[0].created_at.substring(11, 18) === lastTweet) {
console.log("we here");
newMessage = false;
}
else{
newMessage = true
updateSphero(true);
lastTweet = item.statuses[0].created_at.substring(11, 18);
}
});
}, 1000);
function updateSphero(flag)
{
console.log('updating sphero');
sphero.on('open', function() {
sphero.setRGB(COLORS.RED, false);
setTimeout(function(){sphero.setRGB(COLORS.BLUE, false);}, 2000);
});
sphero.open(spheroPort);
}
6、在左侧选择“package.json”。在相关性部分,请务必根据代码要求为每个模块添加一个条目:
"dependencies": {
"mtwitter":"latest",
"colors":"latest",
"moment":"latest",
"spheron":"latest"
}

本部分将介绍如何创建新的 Twitter 应用,并检索相应的密钥以支持代码与 Twitter 进行交互。
1、导航至 https://apps.twitter.com/ 的 Twitter Apps 站点,并使用 Twitter 帐号登录。
2、点击“Create New App”。“Create an application”页面打开。

3、在“Create an application”页面中进行以下操作:
a. 在“Name”字段中输入您的姓名。
b. 在“Description”字段中输入应用描述。
c. 在“Website”字段中输入占位符 URL。

4、阅读使用条款与条件,然后选中“Yes, I agree”复选框。点击“Create your Twitter application”。

5、点击“Keys and Access Tokens”选项卡。

6、在“Application Settings”部分,将“Consumer Key (API Key)”字段中的用户密钥和“Consumer Secret (API Secret)”字段中的用户机密分别复制粘贴至文本编辑程序(比如 Notepad)。

7、在“Your Access Token”部分点击“Create my access token”。

8、将“Access Token”字段中的访问令牌密钥和“Access Token Secret”字段中的访问令牌机密分别复制粘贴至文本编辑程序。

9、在英特尔 XDK IoT Edition 中导航至以“consumer_key”开头的行。将代码中的占位符文本替换为用户密钥、用户机密、访问令牌密钥和访问令牌机密。

本部分将介绍如何使用蓝牙* 连接开发板和 Sphero。
1、在串行通信窗口中连接开发板。
2、如要激活蓝牙并扫描支持蓝牙的设备,请输入以下命令:
rfkill unblock bluetooth
bluetoothctl
3、Sphero 的 MAC 地址应以XX-XX-XX-XX-XX-XX 的格式显示在 Sphero 文本框底端。如果无法找到 Sphero 的 MAC 地址,请进行下列操作:
a. 输入命令:
scan on
b. 在列表中找到 Sphero,并复制 MAC 地址。
c. 输入命令:
scan off
4、输入以下命令,以配对开发板和 Sphero:
pair SpheroMacAddress
exit
rfcomm bind 0 SpheroMacAddress 1
The MAC address should be in the format XX:XX:XX:XX:XX:XX.这样将创建 Sphero 连接。
本部分将介绍如何构建、上传和运行代码。这些步骤完成后,Sphero 的颜色将会根据推文发生变化。
1、在英特尔 XDK IoT Edition 中点击“Install/Build”图标
。如果提示选择上传或创建您的项目,请点击“Build”。
2、项目创建完成后,显示构建完成消息。点击“Upload”图标
,将项目上传至您的开发板。
3、点击“Run”图标
运行项目。Sphero 将每秒查看一次推文,如果有人使用 hashtag #intelmaker 发布了推文,Sphero 将变成蓝色。
文章来源:英特尔开发人员专区