如何将Arduino传感器数据发送至Odoo?连接端口配置存疑
Arduino 数据发送到 Odoo 的问题修正
核心问题与修复步骤
1. 端口与HTTPS连接错误
- Odoo的XML-RPC/REST API默认端口:HTTP用80,HTTPS用443,你写的4404完全不符合规范。
- 你的Odoo地址是HTTPS协议,
EthernetClient不支持SSL加密,必须改用EthernetClientSecure库才能建立HTTPS连接,否则无法和服务器通信。
2. 服务器地址与Host头配置不匹配
- 代码里
server的IP(198.11.11.11)和HOST_NAME(xyz-test200.odoo.com)不一致,直接用域名连接更可靠,避免IP变动带来的问题。 Host请求头必须设置为你的Odoo域名xyz-test200.odoo.com,而不是192.168.1.2:8169。
3. 请求路径与Post数据格式错误
- 你提到Python用REST API正常,那就要使用Odoo的REST API路径(比如
/jsonrpc或对应业务模型的API端点),别用/xmlrpc/common——这个路径是用于身份认证的,不是提交数据的。 - PostData不能直接拼接URL,REST API要求发送JSON格式的请求体,比如向指定模型提交数据的JSON示例:
{ "jsonrpc": "2.0", "method": "call", "params": { "model": "你的业务模型名称", "method": "create", "args": [[{"x_1": 5.00, "x_2": "myfvrtstring"}]], "kwargs": {} }, "id": 1 }
修正后的简化代码
#include <SPI.h> #include <Ethernet.h> #include <EthernetClientSecure.h> byte mac[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; IPAddress ip(192, 11, 11, 11); const char* host = "xyz-test200.odoo.com"; const int httpsPort = 443; EthernetClientSecure client; // 预先通过Python或API获取的Odoo认证Token String authToken = "你的认证Token"; // JSON格式的请求体 String postData = "{\"jsonrpc\":\"2.0\",\"method\":\"call\",\"params\":{\"model\":\"你的业务模型名称\",\"method\":\"create\",\"args\":[[{\"x_1\":5.00,\"x_2\":\"myfvrtstring\"}]],\"kwargs\":{}},\"id\":1}"; void setup() { Serial.begin(9600); while (!Serial); Ethernet.begin(mac, ip); delay(1000); Serial.println("连接Odoo中..."); // 测试阶段可临时跳过证书验证,生产环境请勿使用 client.setInsecure(); if (client.connect(host, httpsPort)) { Serial.println("连接成功"); // 发送POST请求 client.println("POST /jsonrpc HTTP/1.1"); client.println("Host: xyz-test200.odoo.com"); client.println("Authorization: Bearer " + authToken); client.println("Content-Type: application/json"); client.println("Connection: close"); client.print("Content-Length: "); client.println(postData.length()); client.println(); client.println(postData); } else { Serial.println("连接失败"); } } void loop() { // 读取服务器返回内容并打印 while (client.available()) { char c = client.read(); Serial.print(c); } // 断开连接后终止程序 if (!client.connected()) { Serial.println("\n已断开连接"); client.stop(); while(1); } }
额外注意事项
- 必须先通过Odoo的REST API完成身份认证,获取Bearer Token——可以先用Python脚本获取,也可以在Arduino中添加认证请求逻辑。
- 生产环境下,
EthernetClientSecure需要配置正确的SSL证书,不能用client.setInsecure()跳过验证。 - 确保Arduino设备能访问公网,检查路由器防火墙、网络权限等配置。
内容的提问来源于stack exchange,提问作者Abdullah Raihan Bhuiyan
相关产品推荐
相关产品推荐

