-
Good catch with the division. I've now added the try/catch to see if the issue is with that.
Regarding L68, I'm not sure it is acceptable practice, but it was a trick I found to round a number to x decimal places. If that is the issue, then the try/catch should catch that, although I've been using the same snippet in other work, so I'd be surprised if that is the issue.
Thanks for the tips, I've updated the Puck code so I'll wait and see what gets logged out.
-
-
I was so sure it was the battery that's the issue as it was working for 4-5 days without issue. Then last night (still with the new battery) it stopped working again.
So whilst it is broken, when the light on the electric meter flashes, I don't get any light flashing on the Puck (I did implement the try/catch above, but neither the red nor green light flashes). I get no error messages when connected in the IDE. I wondered could it have something to do with the memory slots, but doing
process.memory().free
showed2016
, which I assume is more than enough.Would you happen to have any other ideas what I could try?
-
-
It looks like you were right about the battery. Although when I checked, it showed around 50%, every time I checked the percentage varied considerably. It was an old battery, so I changed it for a new one. It's been many days in now and there's not been a single issue with the watch not working.
Many thanks for helping to fix this issue!
-
@Gordon This is what the end of the
dump()
looks like -
@Gordon actually I was mistaken in the last message. When I check the
dump()
again, I can see that thesetWatch
code is repeated at the bottom. Surely this has to be the issue? What could cause the duplication of thesetWatch
script? -
You're correct that the original code did work without issue. Thanks for the suggestion about nesting
Puck.eval
. I've now implemented that, but the issue has occurred again. Not sure if it's related, but it seemed to stop watching the flashes after I receivedgattserverdisconnected
error (although I haven't noticed that happen before in relation to it stopping working).The current Puck code is as follows:
const years = [] let lastFlashTime = Date.now() let currentkWh = 0 /** * Get the last 2 digits of full year * @param {number} year Year to parse * @return {number} */ function getYear(year) { return parseInt(year.toString().substr(-2)) } /** * Increment count, create new year, month, date, hour as needed */ function increment() { const d = new Date() const hour = d.getHours() const date = d.getDate() const month = d.getMonth() const year = d.getFullYear() // 2-digit year, as number const yr = getYear(year) if (years[yr]) { if (years[yr][month]) { if (years[yr][month][date]) { if (years[yr][month][date][hour]) { years[yr][month][date][hour]++ } else { // No hour in current date years[yr][month][date][hour] = 1 } } else { // No date in current month years[yr][month][date] = new Uint16Array(24) years[yr][month][date][hour] = 1 } } else { // No month in current year years[yr][month] = [] years[yr][month][date] = new Uint16Array(24) years[yr][month][date][hour] = 1 } } else { // Current year not yet created years[yr] = [] years[yr][month] = [] years[yr][month][date] = new Uint16Array(24) years[yr][month][date][hour] = 1 } } /** * Set the current usage in kWh from timings of flashes */ function setCurrentUsage() { const currentTime = Date.now() const diffMs = currentTime - lastFlashTime const diffSec = diffMs / 1000 const kWh = (3600 / diffSec) * 0.001 const decimalPlaces = 2 currentkWh = Number(Math.round(kWh + 'e' + decimalPlaces) + 'e-' + decimalPlaces) lastFlashTime = currentTime } /** * Watch fires an update */ function update() { increment() setCurrentUsage() } /** * Initialization function */ function onInit() { clearWatch() D1.write(0) pinMode(D2, 'input_pullup') setWatch(function (e) { update() digitalPulse(LED1, 1, 1) // Show activity }, D2, { repeat: true, edge: 'falling' }) }
When I type
dump()
into the IDE whilst it's broken, I still see thesetWatch
there, nothing seems out of place.Regarding your comment about an error in
setWatch
, how would I go about catching an error in there? I guess atry/catch
? -
Thanks. I don't print or log anything on the Puck itself, there's no libs used and the only code that interacts with the Puck from the web page is that shown above. I tried the possible fix from the link, but the issue still persists.
It doesn't always stop working on the first connect from the web page, but usually by the second or third time it's stopped.
-
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):
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); }); }
-
-
Hi, I've been trying out the DIY smart meter project, however, whenever I try to read the information via the example web page, I get the error:
Unable to decode "<- Serial1\r", got SyntaxError: Unexpected token < in JSON at position 0
I was on version 2.03 at the time. I updated to 2.04 and the problem persists. I've tried stripping out all but the essential code on both the puck and the web page, but still I'm getting the JSON error. The strange thing is that once it worked correctly and the value came through to the web page. I don't think I did anything special that time and I haven't been successful since.
Any ideas what could be causing this?
-
-
Hi all. I've just created a web app that can use Puck.js to help users type, as an alternative to traditional keyboard entry. Maybe useful for those with reduced mobility.
It includes predicted words logic, as well as basic settings.Once connected to Puck.js from the menu bar button, a single click starts the row selector. Another click will select the row and start iterating through the letters in the row.
Also works without Puck.js by using the keyboard's space bar instead.
-
@Tobi great variations on the game! The double-click in the advanced game is a really interesting twist. I seem to get faster times when the LED remains on, compared to my game where the LED flashes.
-
I built a reaction game for the Puck. Each turn you have to beat the fastest reaction time. After turning on, a random amount of time will pass, then a blue LED will flash. Click the button as fast as possible. Green LED indicates a new fastest time. Another click starts a new turn. Gets quite addictive! Fastest time can be reset by holding the button down for 3 seconds.
Yes, you're right, I should just format it on the page. Thanks.