brian150509
Member since Oct 2022 • Last active Dec 2022Most recent activity
-
- 4 comments
- 420 views
-
-
I've tried creating an electric pulse meter with the puck js 2.1 The code works on occasion but then starts reporting funny values for watts. The general idea of the code is to get the current time, compare it with the previous time of the last pulse and use the time difference to calculate the watts. Sometimes however, even though i can tell from the logs that say the last pulse time was 12 or so seconds ago, the calculation only reads .01 of a second therefore throwing the calculation off. It's not consistent, sometimes it gets it bang on, other times not. For instance I ran it yesterday for the first time, it seem to run fine for a few hours but then started reporting erroneous values. When i ran updated code 10 minutes ago with better logging, i can see that every second reading is correct. Note originally i was using Date.now() for the time, but then tried getTime() to see if it would fix the issue, but it did not. Please see logs and code below.
var watthours = 0; var lastpulse = getTime(); var watts = 0; var battery = Puck.getBatteryPercentage(); var debug = 1; function advertise() { NRF.setAdvertising([ 0x0e, 0xff, 0xa0, 0x83, watthours, watthours >> 8, watthours >> 16, watthours >> 24, watts, watts >> 8, // current usage battery, // battery % 0x00, 0x00, 0x00, 0x00 ], { name: "The Q Meter 3000", interval: 600 }); } function calculate() { var now = getTime(); var pulsetime = now - lastpulse; if (debug) { console.log(`\n${now} now`); console.log(`${pulsetime} pulse time`); console.log(`${lastpulse} last pulse`); } lastpulse = now; watts = 3600 / pulsetime; watts = Number(watts.toFixed(0)); if (debug) { console.log(`${watts} watts`); console.log(`${watthours} watt hours`); console.log(`${battery}% battery`); } } // Set up pin states D1.write(0); pinMode(D2, "input_pullup"); // Watch for pin changes setWatch(function(e) { watthours++; if (watthours % 720 == 0) { battery = Puck.getBatteryPercentage(); } calculate(); if (watthours > 2) { advertise(); } }, D2, { repeat: true, edge: "falling" });
Here are the logs
1666997346.05803203582 now 0.00997924804 pulse time 1666997346.04805278778 last pulse 360748 watts 122 watt hours 76% battery 1666997359.96943950653 now 13.91140747070 pulse time 1666997346.05803203582 last pulse 258 watts 123 watt hours 76% battery 1666997359.97951030731 now 0.01007080078 pulse time 1666997359.96943950653 last pulse 357469 watts 124 watt hours 76% battery 1666997373.88667583465 now 13.90716552734 pulse time 1666997359.97951030731 last pulse 258 watts 125 watt hours 76% battery 1666997373.89662456512 now 0.00994873046 pulse time 1666997373.88667583465 last pulse 361855 watts 126 watt hours 76% battery 1666997387.80678081512 now 13.91015625 pulse time 1666997373.89662456512 last pulse 258 watts 127 watt hours 76% battery 1666997387.81676006317 now 0.00997924804 pulse time 1666997387.80678081512 last pulse 360748 watts 128 watt hours 76% battery
Thanks. That worked too. I had written a workaround to say if the reading was too small then skip. But denounce:20 accomplishes the same thing. Thank you.