【发布时间】:2022-01-07 18:42:14
【问题描述】:
我无法完全掌握这个谜题的最后一步。一切都可以编译,并且“没有错误”。这是我第一次涉足通信/全栈,尽管有很多优秀的教程,我还是被难住了。
-
[WORKING] Arduino 读取和解释传感器数据
-
[WORKING] index.js 通过 USB 串行通信获取数据
-
[WORKING] index.js 使用 nodejs 创建一个 WebSocket 连接
-
[WORKING] index.html 执行 WebSocket 握手
-
[WORKING] index.html 使用 Plotly 创建实时折线图
-
[WIP] index.html 在 Plotly 函数中传递 Arduino 数据
砍掉 index.html:
<script src="server/plotly.min.js"></script>
<script>
//connection to the web socket server
const ws = new WebSocket("ws://localhost:5000");
let foo = 0.0;
//working
ws.addEventListener("open", () => {
console.log("We Are Connected");
ws.send("TestData");
});
//working
ws.addEventListener("message", e => {
console.log(e);
console.log("Data Recieved! Success.");
});
</script>
文件的其余部分只是我想通过 Arduino 数据传递的图形函数。
index.js
const WebSocket = require("ws");
const wss = new WebSocket.Server({ port: 5000 });
//create a serial port that allows serial connection from Arduino
let SerialPort = require("serialport");
let port = new SerialPort('COM4', { baudRate: 9600 });
let Readline = require("@serialport/parser-readline");
let parser = port.pipe(new Readline({ delimiter: '\n' }));
wss.on("connection", ws => {
//working
console.log("New Client Connection");
//this is what I need to passthrough my Plotly arg
parser.on("data", data => {
//event is firing but can't get client to grab this. Console logs data correctly.
console.log(RPM: ${data});
});
//working on both ends
ws.on("message", data => {
console.log("TEST")
ws.send(data);
});
//doesn't log?
port.on("open", () => {
console.log("Serial Port Open");
});
});
//working
console.log("The server is ON");
我正在寻找一种策略或方法来获取我的 HTML 文件中的传感器数据。我在概念上缺少一些简单的东西吗?谢谢。
【问题讨论】:
标签: html node.js websocket arduino