使用 Intel® Edison 主板更新 Orbotix Sphero 以响应推文
本指南包含让您的 Orbotix Sphero 检查 Twitter 并根据使用标签 #intelmaker 发布的新推文更改其彩色灯光的步骤。
获取新的 Intel® IoT 开发者套件,这是一个完整的硬件和软件解决方案,允许开发人员使用 Intel® Galileo 和 Intel® Edison 开发板创建令人兴奋的新解决方案。访问 Intel® IoT 开发者专区。
本指南包含让您的 Orbotix Sphero* 检查 Twitter 并根据使用标签 #intelmaker 发布的新推文更改其彩色灯光的步骤。
要求
您必须拥有
- 一个 Sphero
- 一个 Twitter 帐户
- 一个已设置并连接到您的系统的 Intel® Edison 开发板。有关步骤,请参阅 连接到网络。
- 安装了 Intel® XDK IoT Edition 并将其连接到您的开发板。有关步骤,请参阅 开始使用 Intel® XDK IoT Edition。
本节包含在 Intel XDK IoT Edition 中创建一个新项目,并使用适当的代码来更新您的 Sphero 的步骤。
- 在 Intel XDK IoT Edition 中,单击 Projects(项目) 选项卡。单击 Start a New Project(开始一个新项目)。
- 在 Internet of Things Embedded Application 下,单击 Templates(模板)。
- 单击 Blank Template(空白模板),然后单击 Continue(继续)。
- 在 Project(项目) Name(名称) 字段中,键入您的项目的名称,然后单击 Create(创建)。
- 您的新项目将打开,并显示 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); }
- 从左侧,选择 package.json。在 dependencies(依赖项) 部分,确保为代码所需的每个模块添加一个条目
"dependencies": { "mtwitter":"latest", "colors":"latest", "moment":"latest", "spheron":"latest" }
本节包含创建新的 Twitter 应用程序并检索适当的密钥以允许您的代码与 Twitter 交互的步骤。
- 导航到 Twitter 应用程序网站 https://apps.twitter.com/ 并使用您的 Twitter 帐户登录。
- 单击 Create New App(创建新应用程序)。将打开“创建应用程序”页面。
- 在“创建应用程序”页面上,执行以下操作
- 在 Name(名称) 字段中键入您的名称。
- 在 Description(描述) 字段中键入您的应用程序的描述。
- 在 Website(网站) 字段中键入一个占位符 URL。
- 阅读使用条款和条件,然后选中 Yes, I agree(是的,我同意) 复选框。单击 Create your Twitter application(创建您的 Twitter 应用程序)。
- 单击 Keys and Access Tokens(密钥和访问令牌) 选项卡。
- 在“应用程序设置”部分,从 Consumer Key (API Key)(消费者密钥(API 密钥)) 字段复制并粘贴消费者密钥,并从 Consumer Secret (API Secret)(消费者密钥(API 密钥)) 字段复制消费者密钥到单独的文本编辑程序中,例如记事本。
- 在“您的访问令牌”部分,单击 Create my access token(创建我的访问令牌)。
- 从 Access Token(访问令牌) 字段复制并粘贴访问令牌密钥,并从 Access Token Secret(访问令牌密钥) 字段复制访问令牌密钥到您的文本编辑程序中。
- 在 Intel XDK IoT Edition 中,导航到以
consumer_key
开头的行。使用消费者密钥、消费者密钥、访问令牌和访问令牌密钥替换代码中的占位符文本。
本节包含使用 Bluetooth* 将您的开发板连接到 Sphero 的步骤。
- 在串行通信窗口中连接到您的开发板。
- 要激活蓝牙并扫描支持蓝牙的设备,请输入以下命令
rfkill unblock bluetooth
bluetoothctl
- 您的 Sphero 的 MAC 地址应显示在 Sphero 盒子的底部,格式为
XX-XX-XX-XX-XX-XX
。如果找不到 Sphero 的 MAC 地址,请执行以下操作- 键入命令:
scan on
- 在列表中找到您的 Sphero 并复制 MAC 地址。
- 键入命令:
scan off
- 键入命令:
- 通过输入以下命令将您的开发板与 Sphero 配对
pair SpheroMacAddress
exit
rfcomm bind 0 SpheroMacAddress 1
MAC 地址的格式应为XX:XX:XX:XX:XX:XX
。这将创建一个 Sphero 连接。
本节包含构建、上传和运行您的代码的步骤。完成这些步骤后,您的 Sphero 的颜色将根据推文而变化。
- 在底部工具栏中,单击 Upload(上传) 图标
将项目上传到您的开发板。
- 单击 Run(运行) 图标
运行您的项目。 您的 Sphero 每秒检查一次 Twitter,当有人使用标签 #intelmaker 发推文时,它会变成蓝色。