修改我的代码以遵循此处的示例
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>
答案 0 :(得分:0)
我遇到了同样的问题。根据{{3}},接收机器人发出的事件连接到机器人而不是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( ...)
。