Physcial Computing Blog

Week 8 - Serial Communication pt. 2

Continuing with the serial communication, I tried to dive deeper and instead of using the p5webserial, I used the serialport node library and set up a WebSocket server so that the client side could connect and read the emitted data from the server side. I used the acceleration sensors within the Arduino and wrote the incoming values as a comma-separated string into the serial output. Inside the server-side file, I used the ReadlineParser to get a new reading every time the parser comes across a newline character. In the client side I still got the previously mentioned buffer object with hex values, I thought maybe passing it to the String() constructor would make it a string and it did! So finally I was able to get a proper reading on the client side using the serialport library and a websocket connection, I assigned the sensor values to control the rotation of a cube as a simple demonstration of what kind of reading I'm getting. I'm sure there are lots of creative applications that can stem from this simple reading.

1
2
3
4
5
6
const socket = new WebSocket("ws://localhost:8081");
let arr;
socket.addEventListener("open", (event) => {});
socket.addEventListener("message", (event) => {
  arr = String(event.data).split(",");
});

Nima Niazi