Missing publishes in manual mode after deep sleep (on a timer)?

Hi there! I’m working on a low power wifi project with a Photon and am having some trouble getting consistent publishing after coming out of deep sleep in MANUAL mode. After digging in the forums and not finding anything addressing this particular problem, I’m here to ask for some help. (Apologies if I’ve missed this problem being addressed somewhere else!) The idea of the device’s functionality is to:

  1. wake up
  2. take some sensor readings
  3. turn on wifi
  4. connect to particle
  5. publish those readings
  6. disconnect from particle
  7. turn off wifi
  8. deep sleep for some consistent amount of time (60 seconds in the code below)

The issue I’m experiencing is that it’s doing all steps consistently except step 5 - publishing. Every once in a while (read: 65%+ of the time), the Photon connects to wifi, connects to particle, ‘publishes’ with an ACK - but nothing shows within Particle Console, the webhook doesn’t fire, nothing. The Photon publishes just fine when substituting the sleep for a delay of the same time.

There’s quite a bit of serial debugging in the code below, so turn verbose_mode off if you’re feeling spammed.

    // globals area
    // ...
    bool verbose_mode = true;
    SYSTEM_MODE(MANUAL); // only turn on wifi when necessary
    // ...
    void loop() {
    // ...
        // turn on wifi
        if (verbose_mode) Serial.print("ready to publish.\n  turning on wifi... ");
        WiFi.on();
        if (verbose_mode) Serial.print("wifi on.\n  attempting to connect... ");
        WiFi.connect();

        if (WiFi.ready()) {
            if (verbose_mode) Serial.print("wifi ready.\n  force disconnecting from particle... ");
            // force disconnect from particle
            Particle.disconnect();
            waitFor(Particle.disconnected, 10000);
            if (verbose_mode) Serial.print("disconnected.\n  reconnecting to particle... ");
            // re-connect to particle
            Particle.connect();
            if (waitFor(Particle.connected, 10000) && Particle.connected()) { // make sure we're connected to particle
                Particle.process();
                if (verbose_mode) {
                    Serial.print("connected to particle.\n  SSID: ");
                    Serial.print(WiFi.SSID());
                    Serial.print("\n  IP: ");
                    Serial.println(WiFi.localIP());
                }
                // take system stats
                publish_timestamp = Time.now();
                int wifi_strength = WiFi.RSSI();
                sprintf(msg, "debugVitals,%i,%i,%i,%i,%i", publish_timestamp, test_number, wifi_strength);
                Particle.publish("DEBUG_VITALS", msg, WITH_ACK);
                Particle.process();
                delay(2000);
                Particle.process();
                if (verbose_mode) Serial.println(msg);
            } else { // never connected to particle
                WiFi.off();
                if (verbose_mode) Serial.println("  PARTICLE NOT CONNECTED");
            }
            WiFi.off();
            Serial.println("  wifi off.");
        } else { // never connected to wifi
            WiFi.off();
            if (verbose_mode) Serial.println("  WIFI NOT CONNECTED");
        }
        // go to sleep
        System.sleep(SLEEP_MODE_DEEP, 60);
    }

Am I going about this in the wrong way? Is there something silly I’ve forgotten? Thanks in advance for any and all help!

Currently you need to allow for up to 5sec after a publish before turning WiFi off.
Try extending your delay.

In one of the future releases (hopefully 0.7.0) this should be addressed

There is an open issue about this
e.g.
https://github.com/spark/firmware/issues/1165
https://github.com/spark/firmware/issues/1166

Thank you, @ScruffR! Publishing is consistent now. This implicitly means that the WITH_ACK flag doesn’t work as I suspected it would either. I’ll cross my fingers and hope that this is resolved sooner rather than later. Thanks for doing so much legwork on this issue!

1 Like