【问题标题】:Sending custom data using Cylon.js and SocketIO使用 Cylon.js 和 SocketIO 发送自定义数据
【发布时间】:2015-06-18 04:58:25
【问题描述】:

修改了我的代码以遵循此处的示例

https://github.com/hybridgroup/cylon-api-socketio/tree/master/examples/robot_events_commands

这是我在爱迪生上运行的完整服务器代码。 一切正常,我的绊脚石是将任何自定义事件从服务器发送到监听客户端。

var Cylon = require('cylon');
Cylon.robot({
    name: 'chappie',
    connections: {
        edison: { adaptor: 'intel-iot' }//,
    },
    events: ['range'],

    commands: function () {
        return {
            send_range: this.sendRange    
        };
    },

    devices: {
        maxbotix: { driver: 'maxbotix', pin: '0' },
        led: { driver: 'led', pin: 13 }
    },

    work: function (my) {
        var range = 0;
        every((0.1).seconds(), function () {
            range = my.maxbotix.range();
            this.sendRange(range);
        }.bind(this));             
    },

    sendRange: function(data) {
        this.emit('range', { info: data });    
    }

})
Cylon.api(
    'socketio',
    {
        host: '0.0.0.0',
        port: '3000'
 });

Cylon.start();

这是我的客户,现在是一个简单的网页

<!doctype html>
<html>
<meta charset="utf-8">
<head>
    <title>Socket.IO chat</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font: 13px Helvetica, Arial;
        }

        form {
            background: #000;
            padding: 3px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }

            form input {
                border: 0;
                padding: 10px;
                width: 90%;
                margin-right: .5%;
            }

            form button {
                width: 9%;
                background: rgb(130, 224, 255);
                border: none;
                padding: 10px;
            }

        #messages {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }

            #messages li {
                padding: 5px 10px;
            }

                #messages li:nth-child(odd) {
                    background: #eee;
                }
    </style>
</head>
<script src='https://cdn.socket.io/socket.io-1.2.0.js'></script>

<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
    var device;

    window.onload = function() {
        device = io('http://<actual ip of edison>:3000/api/robots/chappie/devices/led');

      device.on('message', function(msg) {
        $('#messages').append($('<li>').text('Message from edison '+msg));
      });
      device.on('range', function (msg) {
          console.log('NOT SEEING THIS... Receiving range data from edison and value is '+msg.info);
      });

      msg = 'You have been subscribed to Cylon socket: ' + device.nsp;

      $('#messages').append($('<li>').text(msg));

      $('form').submit(function(){
        device.emit('message', $('#m').val());
        $('#m').val('');

        return false;
      });
    };
</script>
<body>
    <ul id="messages"></ul>
    <form action="">
        <input id="m" autocomplete="off" /><button>Send</button>
    </form>
</body>
</html>

【问题讨论】:

    标签: javascript node.js socket.io


    【解决方案1】:

    我遇到了同样的问题。根据cylon.js blog,接收机器人发出的事件连接到机器人而不是LED。

    所以在你的客户改变

    device = io('http://<actual ip of edison>:3000/api/robots/chappie/devices/led');
    

    robot = io('http://<actual ip of edison>:3000/api/robots/chappie');
    

    为了保持一致性,将所有 device.on( ...) 更改为 robot.on( ...)

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多