Maximum number of pulses

I did that scripto for turning the time between pulses into a Wattage… There was a problem with fast pulses at the time. I cranked up the priority of the process and that helped a lot. It was surprisingly accurate given rPi runs a non-RTOS.
I would have added a decay to the Wattage ‘reading’ too, so if there was suddenly a drop in pulse freq. the value in emonCMS wouldn’t be stuck at some kWs.
But yeah, if fast pulses are a problem I think you’re idea Brian of holding back the pulse counts in an accumulating variable somewhere before all the MQTT mess is a good one.

If you want to get an idea of how fast a pulse rate the input can handle, jump the gpio to another pin set as an output and send it high and low at different rates.
I can’t remember how fast it was that it started dropping triggers but it was fast, fast enough, I just sent a screenshot to T&G one day with the results, about 2 years ago that was.

That is what I have done. Not going full tilt but seems to work OK.

2020-06-30 10:43:08,444 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 94
2020-06-30 10:43:09,446 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 95
2020-06-30 10:43:10,020 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 96
2020-06-30 10:43:10,070 DEBUG    pulse2     75 NEW FRAME :
2020-06-30 10:43:10,074 DEBUG    pulse2     75 Timestamp : 1593510190.0
2020-06-30 10:43:10,077 DEBUG    pulse2     75 From Node : 3
2020-06-30 10:43:10,081 DEBUG    pulse2     75    Values : [96]
2020-06-30 10:43:10,084 DEBUG    pulse2     75 Sent to channel(start)' : ToEmonCMS
2020-06-30 10:43:10,087 DEBUG    pulse2     75 Sent to channel(end)' : ToEmonCMS
2020-06-30 10:43:10,277 DEBUG    MQTT       Publishing: emon/3/Pulse 96
2020-06-30 10:43:10,336 DEBUG    MQTT3      Publishing: emon/3/Pulse 96
2020-06-30 10:43:10,340 DEBUG    MQTT2      Publishing: emon/3/Pulse 96
2020-06-30 10:43:10,577 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 97
2020-06-30 10:43:11,131 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 98
2020-06-30 10:43:11,689 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 99
2020-06-30 10:43:12,017 DEBUG    pulse2     76 NEW FRAME :
2020-06-30 10:43:12,020 DEBUG    pulse2     76 Timestamp : 1593510192.0
2020-06-30 10:43:12,023 DEBUG    pulse2     76 From Node : 3
2020-06-30 10:43:12,026 DEBUG    pulse2     76    Values : [99]
2020-06-30 10:43:12,029 DEBUG    pulse2     76 Sent to channel(start)' : ToEmonCMS
2020-06-30 10:43:12,033 DEBUG    pulse2     76 Sent to channel(end)' : ToEmonCMS
2020-06-30 10:43:12,118 DEBUG    MQTT       Publishing: emon/3/Pulse 99
2020-06-30 10:43:12,191 DEBUG    MQTT3      Publishing: emon/3/Pulse 99
2020-06-30 10:43:12,194 DEBUG    MQTT2      Publishing: emon/3/Pulse 99
2020-06-30 10:43:12,247 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 100
2020-06-30 10:43:12,801 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 101
2020-06-30 10:43:13,359 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 102
2020-06-30 10:43:13,917 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 103
2020-06-30 10:43:14,056 DEBUG    pulse2     77 NEW FRAME :
2020-06-30 10:43:14,069 DEBUG    pulse2     77 Timestamp : 1593510194.0
2020-06-30 10:43:14,081 DEBUG    pulse2     77 From Node : 3
2020-06-30 10:43:14,085 DEBUG    pulse2     77    Values : [103]
2020-06-30 10:43:14,107 DEBUG    pulse2     77 Sent to channel(start)' : ToEmonCMS
2020-06-30 10:43:14,111 DEBUG    pulse2     77 Sent to channel(end)' : ToEmonCMS
2020-06-30 10:43:14,228 DEBUG    MQTT3      Publishing: emon/3/Pulse 103
2020-06-30 10:43:14,232 DEBUG    MQTT2      Publishing: emon/3/Pulse 103
2020-06-30 10:43:14,258 DEBUG    MQTT       Publishing: emon/3/Pulse 103
2020-06-30 10:43:14,471 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 104
2020-06-30 10:43:15,031 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 105
2020-06-30 10:43:15,589 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 106
2020-06-30 10:43:16,045 DEBUG    pulse2     78 NEW FRAME :
2020-06-30 10:43:16,048 DEBUG    pulse2     78 Timestamp : 1593510196.0
2020-06-30 10:43:16,052 DEBUG    pulse2     78 From Node : 3
2020-06-30 10:43:16,057 DEBUG    pulse2     78    Values : [106]
2020-06-30 10:43:16,062 DEBUG    pulse2     78 Sent to channel(start)' : ToEmonCMS
2020-06-30 10:43:16,067 DEBUG    pulse2     78 Sent to channel(end)' : ToEmonCMS
2020-06-30 10:43:16,148 DEBUG    Dummy-5    pulse2 : Pulse Channel 15 pulse: 107
2020-06-30 10:43:16,165 DEBUG    MQTT2      Publishing: emon/3/Pulse 106
2020-06-30 10:43:16,170 DEBUG    MQTT3      Publishing: emon/3/Pulse 106
2020-06-30 10:43:16,185 DEBUG    MQTT       Publishing: emon/3/Pulse 106

