I've recreated and adapted the Smart meter tutorial. What I've noticed is, after I upload the code to the Puck, it watches the flashes correctly and will continue until I connect from the web page. From that moment on, it is as if the watch on the pin is removed. The Puck no longer flashes when the electric meter flashes, and there are no further records of the flashes. The variables stored in the Puck remain and are not reset, it seems it is just the watch method which has an issue after connection from the web page.
I'm not sure how to debug this issue. The only place clearWatch() is called is in the onInit() function on the Puck. The connection code from the web page seems simple, and I cannot see anywhere there that might clear the watch. Any ideas on how I can further debug this issue?
Here is the code from the website connection script (I don't call any Puck functions anywhere else apart from here):
function connectDevice() {
const button = document.getElementById('connect-button')
// Disable connect button
button.disabled = true
button.textContent = 'Connecting...'
// Connect, issue Ctrl-C to clear out any data that might have been left in REPL
Puck.write("\x03", function () {
setTimeout(function () {
// After a short delay ask for the battery percentage
Puck.eval('E.getBattery()', function (batteryPercentage) {
if (!batteryPercentage) {
console.log('Battery error:')
button.textContent = 'Failed to connect'
return
}
button.textContent = 'Connected'
// Update battery meter
const batteryEl = document.getElementById('battery-container')
batteryEl.textContent = `Battery: ${batteryPercentage}%`
document.documentElement.classList.remove('not-connected')
Puck.eval('years', function (years) {
// Ensure a valid array is returned
if (typeof years !== 'object' || years.length < 20) {
return
}
console.log(years)
window.usageData = years
});
Puck.eval('currentkWh', function (currentkWh) {
window.currentkWh = currentkWh
})
})
}, 1000 * 1);
});
}
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.
I've recreated and adapted the Smart meter tutorial. What I've noticed is, after I upload the code to the Puck, it watches the flashes correctly and will continue until I connect from the web page. From that moment on, it is as if the watch on the pin is removed. The Puck no longer flashes when the electric meter flashes, and there are no further records of the flashes. The variables stored in the Puck remain and are not reset, it seems it is just the watch method which has an issue after connection from the web page.
I'm not sure how to debug this issue. The only place
clearWatch()
is called is in theonInit()
function on the Puck. The connection code from the web page seems simple, and I cannot see anywhere there that might clear the watch. Any ideas on how I can further debug this issue?Here is the code from the website connection script (I don't call any Puck functions anywhere else apart from here):