# \[solved\] Electron Particle.publish multiple times

**URL:** https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088
**Category:** Troubleshooting
**Created:** [February 15, 2016, 7:25pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088 "2016-02-15T19:25:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 15, 2016, 7:25pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/1 "2016-02-15T19:25:14Z")

</div>

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"
SYSTEM_MODE(AUTOMATIC);

uint32_t lastTime;

void setup()
{

}//setup

void loop()
{

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

}//loop

```

The Dashboard displays:

![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/d/d80e755f3d3f3d92c46ae257ecc48267e082b5a5.png)

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

Am I missing something simple??

---

<div class="post-metadata">

### Author: ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)
#### Post date: [February 15, 2016, 7:53pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/2 "2016-02-15T19:53:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 15, 2016, 8:09pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/3 "2016-02-15T20:09:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rickkas7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rickkas7/32/8681_2.png) [@rickkas7](https://community.particle.io/u/rickkas7)
#### Post date: [February 15, 2016, 8:34pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/4 "2016-02-15T20:34:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bpr](https://avatars.discourse-cdn.com/v4/letter/b/8797f3/32.png) [@bpr](https://community.particle.io/u/bpr)
#### Post date: [February 15, 2016, 8:55pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/5 "2016-02-15T20:55:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![developer\_bt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/developer_bt/32/1311_2.png) [@developer\_bt](https://community.particle.io/u/developer_bt)
#### Post date: [April 16, 2016, 6:02pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/6 "2016-04-16T18:02:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [April 16, 2016, 6:37pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/7 "2016-04-16T18:37:55Z")

</div>

Nope, not for me.  
 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/9/9985cf99e04db33e1ebba7e004db2c60fed8add1.png)

This code

```cpp
int count;

void setup() {
  Serial.begin(9600);
}

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

```

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

---

<div class="post-metadata">

### Author: ![developer\_bt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/developer_bt/32/1311_2.png) [@developer\_bt](https://community.particle.io/u/developer_bt)
#### Post date: [April 16, 2016, 8:14pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/8 "2016-04-16T20:14:59Z")

</div>

Hello @ScruffR.  
Thanks about the rapid response 🙂  
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 …

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [April 16, 2016, 8:20pm UTC](https://community.particle.io/t/solved-electron-particle-publish-multiple-times/20088/9 "2016-04-16T20:20:47Z")

</div>

I couldn’t really comment on that, sorry.
