added json parsing

This commit is contained in:
sam
2023-10-11 16:47:39 +02:00
parent 65b0c13a86
commit a076c49dd2

View File

@@ -4,7 +4,7 @@ let reader;
let decoder = new TextDecoder("utf-8");
let counterElement = document.getElementById("counter");
let readibleoutput = 0;
let temp = 0;
let tempArray = [];
let humid = 0;
let licht = 0;
@@ -28,50 +28,50 @@ async function readLoop() {
const { value, done } = await reader.read();
for (let iByte=0; iByte < value.length; iByte++){
for (let iByte = 0; iByte < value.length; iByte++) {
let singleByte = value[iByte];
if (singleByte != 10){
buffer.push(singleByte);
}
else {
let sensorString = decoder.decode(new Uint8Array(buffer));
let SensorValues;
try{
SensorValues = JSON.parse(sensorString);
} catch(e){
console.log("json niet geparserd");
if (singleByte != 10) {
buffer.push(singleByte);
}
if (SensorValues) {
console.log(SensorValues)
else {
let sensorString = decoder.decode(new Uint8Array(buffer));
let SensorValues;
try {
SensorValues = JSON.parse(sensorString);
} catch (e) {
console.log("json niet geparserd");
}
if (SensorValues) {
console.log(SensorValues)
tempArray.push(SensorValues[0])
}
buffer = [];
// var array = sensorString.split(','),
// temp = array[0], humid = array[1], licht = array[2];
// console.log(temp);
// console.log(humid);
// console.log(licht);
}
console.log(sensorString);
buffer = [];
// var array = sensorString.split(','),
// temp = array[0], humid = array[1], licht = array[2];
// console.log(temp);
// console.log(humid);
// console.log(licht);
}
// Show the received data in the console
// Show the received data in the console
// Exit the loop when done
if (done) {
console.log('[readLoop] DONE', done);
reader.releaseLock();
break;
// Exit the loop when done
if (done) {
console.log('[readLoop] DONE', done);
reader.releaseLock();
break;
}
}
}
}
}