# Photon flashing red on particle.publish \[SOLVED\]

**URL:** <https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036>\
**Category:** Troubleshooting\
**Created:** [December 5, 2015, 1:18pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036 "2015-12-05T13:18:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![batmanish](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@batmanish](https://community.particle.io/u/batmanish)\
**Post date:** [December 5, 2015, 1:18pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/1 "2015-12-05T13:18:42Z")

</div>

Hey

I have a code that runs mainly offline, but at a button press, it connects to wifi and publishes a given number of hours logged offline to a google drive document through IFTTT. Only problem is, every other time or so it flashes red after connecting the wifi, and then turns white again, without publishing. The string it publishes is limited to 30 or so characters and the `Particle.publish` is not called often, but is, however in a separate `void postToDrive()`, and I just read somewhere else that it’s better if it’s called in `main loop`! I’ll post the whole code, but it’s excruciatingly long, and the publishing is what I’m mostly uncertain of so for now I’ll post only that:  
(mind you, this is only a part of the code, but all the necessary information should still be there. the code works perfectly without the publishing)

```auto
char* myStrings[12] = { 
  "VELOtrail",
  "FutureBuilt",
  "WorkLogger",
  "LeafBall",
  "Visual ID",
  "Schoolies"
};

void setup()
{
  WiFi.off();
}

void loop() {

  if (stopActivity) {
    WiFi.on();
    strip.setBrightness(maxValue);
    for (int i = 0; i < totalPixels; i++) {
      strip.setPixelColor(i, red / factor, green / factor, blue / factor);
      strip.show();
      delay(sweepAnimation);
    }
    if (workSec > 0 && workSec <= 30) workSec = 5;
    else if (workSec > 30) {
        workMin++;
        workSec = 0;
    }
    stopHrs = workHrs; // save logged hours
    stopMin = workMin; // save logged minutes
    stopSec = workSec; // save logged seconds
    activity = false;
    stopActivity = false;
    postValues = true;
  }
  
  if (postValues) {
    if(WiFi.ready()) {
      postValues = false;
      postToDrive(); // call transmitter
    }
  }

void postToDrive() { 
    char publishData[25] = "";
    sprintf(publishData, "%s|||%u,%u", myStrings[project], stopMin, stopSec);
    Particle.publish("data", publishData);
    
    for (int i = 0; i < totalPixels; i++) {
      strip.setPixelColor(i, color);
      strip.show();
      delay(sweepAnimation);
    }
    workHrs = 0; // reset hours
    workMin = 0; // reset minutes
    workSec = 0; // reset seconds
    WiFi.off();
    timeStamp = millis();
}

```

Thank you

---

<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:** [December 5, 2015, 1:27pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/2 "2015-12-05T13:27:45Z")

</div>

First, if you are intending to run offline most of the time using `SYSTEM_MODE(SEMI_AUTOMATIC)` would be a better choice than going with default `SYSTEM_MODE(SEMI_AUTOMATIC)`.

Next you'd need to call `Particle.connect()` and check for `Particle.connected()` rather than only `WiFi.on()`/`WiFi.ready()` in order to successfully `Particle.publish()`.

Publishing without propperly established cloud connection might well result in a red SOS.

> [@batmanish](#):
>
> I just read somewhere else that it's better if it's called in main loop!

This might be a misunderstanding on your or a misconception on the sources side.  
It's best to not call `Particle.publish()` from an ISR, a `Particle.function()` or a `Particle.subscribe()` handler and such, but it's OK to put it in a "normal" function.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 5, 2015, 4:17pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/3 "2015-12-05T16:17:48Z")

</div>

@batmanish in addition to what @ScruffR, the tireless Elite said, I have to add these comments:

1. If workSec is an unsigned var then you don’t need `workSec > 0` in your IF statement
2. Assuming the above is true, then the `else if (workSec > 30)` is not necessary since values are either `<= 30" or everything else is`\> 30’. So all you need is `else {`
3. Calling Particle.process() right after Particle.publish() will ensure the publish gets processed asap.

If you want to save power, then run in `SYSTEM_MODE(MANUAL)` and write a function that:

- Turns on WiFi and waits for WiFi.ready() with a timeout using [waitFor()](https://docs.particle.io/reference/firmware/photon/#-code-waituntil-code-). You can decide what to do if the wifi doesn’t connect within the waiting time.

- Connect to the Cloud and wait for Particle.connected(), again using waitFor()

- Do the publish, call Particle.process and use one of two ways to ensure your publishes are complete:

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [December 5, 2015, 5:44pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/4 "2015-12-05T17:44:30Z")

</div>

@peekay123, In regard to your comments about ensuring that publish is complete, what does it actually mean when publish returns true? Is this only an indication that the device is connected to the cloud, or does returning true require some acknowledgement from the cloud, that the publish was successful? I’m asking about this in regard to knowing when, after a publish, I can put the Photon into deep sleep.

---

<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:** [December 5, 2015, 8:00pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/5 "2015-12-05T20:00:01Z")

</div>

This only indicates that the data has been successfully handed over to the WiFi module, which will do its job asyncronously to your application.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 5, 2015, 8:21pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/6 "2015-12-05T20:21:18Z")

</div>

@Ric, excellent question! The Particle.publish() function is managed by the system thread, in the background. So when you call Particle.publish() with or without the SYSTEM\_THREADS enabled, the function will return false if there is no active cloud connection and true if the function successfully “passed” the call to the system thread. It does not, however indicate that the publish was successfully sent to the cloud nor that the cloud actually performed the publish.

So, first to make sure the Photon has actually published, you can call Particle.process() and/or wait after the publish. Another way is to add a extra publish which fires a webhook that you also subscribe to. When the subscribe function is called, you know the end-to-end publish has completed.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [December 5, 2015, 8:39pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/7 "2015-12-05T20:39:50Z")

</div>

@peekay123, thanks for the response. I don’t understand the first option of calling Particle.process() – my understanding of that, is that it will cause an immediate process() to be called instead of waiting for the end of loop(), but you would still have to wait (for how long?) for the transmission to the cloud to occur, correct? For the second approach, do you need to use a webhook, or can the Photon just subscribe to its own extra publish event?

---

<div class="post-metadata">

**Author:** ![batmanish](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@batmanish](https://community.particle.io/u/batmanish)\
**Post date:** [December 7, 2015, 8:42am UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/8 "2015-12-07T08:42:54Z")

</div>

Thank you guys very much for all your responses. I added `SYSTEM_MODE(MANUAL)`, enabled `SYSTEM_THREAD`, changed `WiFi.on()` and `WiFi.ready()` to `Particle.connect()` and `Particle.connected()` and added `Particle.process()` and the photon now publishes every time, without ever (so far) SOSing. @peekay123, about the whole `workSec` thing, the script is a timer, and that snippet you’re mentioning is just to round up to 0,5 or 1,0 minutes (actually hours, but minutes for quicker testing), and the numbers are temporary. They may be changed to once passed, say 15 seconds, round up to 0,5 minute, and passed 45 seconds, round up to 1,0 minute, if you catch my drift. Thank you though.

---

<div class="post-metadata">

**Author:** ![batmanish](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@batmanish](https://community.particle.io/u/batmanish)\
**Post date:** [December 8, 2015, 9:19am UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/9 "2015-12-08T09:19:46Z")

</div>

Also, I guess this is solved now, how does one mark a post as solved?

---

<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:** [December 8, 2015, 2:50pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/10 "2015-12-08T14:50:09Z")

</div>

I guess you found out how to, as it is [SOLVED] now 😉

---

<div class="post-metadata">

**Author:** ![batmanish](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@batmanish](https://community.particle.io/u/batmanish)\
**Post date:** [December 8, 2015, 4:07pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/11 "2015-12-08T16:07:15Z")

</div>

Nope, someone else must have done it.

---

<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:** [December 8, 2015, 4:31pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/12 "2015-12-08T16:31:45Z")

</div>

OK, it’s rather easy.  
When you click on the topic title, a pencil-symbol will appear next to it.  
Click on that and just add the word `[SOLVED]` and press the tick button.

---

<div class="post-metadata">

**Author:** ![batmanish](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@batmanish](https://community.particle.io/u/batmanish)\
**Post date:** [December 8, 2015, 4:38pm UTC](https://community.particle.io/t/photon-flashing-red-on-particle-publish-solved/18036/13 "2015-12-08T16:38:17Z")

</div>

You know, I thought so, but it seemed so easy. Too easy! I was hoping there was a button. Oh well
