Hej @Gordon@Robin@AkosLukacs and @maze1980, thanks for taking the time to review my code and also for helping me improving it. I took the time to edit my code and include the suggestions, feel free to double check.
This should store up to 2000 readings of the high accuracy TI TMP117 sensor into an array.
const i2c = new I2C();
const tmp117Address = 0x48;
const temperatureAddress = 0x00;
const timePeriod = 30 * 1000; // every 30 secs
const log = new Float32Array(2000); // our logged data
let logIndex = 0; // index of last logged data item
let lastReadingTime; // time of last reading
// Store data into RAM
const storeMyData = (data) => {
logIndex++;
if (logIndex >= log.length) logIndex = 0;
log[logIndex] = data;
};
// Get Data and store it in RAM
const getData = () => {
i2c.writeTo({address: tmp117Address, stop: false}, temperatureAddress);
setTimeout(() => {
let reading = i2c.readFrom(tmp117Address, 2);
let dataBytes = ((reading[0] << 8) | reading[1]);
let dataCentigrades = dataBytes * 0.0078125;
storeMyData(dataCentigrades);
console.log(dataCentigrades);
lastReadingTime = Date.now();
}, 500);
};
// Dump our data in a human-readable format
const exportData = () => {
for (let i = 1; i <= log.length; i++) {
let time = new Date(lastReadingTime - (log.length - i) * timePeriod);
let data = log[(i + logIndex) % log.length];
console.log(Math.floor(time / 1000) + ' ' + data);
}
};
const startLight = () => {
digitalWrite(LED2, 1);
setTimeout(() => {
digitalWrite(LED2, 0);
}, 1000);
};
const stopLight = () => {
digitalWrite(LED1, 1);
setTimeout(() => {
digitalWrite(LED1, 0);
}, 1000);
};
i2c.setup({ scl : D31, sda: D30 });
let recording = true;
let interval = setInterval(getData, timePeriod);
setWatch(() => {
console.log("Pressed");
if (recording) {
recording = false;
clearInterval(interval);
stopLight();
} else {
recording = true;
interval = setInterval(getData, timePeriod);
startLight();
}
}, BTN, {edge:"rising", debounce: 50, repeat: true});
Then you can download this data from the puck using a site with this code
<html>
<head>
</head>
<body>
<script src="https://www.puck-js.com/puck.js"></script>
<button id="myButton">On!</button>
<script type="text/javascript">
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080');
const button = document.getElementById("myButton");
function onLine(data) {
// CSV data is received here
socket.send(data);
console.log(data);
}
var connection;
button.addEventListener("click", function() {
if (connection) {
connection.close();
connection = undefined;
}
Puck.connect(function(c) {
if (!c) {
alert("Couldn't connect!");
return;
}
connection = c;
// Handle the data we get back, and call 'onLine'
// whenever we get a line
var buf = "";
connection.on("data", function(d) {
buf += d;
var i = buf.indexOf("\n");
while (i>=0) {
onLine(buf.substr(0,i));
buf = buf.substr(i+1);
i = buf.indexOf("\n");
}
});
// Request data from Puck.js
connection.write("\x10exportData()\n");
});
});
</script>
</body>
</html>
but before make sure you are running a node process with this code, it will listen to socket data and use Winston to dump it into the file system.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hej @Gordon @Robin @AkosLukacs and @maze1980, thanks for taking the time to review my code and also for helping me improving it. I took the time to edit my code and include the suggestions, feel free to double check.
This should store up to 2000 readings of the high accuracy TI TMP117 sensor into an array.
Then you can download this data from the puck using a site with this code
but before make sure you are running a node process with this code, it will listen to socket data and use Winston to dump it into the file system.