Avatar for Fisu

Fisu

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

Most recent activity

  • 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:

    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 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