In the init, define the start point

self.last_time = time.time()

In the read check for when last called - fixed time of 2 seconds.

        if not self.pulse_received:
            return False
        elif self.last_time + 2 > time.time():
            return False

        self.pulse_received = False

        self.last_time += 2

Yes, I know it needs tidying up.

There is also a race condition between setting the flag to false and reading the number of pulses. I think at worst, the ‘read’ would return the same pulse number twice.

Instead of setting a received flag would it be better to keep a ‘last count’ and compare the new count against the last count?

There is also the point that the accumulator does not need to be a dict. We started off with reading several pins in the one interfacer but decided to move to multiple interfacers one for each pin if required.

nah, should fit into the rest fine as it is :laughing:

1 Like

I’ve updated the PR if anyone is interested in testing it.

I’ve had a bash and had the following error:

2020-07-01 09:24:08,086 INFO     MainThread Creating EmonHubPulseCounterInterfacer 'pulse2' 
2020-07-01 09:24:08,087 INFO     MainThread pulse2 : Pulse pin set to: 15
2020-07-01 09:24:08,088 ERROR    MainThread Unable to create 'pulse2' interfacer: No access to /dev/mem.  Try running as root!

Interesting, what setup? Mine is a vanilla Raspbian with the emonhub repo cloned and installed using the emonhub install script.

As standard as I think it can be here – rPi 3B+ with the Oct’19 image, updated to latest emoncms.

I cloned the bpo-pulse branch and copied the recommended config text… tried to keep everything as simple as poss.

got it, I had to run the install script.

1 Like

With the pulse sensor wired directly to the Pi GPIO pins?

I have to be honest, I’d not tried it with the emonSD image.

I used this test script I think Bruce gave me first which used the socket interfacer before I decided to try and do it ‘native’.

try:
    import RPi.GPIO as GPIO
    rpi = True
except:
    rpi = False
    print('RPi.GPIO not installed')

#import RPi.GPIO as GPIO
import time
import socket

# a "pulse" is 1 unit of measurement use emonHub scales and unit to define
# eg if 100 pulses is 1m3, ie 0.01m3 each then emonHub should use scale = 0.01 and unit = m3
# Therefore "pulse_id" becomes an accumulating "total used" and should follow the meter reading

nodeid = 3
valueid = 1
bounce = 1
interval = 5
lastsend = 0
host = "192.168.7.193" #"localhost" #emonbase1"
port = 50012
pulse_pin1 = 21
pulse_pin2 = 15

pulse_id = {1:0,2:0}

def eventHandler1(channel):
    processpulse(1,GPIO.input(channel))
def eventHandler2(channel):
    processpulse(2,GPIO.input(channel))


#debug    print("event")

def processpulse(channel,status):
    global pulse_id
    global frame
    global lastsend
    if status: #GPIO.input(channel):
        pulse_id[channel] += 1
#        print("Channel "+ str(channel) + "  on : " + str(pulse_id[channel]))
#    else:
#        print("Channel "+ str(channel) + " off : " + str(pulse_id[channel]))

    t = time.time()
    f = ' '.join((str(t), str(nodeid), str(pulse_id[1]), str(pulse_id[2])))
    if t > (lastsend + interval):
        lastsend = t
        print (f)
        send(f)


def send(f):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    f = f + '\r\n'
    s.send(f)
    s.close()


