ESP8266 Dev Kit - pulse counter

Have a ESP8266 and want to connect the OpenEnergy pulse counter to it and use the board to count the pulses and output it somewhere (initially just blink the onboard LED).

Seems easiest to use the Arduino IDE for the board.

But what do I change in the emonESP sketch to pick up the pulse counter and output the readings? Can only see the CT’s being read in the currently unless I have missed something (total noob to Arduino coding).

Also do I connect the pulse counter to the ADC pin or can I use any GPIO pin?



The pulse counter output is a pulse of fixed amplitude, therefore you’ll need to use a digital, not an analogue, I/O pin.

There are two ways of counting pulses, and we’ve used both in the emonTx. The first is “Interrupt-driven”, where an input pin is configured to trigger a software interrupt. The Interrupt Service Routine then (typically) counts the interrupts and makes that number available to the main loop of the sketch.
The emonTx sketch “emonTxV3_4_DiscreteSampling” does this.

The second method is ‘Polled’, which is where the main loop interrogates the input pin at regular intervals, and counts the pulses it sees.
The sketch here does this and includes switch de-bouncing software, which you should not need with the optical sensor (but you would with a mechanical switch).

Many thanks Robert.

Do I just load this sketch (and update the GPIO reference) and it should work?



Until I look at the ESP sketch, I can’t tell you! But what either sketch does should be reasonably clear (if not, ask) so you probably need to lift the working bits out of whichever and plant them into the ESP sketch - or vice versa.

The sketch on GitHub emonTxFirmware/emontx3_emonesp.ino at master · openenergymonitor/emonTxFirmware · GitHub
uses the standard “discrete sampling” method and it does have the input code for the interrupt-driven pulse counter. But you need to enable it by changing line 10 to “#define PULSE_ENABLE 1”. Is that the sketch you were looking at?

That was the plan Robert.

Do I need to load the emon lib as well? See it referenced in the sketch.

Couldn’t see the reference to show which GPIO to use for the pulse counter input - is this set in the emon lib?

Thanks for answering the noob questions!


Yes, as it’s listed as "#include “EmonLib.h” " in the sketch, it’s necessary. We used to include a plain English “The following is required…” in the comment at the top of the sketch, but I see this has been left out in this instance.

No, it’s cunningly concealed in here:
" if (PULSE_ENABLE) attachInterrupt(1, onPulse, FALLING);"
Which means it uses Interrupt 1 pin, or Interrupt Re Quest 1, and if you look at the circuit diagram for the emonTx (on GitHub via Resources), or at the PCB itself, you’ll see that is on the green terminal block labelled IRQ1.

Many thanks Robert.

Can I used any GPIO pin on the ESP as the interupt pin?


I’ve been looking and can’t find an intelligent data sheet for the module. I don’t have a module so I’m afraid I can’t help. But I think I can safely say that you do need an I/O pin that’s designated to accept interrupts.

I have now found some data that looks to be reliable. It appears that any GPIO input pin (less than 16) can be used for interrupts, therefore take whichever is convenient and change the sketch accordingly.
If you’re not using the analogue input, you can take out everything in the sketch that references EnergyMonitor, voltage, current, CT1-4 etc. More-or-less all that you’ll be left with will be pulsecount, msgnum and the interrupt routine itself, and the watchdog timer.
(If you might need to use the only analogue input at some point, then leave CT1 in!)
If all the analogue stuff goes, I can’t see a need for emonLib.

If you can’t get input pin interrupts to work, you’ll need to adopt the ‘polled’ sketch, which can use any input pin. But as it stands, that uses timer interrupts in the Atmel 328P, which the data I’ve found doesn’t mention as being available in the ESP, so that option might not be open to you.

(Sorry, earlier I was thinking that yours was attached to an emonTx.)

Many thanks for all your help Robert :smile:

Will try to get it up and running over the next couple of weeks. Will share the code once its running or if I get stuck!