[solved] Electron Particle.publish multiple times

I can’t figure out why I’m getting multiple publish events in the dashboard with my brand new Electron. Code below, local build under 0.4.9:

#include "application.h"

uint32_t lastTime;

void setup()


void loop()

  lastTime = millis();
  if (lastTime > 300000) { //5 minutes
      lastTime = 0;
      Particle.publish("electron1", "5 min marker test", 60, PRIVATE);
  }//if (lastTime == 300000)


The Dashboard displays:

and this happens whether I have Particle.process() or not.

Am I missing something simple??

Your logic isn’t right. There are any number of ways to fix it, but one way would be:

Initialize lastTime to zero:
uint32_t lastTime = 0;

Remove this line:
lastTime = millis();

Change the test to:
if ((lastTime + 300000) <= millis())

Change the lastTime = 0 line to:
lastTime = millis();

It’s not necessary to call Particle.process() if you’re going to return from loop() right away, but that’s not the problem.

1 Like

Thanks very much @rickkas7 , works perfect with your fixes. Gotta rethink my logic!

Glad it worked! Also, there are two ways you could code this, one way that makes it publish immediately after reboot and then every 5 minutes after that, or this way that also waits at 5 minutes after reboot. Since this is for the electron, I purposely wrote it to wait 5 minutes after booting in case some sort of error causes the device to go into a frequent unplanned reboot so it won’t keep publishing values after each reboot in rapid succession, just to be safe and not consume too much data.


@rickkas7 Thanks for pointing out that last subtle, but really very important, point about protecting against frquent reboots

I noticed that with electron Publish events is sent twice.
Firmware version is 0.5.0-rc.2.
Can anyone verify?

Nope, not for me.

This code

int count;

void setup() {

void loop() {
  Particle.publish("test", String::format("%d at %d ms", count++, millis()), PRIVATE);

How does your code look and how are you building/flashing?

Hello @ScruffR.
Thanks about the rapid response :slight_smile:
I compile and flash with WEB IDE, just very strange now that same code works great.
Is it possible the problem was to Particle Dashboard? Sometimes it happens before the begin displaying Event’s have a big delay. Then shows only waiting for events …

I couldn’t really comment on that, sorry.