Page 1 of 2

Inconsistent results measuring pulses

Posted: Thu Aug 22, 2019 1:49 pm
by ghayne
I have put together some code to read the pulses from an electricity meter. I have been testing it using a flashing LED.
Mostly the results are constant, as they should be but occasionally I get two readings in series that are double what they should be.
Could Garbage collection be the problem?

Code: Select all

from umqtt.robust import MQTTClient
from machine import Pin
import ubinascii
import machine
import micropython
import utime
import time

micropython.alloc_emergency_exception_buf(100)

meter = Pin(12, Pin.IN)
now_ms =  utime.ticks_ms()
last_ms = utime.ticks_ms()



# Default MQTT server to connect to
server = "192.168.1.2"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
c = MQTTClient(CLIENT_ID, server)
c.connect()

def calc(a):

    global now_ms
    global last_ms

    last_ms = now_ms
    now_ms =  utime.ticks_ms()
    watts = utime.ticks_diff(now_ms, last_ms)
    watts = 3600 / (watts * 0.004 )
    WATTS = str(watts)
    c.publish("current_usage", WATTS)
    print(WATTS)

def trigger(p):
  micropython.schedule(calc, 1)

meter.irq(trigger=Pin.IRQ_FALLING, handler=trigger, hard = True)

Re: Inconsistent results measuring pulses

Posted: Thu Aug 22, 2019 5:28 pm
by ghayne
Thinking about it further, garbage collection would slow down the process which would result in a lower rather than a higher reading! :o

Re: Inconsistent results measuring pulses

Posted: Thu Aug 22, 2019 8:27 pm
by Roberthh
Which kind of device are you using. With ESP32 and ESP8266 I have seen cases that signals with a slow rise or fall time trigger multiple interrupts, Slow means: transition time > 5 µs.

Re: Inconsistent results measuring pulses

Posted: Thu Aug 22, 2019 9:17 pm
by ghayne
This is on an ESP8266. That would explain the observed behaviour completely, a double trigger would halve the time. Is there a way to overcome this, A Schmitt trigger?

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 12:05 am
by jimmo
Yep, schmitt trigger.

Also, another thought. At the moment your detection code is tied up with the publishing code, so a delay in one will affect the other.

Could you break it up into two separate tasks:
- The interrupt handler records pulses and the times associated with those pulses.
- A periodic publisher scoops up recent pulse information and sends it off to mqtt.

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 6:33 am
by pythoncoder
That's certainly how I would do it. Have a trivial interrupt handler which increments a global. The main code runs a loop which:
  • Clears down the global.
  • Pauses for (say) one minute.
  • Reads the value of the global.
  • Calculates the power and publishes the result.
There are possible improvements to account for any pulses lost while in the calculate/publish phase...

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 8:10 am
by ghayne
jimmo wrote:
Fri Aug 23, 2019 12:05 am
Also, another thought. At the moment your detection code is tied up with the publishing code, so a delay in one will affect the other.
Yes jimmo, that bit needs some work too. Thanks for the input.

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 8:24 am
by ghayne
pythoncoder wrote:
Fri Aug 23, 2019 6:33 am
That's certainly how I would do it. Have a trivial interrupt handler which increments a global. The main code runs a loop which:
  • Clears down the global.
  • Pauses for (say) one minute.
  • Reads the value of the global.
  • Calculates the power and publishes the result.
There are possible improvements to account for any pulses lost while in the calculate/publish phase...


I am trying to push data in real time to an InfluxDB database feeding Grafana for display but I think your approach is more reasonable.
Every 10 seconds would probably be achievable. I cannot afford to miss any pulses (or over count) because they are being used to calculate the cost (4000 pulses kWh).

I'll post my progress here as it might help others. Many thanks for all the input.
I wish I had found my way to Micropython earlier. But I am having the time of my life. I've not had this much fun since I discovered Basic, many moons ago.

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 9:51 am
by Roberthh
Since in my ESP8266 project, where this double trigger occurred, I could not add a Schmitt-Trigger gate. The PCB was already done. Therefore I used this little script to clear the second interrupt in the ISR:

Code: Select all

#
# Clear Interrupt status
#
@micropython.viper
def clear_isr(mask: int):
    ptr32(0x60000324)[0] = mask
I call it at the end of the ISR with clear_isr(0x20), because it was GPIO 5. You have have to change the mask according to your needs.

Re: Inconsistent results measuring pulses

Posted: Fri Aug 23, 2019 1:33 pm
by ghayne
Danke Roberthh!