emonTH Pulse Counting - dev firmware

I have been working on adding low power pulse counting support to the emonTH. I have pushed dev firmware sketch to the emonTH development branch on GitHub:

​https://github.com/openenergymonitor/emonTH/blob/development/emonTH_DHT22_DS18B20_RFM69CW/emonTH_DHT22_DS18B20_RFM69CW.ino

It seems to be working well. As with the standard emonTH  sensor readings are taken every 60s and data transmitted via RF. In between readings the ATmega goes into a deep sleep. If a pulse is detected (interrupt on IRQ1) then the emonTH wakes up and increments the pulse count. The current pulse count is transmitted in the RF payload once every 60s. 

However the one issue I have is when a pulse is detected and the emonTH wakes up (on external interrupt) is also then seems to execute some (not all?) of the code in the main loop before going back to sleep again.  This results in '0' being transmitted via RF each time a pulse is detected. I think this could be something to do with the internal state registers not being properly restored when being suddenly awakened by a pulse interrupt?

Here is the JeeLabs sleep function the emonTH uses: https://github.com/jcw/jeelib/blob/master/Ports.cpp#L1139

Could there be issues using both a watchdog interrupt to wake from sleep and an external interrupt? 

The optical pulse sensor can be wired into the emonTH as follows: 

http://wiki.openenergymonitor.org/index.php/EmonTH_V1.5#Pulse_Sensor_Con...

 

dBC's picture

Re: emonTH Pulse Counting - dev firmware

However the one issue I have is when a pulse is detected and the emonTH wakes up (on external interrupt) is also then seems to execute some (not all?) of the code in the main loop before going back to sleep again.

Regardless of how it wakes up (wdog or external interrupt), once awake it will return from your call to dodelay(100). (By the way, it looks like you only sleep for 100msecs, but above you said 60 seconds?)  If no pulses occurred then it will wake up 100msecs later, but if one did occur it wakes up at the time of that pulse.  Either way, it should then continue to execute your main loop again until your next call to dodelay().

This results in '0' being transmitted via RF each time a pulse is detected.

While you're asleep, CLKio won't be running which means timer0 won't be running which means millis() and micros() will be frozen in time.  I suspect that'll cause the debounce code inside in your pulse ISR to think the pulse time is too short and so not count it.  It looks like Sleepy's loseSomeTime() attempts to fix that up, but it can only do that when it was awakened by the wdog.  It seems if it's awakened by an external interrupt it guesses... (half the requested sleep interval).  And in any case, that code runs after your ISR has returned.

I'm also a bit puzzled about your ADC transactions in the pulse counter ISR.  The guts of that ISR doesn't need the ADC to be powered up does it?

Eric_AMANN's picture

Re: emonTH Pulse Counting - dev firmware

Hy Glyn,

Glad to see that you're developing such a sketch because I'm also working on that point.

You can find here the last version. It was first intended to work with a LDR but I'm now using it with the optical pulse sensor from the shop. My approach is different and maybe too extricate. I hope it may help you.

Eric

goodfidelity's picture

Re: emonTH Pulse Counting - dev firmware

I have found a one wire sensor counter, that can count pulses and be addressed on the one wire sensor bus.

http://www.maximintegrated.com/en/products/comms/one-wire/DS2423.html

Is it possible to use this unit for counting pulses instead of using the arduino directly? Will it save energy from the arduino? Because then the arduino can wake up every now and then and just read the state of the one wire counter and post it to the emoncms. No need to wake up on every pulse.

 

Also it seems the counter has two inputs, maybe two energymeters can be read at the same time.

 

Just a thought.

 

//J

kirkholt's picture

Re: emonTH Pulse Counting - dev firmware

The DS2423 was a brilliant little product, Unfortunately it is no longer available. 

This i stated in the link you refer to

 

goodfidelity's picture

Re: emonTH Pulse Counting - dev firmware

The DS2423 was a brilliant little product, Unfortunately it is no longer available. 

This i stated in the link you refer to

 

Ah. 

Didnt quite see that when i read it. Thats a real shame. I think that the one wire chip for this might be a good solution for many problems.

Does anyone know if there is replacement for this functionality?

 

//J

goodfidelity's picture

Re: emonTH Pulse Counting - dev firmware

Glyn!

 

I have some questions, is it possible to hook up two pulse counters to one emonTH?

Is there a wiring diagram for the LED reader, with the rear mounted LED? I would be happy to add the same feature to my setup, as i have only the LED reader and no verification led on the back which i found very clever.

 

//J

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.