65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Intel® Edison 主板更新 Orbotix Sphero 以响应推文

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2015年5月8日

CPOL

4分钟阅读

viewsIcon

6727

本指南包含有关如何让您的 Orbotix Sphero* 检查 Twitter 并根据使用 #intelmaker 标签发布的新推文更改其颜色灯光的操作步骤。

获取新的 Intel® 物联网开发者套件,这是一个完整的硬件和软件解决方案,使开发人员能够使用 Intel® Galileo 和 Intel® Edison 板创建令人兴奋的新解决方案。请访问 Intel® 物联网开发者中心

要求

您必须拥有

本节包含在 Intel XDK IoT Edition 中创建新项目的步骤,并附有更新您的 Sphero 的适当代码。

  1. 在 Intel XDK IoT Edition 中,点击 项目 选项卡。点击 开始一个新项目

  2. 在 Internet of Things Embedded Application 下,单击 Templates(模板)。

  3. 项目 名称 字段中,输入您的项目名称,然后点击 创建

  4. 您的新项目将打开,并显示 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);
     }
  5. 从左侧选择 package.json。在 dependencies 部分,请确保为代码所需的每个模块添加一个条目
            "dependencies": {
      
                    "mtwitter":"latest",
                    "colors":"latest",
                    "moment":"latest",
                    "spheron":"latest"
            }

本节包含创建新的 Twitter 应用并检索适当密钥以允许您的代码与 Twitter 交互的步骤。

  1. 导航到 Twitter Apps 网站 https://apps.twitter.com/,并使用您的 Twitter 账户登录。
  2. 点击 创建新应用。将打开“创建应用程序”页面。

  3. 在“创建应用程序”页面上,执行以下操作
    1. 名称 字段中输入您的名字。
    2. 描述 字段中输入您的应用程序的描述。
    3. 网站 字段中输入一个占位符 URL。
  4. 阅读使用条款和条件,然后选择 是,我同意 复选框。点击 创建您的 Twitter 应用程序

  5. 点击 密钥和访问令牌 选项卡。

  6. 在“应用程序设置”部分,将 消费者密钥 (API 密钥) 字段中的消费者密钥和 消费者密钥 (API 密钥) 字段中的消费者密钥复制并粘贴到单独的文本编辑程序(如记事本)中。

  7. 在“您的访问令牌”部分,点击 创建我的访问令牌

  8. 访问令牌 字段中的访问令牌密钥和 访问令牌密钥 字段中的访问令牌密钥复制并粘贴到您的文本编辑程序中。

  9. 在 Intel XDK IoT Edition 中,导航到以 consumer_key 开头的行。将代码中的占位符文本替换为消费者密钥、消费者密钥、访问令牌和访问令牌密钥。

本节包含使用蓝牙*将开发板连接到 Sphero 的步骤。

  1. 在串行通信窗口中连接到您的开发板。
  2. 要激活蓝牙并扫描启用了蓝牙的设备,请输入以下命令
    rfkill unblock bluetooth
    bluetoothctl

     
  3. 您的 Sphero 的 MAC 地址应显示在 Sphero 包装盒底部,格式为 XX-XX-XX-XX-XX-XX。如果找不到 Sphero 的 MAC 地址,请执行以下操作
    1. 键入命令:
      scan on
    2. 在列表中找到您的 Sphero 并复制 MAC 地址。
    3. 键入命令:
      scan off
  4. 通过输入以下命令将您的开发板与 Sphero 配对
    pair SpheroMacAddress
    exit
    rfcomm bind 0 SpheroMacAddress 1

    MAC 地址的格式应为 XX:XX:XX:XX:XX:XX。这将创建一个 Sphero 连接。

本节包含构建、上传和运行代码的步骤。完成这些步骤后,您的 Sphero 的颜色将根据推文而改变。

  1. 在 Intel XDK IoT Edition 中,点击 安装/构建 图标 。如果系统提示您选择上传还是构建项目,请点击 构建
  2. 项目构建完成后,将显示“构建完成”消息。点击 上传 图标 将项目上传到您的开发板。
  3. 点击 运行 图标 来运行您的项目。您的 Sphero 会每秒检查一次 Twitter,并在有人使用 #intelmaker 标签发推文时变为蓝色。

后续步骤

入门主页

故障排除

通过论坛寻求帮助

资源

Intel® 物联网开发者中心

立即开始创新!Intel® 物联网开发者计划提供知识、工具、套件以及专家社区,助您快速轻松地将您的创新想法转化为物联网解决方案。

梦想一下,用适用于 Intel® Edison 和 Intel® Galileo 平台的 Intel® 物联网开发者套件来构建它。这些套件是多功能、性能优化且完全集成的端到端物联网解决方案,支持各种编程环境、工具、安全、云连接和硬件。

如需更多资源并了解新的 Intel® 物联网开发者套件 v1.0 如何帮助您简化物联网项目

© . All rights reserved.