简体   繁体   English

Nodejs CoAP服务器未接收有效负载

[英]Nodejs CoAP Server not receiving payload

I just created CoAP Client using Arduino and am able to send payload("hello") to the coap browser(installed from mozilla browser). 我刚刚使用Arduino创建了CoAP客户端,并且能够将有效负载(“hello”)发送到coap浏览器(从mozilla浏览器安装)。 I can see it in browser, it receives. 我可以在浏览器中看到它,它收到了。

Now I need to create my own |Nodejs server to receive my payload (say "hello") from Arduino client. 现在我需要创建自己的| Nodejs服务器以从Arduino客户端接收我的有效负载(比如“hello”)。 How can I achieve it ? 我怎样才能实现它?

server 服务器

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 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();
}

On the Arduino, you should implement a coap server in firmware (not in Node). 在Arduino上,您应该在固件中实现一个coap服务器(不在Node中)。

Are you using this library? 你在用这个图书馆吗? If so then you should check out especially this example which reads from the ethernet peripheral, parses the packet, and then responds accordingly. 如果是这样,那么你应该特别检查这个从以太网外设读取的示例 ,解析数据包,然后做出相应的响应。

Pseudocode: 伪代码:

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)

There's another example server with 2 endpoints here . 有2个端点的另一个例子服务器在这里

The important parts are: 重要的部分是:

Inside void setup() : 内部void setup()

coap.add_resource(&aSensor);
coap.add_resource(&bSensor);

And then inside void loop() : 然后在void loop()里面:

coap.handler();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM