Page 2 of 2

Re: Inconsistent results measuring pulses

Posted: Sat Aug 24, 2019 5:17 am
by pythoncoder
ghayne wrote:
Fri Aug 23, 2019 8:24 am
...I cannot afford to miss any pulses (or over count) because they are being used to calculate the cost (4000 pulses kWh)...
Easily achieved. The interrupt handler updates a global. Before you clear down the global, disable interrupts. Read the global, clear it down, and re-enable interrupts. Ensure that the period for which interrupts are disabled is shorter than the shortest interval between pulses:

Code: Select all

state = machine.disable_irq()
running_total += gpulse_count
gpulse_count = 0
The reason for clearing down the global is to avoid it exceeding 30 bits in size, which would lead to it becoming a long integer which cannot be handled in an ISR. If your consumption won't exceed 268MWH between reboots you could just let the ISR update the running total and skip this bit of code.

Re: Inconsistent results measuring pulses

Posted: Thu Aug 29, 2019 6:32 pm
by ghayne
Thank you for your input Peter. I have discovered that my cheap photo sensor is not the best solution for detecting pulses and I am currently waiting for delivery of a BPW34 Photodiode to try and correct this.

Re: Inconsistent results measuring pulses

Posted: Tue Sep 03, 2019 6:48 am
by ThomasChr
Hi ghayne,

I've done the very same thing (without using MQTT, that is) and here is the code: ... er/

Works since a few months now and only resets/crashes about all two months. So pretty stable.
Just try out if your problem vanishes when trying my code.