Inconsistent results measuring pulses

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 3651
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Inconsistent results measuring pulses

Post by pythoncoder » Sat Aug 24, 2019 5:17 am

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
machine.enable_irq(state)
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.
Peter Hinch

User avatar
ghayne
Posts: 37
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: Inconsistent results measuring pulses

Post by ghayne » Thu Aug 29, 2019 6:32 pm

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.

ThomasChr
Posts: 120
Joined: Sat Nov 25, 2017 7:50 am

Re: Inconsistent results measuring pulses

Post by ThomasChr » Tue Sep 03, 2019 6:48 am

Hi ghayne,

I've done the very same thing (without using MQTT, that is) and here is the code:
https://github.com/ThomasChr/ESP8266-re ... er/main.py

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.

Greetings,

Thomas

Post Reply