if rpi:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pulse_pin1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.add_event_detect(pulse_pin1, GPIO.BOTH, callback=eventHandler1, bouncetime=bounce)
    GPIO.setup(pulse_pin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.add_event_detect(pulse_pin2, GPIO.BOTH, callback=eventHandler2, bouncetime=bounce)


try: # CTRL+C to break - requires graceful exit
    while True:
        time.sleep(5) # the value doesn't matter.
except KeyboardInterrupt:
    GPIO.cleanup() # clean up GPIO on CTRL+C exit
    exit()

GPIO.cleanup() # just in case

cool. i’ll jump a gpio over to 15 and see about sending some pulses.

fixed now. the install.sh script added the user ‘emonhub’ to the gpio thing. I would guess that’s what fixed it for me.

1 Like

. not posting to emoncms.
. restarting emonhub failed after uncommenting rate_limit = 2
. emonhub log no longer updating
. rebooted pi and log still not updating – emonhub service failing to start with boot.

pi@emonpi:~ $ sudo systemctl status emonhub
● emonhub.service - emonHub service description
   Loaded: loaded (/opt/openenergymonitor/emonhub/service/emonhub.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-07-01 10:11:20 BST; 22s ago
  Process: 1062 ExecStartPre=/bin/mkdir -p /var/log/emonhub/ (code=exited, status=0/SUCCESS)
  Process: 1063 ExecStartPre=/bin/chgrp -R emonhub /var/log/emonhub/ (code=exited, status=0/SUCCESS)
  Process: 1064 ExecStartPre=/bin/chmod 775 /var/log/emonhub/ (code=exited, status=0/SUCCESS)
  Process: 1065 ExecStart=/usr/local/bin/emonhub/emonhub.py --config-file=/etc/emonhub/emonhub.conf --logfile=/var/log/emonhub/emonhub.log (code=exit
 Main PID: 1065 (code=exited, status=1/FAILURE)

Jul 01 10:11:20 emonpi systemd[1]: emonhub.service: Service RestartSec=100ms expired, scheduling restart.
Jul 01 10:11:20 emonpi systemd[1]: emonhub.service: Scheduled restart job, restart counter is at 5.
Jul 01 10:11:20 emonpi systemd[1]: Stopped emonHub service description.
Jul 01 10:11:20 emonpi systemd[1]: emonhub.service: Start request repeated too quickly.
Jul 01 10:11:20 emonpi systemd[1]: emonhub.service: Failed with result 'exit-code'.
Jul 01 10:11:20 emonpi systemd[1]: Failed to start emonHub service description.

seems now emonhub won’t start as the emonhub.log is broken or unreadable.

I deleted emonhub.log and tried running the emonhub install script, it didn’t replace the log file.

I would know how to set the correct permission on a recreated log file, so I’m stuck there.

Possibly the log file got corrupted as a result of some added unexpected syntax from uncommenting rate_limit? I don’t know.

I was getting the expected values in Dummy output in the log file from reading pulses at the expected intervals, only tested to 1 pulse per second so far – the basic pulse counting is working.

I deleted the log file and simply restarted emonhub service, this recreated the log file.

Pulse interfacer working again.

Uncommenting bouncetime or rate_limit causes a failure to start emonhub.

I hope that helps.

The errors often propagate to the syslog, so a journalctl -f when you restart the emonhub.

Can you check please.

Uncommenting where? in the config?

Yeah in the config, where the usual emonHub interfacers and nodes are listed.

I will when I get home.

I cloned the repo again to make it work again… I should mention a change I made which might have caused an error, which I interpreted as the log file error. I’d added a file to src/interfacers, the script I’d used to toggle the other gpio pin.
would a ‘stray’ file in the interfacers dir cause a problem? (could test this when I get home)

I hadn’t tested the passing of the options, but it all seems OK here.

alright.
so the stray file in interfacers directory does cause emonhub to fail.

typical, the tester totally jacking the test environment :slight_smile:

the counter, bouncetime and rate_limit are working flawlessly Brian. I like the way when the pulses stop, the rate_limit may fire a few seconds later – kinda wraps up the period’s counting with a post, and doesn’t post if there’s no pulse, feels tidy.

I’ve tested is at a speed of a pulse per 40ms, 200 pulses, all caught. rate_limit was at 5 seconds, so 3 posts to emoncms were fired in the time too.

1 Like

Great, many thanks for that.