我刚刚使用Arduino创建了CoAP客户机,并且能够向coap浏览器(从mozilla安装)发送有效负载(“hello”)。我可以在浏览器里看到它,它收到了。
现在,我需要创建自己的\Nodejs服务器,以便从Arduino客户端接收我的有效负载(说“你好”)。我怎样才能做到这一点?
服务器
var coap = require('coap');
var server = coap.createServer();
// At this point, I checked from mozilla coap browser, sent "hello" and I am able to receive the HEX values. Same way, I tried to send it from Arduino, but did not print anything
server.on('request', function(req, res) {
console.log(req.payload);
})
// the default CoAP port is 5683
server.listen(function() {
var req = coap.request('coap://localhost');
console.log('Listening on : 5683')
req.on('response', function(res) {
res.pipe(process.stdout);
});
req.end()
});arduino
void loop() {
// send GET or PUT coap request to CoAP server.
// To test, use libcoap, microcoap server...etc
int msgid = coap.put(IPAddress(192,168,0,11), 5683, "light","1");
Serial.println("Send Request");
//int msgid1 = coap.get(IPAddress(192, 168, 0, 11), 5683, "time");
delay(1000);
coap.loop();
}发布于 2017-03-01 19:49:42
在Arduino上,您应该在固件(而不是节点)中实现coap服务器。
你在使用这库吗?如果是这样的话,那么您应该特别检查这个例子,它从以太网外围设备读取,解析数据包,然后相应地作出响应。
伪码:
udp.setup()
while true
p = udp.read()
if(p == SUCCESSFUL_UDP_READ_FLAG)
parsed = p.parse()
if(parsed matches some condition)
udp.respond(message regarding condition)还有另一个带有两个端点这里的示例服务器。
重要的部分是:
内部void setup()
coap.add_resource(&aSensor);
coap.add_resource(&bSensor);然后在void loop()内部
coap.handler();https://stackoverflow.com/questions/41862202
复制相似问题