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
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Inconsistent results measuring pulses

Post by ghayne » Thu Aug 22, 2019 1:49 pm

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)

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

Re: Inconsistent results measuring pulses

Post by ghayne » Thu Aug 22, 2019 5:28 pm

Thinking about it further, garbage collection would slow down the process which would result in a lower rather than a higher reading! :o

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Inconsistent results measuring pulses

Post by Roberthh » Thu Aug 22, 2019 8:27 pm

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.

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

Re: Inconsistent results measuring pulses

Post by ghayne » Thu Aug 22, 2019 9:17 pm

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?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Inconsistent results measuring pulses

Post by jimmo » Fri Aug 23, 2019 12:05 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Inconsistent results measuring pulses

Post by pythoncoder » 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...
Peter Hinch
Index to my micropython libraries.

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

Re: Inconsistent results measuring pulses

Post by ghayne » Fri Aug 23, 2019 8:10 am

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.

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

Re: Inconsistent results measuring pulses

Post by ghayne » Fri Aug 23, 2019 8:24 am

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Inconsistent results measuring pulses

Post by Roberthh » Fri Aug 23, 2019 9:51 am

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.

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

Re: Inconsistent results measuring pulses

Post by ghayne » Fri Aug 23, 2019 1:33 pm

Danke Roberthh!

Post Reply