# Can't Particle.publish() in setup() with CPP file

**URL:** <https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688>\
**Category:** Firmware\
**Tags:** argon\
**Created:** [April 8, 2024, 9:26pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688 "2024-04-08T21:26:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob7](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@rob7](https://community.particle.io/u/rob7)\
**Post date:** [April 8, 2024, 9:26pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/1 "2024-04-08T21:26:01Z")

</div>

I'm updating some classroom projects that use the older .INO templates. In the code, there is a call to `Particle.publish()` in `setup()`. For simplicity, it looks like this

```c++
void setup() {
    Serial.begin(9600);
    Particle.publish("SETUP", "SETUP DATA");
    Serial.println("Setup publish");
}

```

However, when I switch to the .CPP template using the same code, the `publish` never shows up on the Particle Console (but the `Serial.println` does.

Is it not possible to call `Particle.publish()` in `setup()` when using a .CPP file? If that's the case, is there a simple way to execute publish statement at startup (I could set a flag that is checked for one-time actions, but putting the code in `setup()` seems more appropriate).

Thanks  
Rob

---

<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 9, 2024, 9:18am UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/2 "2024-04-09T09:18:48Z")

</div>

Are you using `SYSTEM_THREAD(ENABLED)` or any of the non-AUTOMATIC `SYSTEM_MODE()`s?

You can only publish once `Particle.connected()` returns `true`.  
And even then, if you wish to subscribe to a such an event (or remote response to it) chances are high, that the subcription will not become active till after the end of `setup()`.

---

<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:** [April 9, 2024, 9:25am UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/3 "2024-04-09T09:25:43Z")

</div>

What ScruffR said. The old template did not enable threading, but the new .cpp one does. It's not a .ino/.cpp difference per se, but rather:

```cpp
SYSTEM_THREAD(ENABLED);

```

When threading is enabled setup runs before cloud connectivity is established and publish may fail.

With threading disabled (or the line is missing), setup and loop only run when cloud connected and publish will generally succeed.

Setting a flag and publishing from loop when cloud connected is the best long-term solution, but you could insert this right before the publish call to make setup wait until cloud connected so the publish works.

```cpp
waitUntil(Particle.connected);
Particle.publish("SETUP", "SETUP DATA");

```

---

<div class="post-metadata">

**Author:** ![rob7](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@rob7](https://community.particle.io/u/rob7)\
**Post date:** [April 10, 2024, 7:03pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/4 "2024-04-10T19:03:35Z")

</div>

Thank you @rickkas7 and @ScruffR ! That is very helpful. I think the flag will work fine for students.

I did have two followup questions.

1. With threading enabled, it is still fine to `Particle.subscribe()` in `setup()`?

2. I tried using a flag approach to publish in `loop()` but it doesn't seem to be working with threading enabled (but the print statement works). Is there a different event I should be waiting for?

```auto
bool runOnce = true;

void loop() {
    if (runOnce == true) {
        runOnce = false;
        Particle.publish("WeatherStackJSON", "", PRIVATE);
        Serial.println("run once");
    }

```

---

<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:** [April 11, 2024, 9:42am UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/5 "2024-04-11T09:42:25Z")

</div>

Yes, you should subscribe in setup. However, you cannot rely on subscriptions taking effect until after you return from setup.

You need to check for `runOnce && Particle.connected()` in loop.

---

<div class="post-metadata">

**Author:** ![rob7](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@rob7](https://community.particle.io/u/rob7)\
**Post date:** [April 11, 2024, 9:34pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/6 "2024-04-11T21:34:14Z")

</div>

> [@rickkas7](#):
>
> Particle.connected()

Thank you!

---

<div class="post-metadata">

**Author:** ![rob7](https://avatars.discourse-cdn.com/v4/letter/r/9fc348/32.png) [@rob7](https://community.particle.io/u/rob7)\
**Post date:** [April 17, 2024, 6:40pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/7 "2024-04-17T18:40:32Z")

</div>

@rickkas7 As a followup, it seems that `Serial.println()` also doesn't work with threading enabled. For example,

```auto
SYSTEM_THREAD(ENABLED);
void setup() { 
  Serial.println("setup!");
   }

```

This doesn't ever show the print. I wouldn't think this would be connected to `Particle.publish()` situation you described above. Is this the intended behavior?

---

<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:** [April 17, 2024, 6:44pm UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/8 "2024-04-17T18:44:05Z")

</div>

It works, it's just that the statement is executed before USB serial can reconnect. If you need early log message, you can add this to the beginning of setup():

```cpp
waitFor(Serial.isConnected, 15000);
delay(1000);

```

This will wait 15 seconds for USB serial to connect, then wait 1 more second before continuing. Comment this out before deploying the device in production so it won't wait 16 seconds on every boot if there's no USB serial monitoring.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)\
**Post date:** [May 18, 2024, 4:45am UTC](https://community.particle.io/t/cant-particle-publish-in-setup-with-cpp-file/66688/9 "2024-05-18T04:45:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
