Avatar for Fisu

Fisu

Member since Dec 2016 • Last active Nov 2019
  • 2 conversations
  • 17 comments

Most recent activity

    • 22 comments
    • 8.89k views
    @Fisu replied
  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    Yes, you're right, I should just format it on the page. Thanks.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    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.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    Many thanks. Plenty of ideas to try there. I'll implement what you suggest and report back what happens when it fails again.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    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 showed 2016, which I assume is more than enough.

    Would you happen to have any other ideas what I could try?

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    Thanks for the link. Some interesting stuff there.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    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!

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    @Gordon This is what the end of the dump() looks like

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    @Gordon actually I was mistaken in the last message. When I check the dump() again, I can see that the setWatch code is repeated at the bottom. Surely this has to be the issue? What could cause the duplication of the setWatch script?

  • in Puck.js, Pixl.js and MDBT42
    Avatar for Fisu

    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 received gattserverdisconnected error (although I haven't noticed that happen before in relation to it stopping working).

    The current Puck code is as follows:

    1. const years = []
    2. let lastFlashTime = Date.now()
    3. let currentkWh = 0
    4. /**
    5. * Get the last 2 digits of full year
    6. * @param {number} year Year to parse
    7. * @return {number}
    8. */
    9. function getYear(year) {
    10. return parseInt(year.toString().substr(-2))
    11. }
    12. /**
    13. * Increment count, create new year, month, date, hour as needed
    14. */
    15. function increment() {
    16. const d = new Date()
    17. const hour = d.getHours()
    18. const date = d.getDate()
    19. const month = d.getMonth()
    20. const year = d.getFullYear()
    21. // 2-digit year, as number
    22. const yr = getYear(year)
    23. if (years[yr]) {
    24. if (years[yr][month]) {
    25. if (years[yr][month][date]) {
    26. if (years[yr][month][date][hour]) {
    27. years[yr][month][date][hour]++
    28. } else {
    29. // No hour in current date
    30. years[yr][month][date][hour] = 1
    31. }
    32. } else {
    33. // No date in current month
    34. years[yr][month][date] = new Uint16Array(24)
    35. years[yr][month][date][hour] = 1
    36. }
    37. } else {
    38. // No month in current year
    39. years[yr][month] = []
    40. years[yr][month][date] = new Uint16Array(24)
    41. years[yr][month][date][hour] = 1
    42. }
    43. } else {
    44. // Current year not yet created
    45. years[yr] = []
    46. years[yr][month] = []
    47. years[yr][month][date] = new Uint16Array(24)
    48. years[yr][month][date][hour] = 1
    49. }
    50. }
    51. /**
    52. * Set the current usage in kWh from timings of flashes
    53. */
    54. function setCurrentUsage() {
    55. const currentTime = Date.now()
    56. const diffMs = currentTime - lastFlashTime
    57. const diffSec = diffMs / 1000
    58. const kWh = (3600 / diffSec) * 0.001
    59. const decimalPlaces = 2
    60. currentkWh = Number(Math.round(kWh + 'e' + decimalPlaces) + 'e-' + decimalPlaces)
    61. lastFlashTime = currentTime
    62. }
    63. /**
    64. * Watch fires an update
    65. */
    66. function update() {
    67. increment()
    68. setCurrentUsage()
    69. }
    70. /**
    71. * Initialization function
    72. */
    73. function onInit() {
    74. clearWatch()
    75. D1.write(0)
    76. pinMode(D2, 'input_pullup')
    77. setWatch(function (e) {
    78. update()
    79. digitalPulse(LED1, 1, 1) // Show activity
    80. }, D2, { repeat: true, edge: 'falling' })
    81. }

    When I type dump() into the IDE whilst it's broken, I still see the setWatch 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 a try/catch?

Actions