【问题标题】:Can I connect CoAPthon (CoAP of Python) to node-coap(CoAP for nodejs)?我可以将 CoAPthon(Python 的 CoAP)连接到 node-coap(用于 nodejs 的 CoAP)吗?
【发布时间】:2021-08-20 20:06:16
【问题描述】:

我正在开发一个运行 python 脚本的树莓派项目,我想将传感器数据从该脚本发送到我的 nodejs 服务器。我正在尝试将 python 脚本中的 CoAP 服务器连接到节点服务器上的客户端。使用的库是 python 中的 CoAPthon 和 nodejs 中的 node-coap。当我尝试连接 CoAPServer 时说编码错误,有什么解决方案吗??? 错误:“utf-8”编解码器无法解码位置 3 中的字节 0xbd:无效的起始字节”。
CoAPthon服务器的python版本如下。

#CoAPServer.py
from coapthon.server.coap import CoAP
from coapthon.resources.resource import Resource
gotin=1
class sensor(Resource):
    def __init__(self, name="sensor", coap_server=None):
        super(sensor, self).__init__(name, coap_server, visible=True,observable=True, allow_children=True)
        self.payload = str(gotin)
        self.content_type = "text/plain"
    def render_GET(self, request):
        return self
class CoAPServer(CoAP):
    def __init__(self, host, port):
        CoAP.__init__(self, (host, port))
        self.add_resource('/sensor', sensor())
def main():
    server = CoAPServer("0.0.0.0", 5682)
    try:
        server.listen(10)
    except KeyboardInterrupt:
        print("Server Shutdown")
        server.close()
if __name__ == '__main__':
    main()

nodejs CoAP客户端如下。

//app.js
const express = require("express");
const app = express();
const path = require("path");
const port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, "public")));
var coap = require('coap');
var packet = require('coap-packet');
var req = coap.request({
  host: '127.0.0.1',
  pathname: '/sensor',
  port: 5682,
  method: 'get'
})
var payload = {
  user : 'ash'
}
req.write(JSON.stringify(payload));
req.setOption('Content-Format','text/plain');
console.log(req);
req.on('response', function(res) {
  console.log('response code', res.code);
  res.pipe(process.stdout)
});
req.end();
app.listen(port, () => {
  console.log(`????????Listening on port : ${port}`);
});

得到的错误是

Traceback (most recent call last):
  File "E:\practice\raspberrypi\CoAPServer1.py", line 28, in <module>
    main()
  File "E:\practice\raspberrypi\CoAPServer1.py", line 23, in main
    server.listen(10)
  File "C:\Users\91948\AppData\Local\Programs\Python\Python39\lib\site-packages\coapthon\server\coap.py", line 153, in listen
    message = serializer.deserialize(data, client_address)
  File "C:\Users\91948\AppData\Local\Programs\Python\Python39\lib\site-packages\coapthon\serializer.py", line 57, in deserialize
    message.token = token_value.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 0: invalid start byte

【问题讨论】:

  • 尝试使用wireshark查看实际发送的内容。如果将 5683(标准)用于 coap,可能会更容易,尽管 wireshark 知道这一点。

标签: python node.js raspberry-pi3 iot coap


【解决方案1】:

这看起来像是 coapython 中的一个错误——没有理由假设一个标记必然代表 UTF-8 编码中的任何内容。请确认最新版本中仍然存在这种情况,如果是,请向 CoAPython 开发人员报告。

【讨论】:

    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多