Pulse counter working on Core but not on Photon

Hi All,

I have had a Core using a TSL251R-LF light-to-voltage working as pulse counter hooked up to my electricity meter running for a while now. As I have some connectivity issues with the Core, I desided to try a Photon with the same code. As it does not have interrupt on pin D0, I have changed it to D1 from the original code, this is the only thing I have changed. It flashes ok, and connects and publish.

But the counting does not happend. I’m a novice at this, is there something I have missed?

Here is my code, it’s based on https://github.com/scottsweb/watz / Watz

// Puls counter for energy-meter based on https://github.com/scottsweb/watz 
// pins
int intled = D7;
int sensor = D1;

// general vars
char json[64];

// pulse vars
void pulseInt(void);		    // function for interrupt
float pulseCount = 0;  			// counts power pulses on sensor

void setup() {
	// set led as output
	pinMode(intled, OUTPUT);
	// set sensor as input
	pinMode(sensor, INPUT);
	// attach interrupt to LED pin, when RISING
	attachInterrupt(sensor, pulseInt, RISING);

void loop() 
	// publish once every 1 minutes
	if (Time.second() == 30) 
		noInterrupts();   // Disables interrupts while publishing data
		float avgInterval = 60 / pulseCount;
		float kw = 3600 / (avgInterval * 1000);
		sprintf(json,"{\"kw\": %.3f, \"count\": %.0f}", kw, pulseCount);
		Particle.publish("watz", json, 60, PRIVATE);
		pulseCount = 0;
		delay(1500);  // delay more than 1000ms so that the data on second 30 is only published one time
		interrupts(); // Re-enables interrupts when data is published
void pulseInt() {
	// visually indicate the blink has been detected on internal LED D7
	digitalWrite(intled, HIGH);
	digitalWrite(intled, LOW);
	// increment pulse count

All help is appreciated

You can’t call delay() from an interrupt service routine.

And, that noInterrupts() block in loop stops interrupts for way too long. I’m not sure that that delay will work correctly without interrupts enabled either. You should minimize the no interrupts section, sort of like this:

int pulseCountCopy = pulseCount;
float avgInterval = 60.0 / (float)pulseCountCopy;
float kw = 3600 / (avgInterval * 1000);
sprintf(json,"{\"kw\": %.3f, \"count\": %.0f}", kw, pulseCountCopy);
Particle.publish("watz", json, 60, PRIVATE);
pulseCount -= pulseCountCopy; 
delay(1500);  // delay more than 1000ms so that the data on second 30 is only published one time

And pulseCount should be declared as a volatile int:

volatile int pulseCount = 0;

Thank you.

Your first post solved the issue, and it’s now doing what it is suppose to do.

I added the noInterrupts() in the loop to try to not have the Core drop out of the cloud all the time. It happened most when there is a heavy load, and lots of flashes, my novice logic was that the interrupts might influence the publish events. I think it helped, but it’s hard to say, maybe a 25% increase in successful published events.

I’ll take a look at the rest of your suggestions and try to implement that too.

Thank you again, have a nice day :smile: