added code from tutorial

This commit is contained in:
sam
2023-10-10 15:31:06 +02:00
parent cf43544909
commit f75e67fe99

View File

@@ -15,22 +15,35 @@ async function connect() {
// Read data // Read data
async function readLoop() { async function readLoop() {
let buffer = [];
// loop until reader.cancel() is called // loop until reader.cancel() is called
while (true) { while (true) {
// Wait for data // Wait for data
const { value, done } = await reader.read(); const { value, done } = await reader.read();
for (let iByte=0; iByte < value.length; iByte++){
let singleByte = value[iByte];
if (singleByte = 10)
buffer.push(singlebyte);
else {
let sensorString =
console.log(buffer);
buffer = [];
}
// Show the received data in the console // Show the received data in the console
if (value) { // if (value) {
readibleoutput = (decoder.decode(value)); // readibleoutput = (decoder.decode(value));
var array = readibleoutput.split(','), // var array = readibleoutput.split(','),
temp = array[0], humid = array[1], licht = array[2]; // temp = array[0], humid = array[1], licht = array[2];
console.log(temp); // console.log(temp);
console.log(humid); // console.log(humid);
console.log(licht); // console.log(licht);
setTimeout(() => { // }
}, 400);
}
// Exit the loop when done // Exit the loop when done
if (done) { if (done) {
console.log('[readLoop] DONE', done); console.log('[readLoop] DONE', done);
@@ -38,8 +51,8 @@ async function readLoop() {
break; break;
} }
} }
}
} }
// Close the port // Close the port
async function disconnect() { async function disconnect() {
await reader.cancel(); await reader.cancel();