使用Cylon.js和SocketIO发送自定义数据

Sending custom data using Cylon.js and SocketIO

本文关键字:自定义 数据 SocketIO Cylon js 使用      更新时间:2024-01-01

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

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

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

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>

我也遇到了同样的问题。根据cylon.js博客的说法,要接收机器人发出的事件,请连接到机器人,而不是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');

并且为了一致性将所有的CCD_ 1改变为CCD_。