Shutdown after 2 hrs when powered from battery? [SOLVED]

I was running my Photon with a sketch to scan for WiFi APs and send the result to a server (this sketch) and running it off a 4000mAh power pack. After almost exactly 2 hours (2 hrs 33 seconds), the server stopped receiving signals from the Photon and I noticed in the morning that it was shut down (no lights). The battery still had >80% of the life left, so I unplugged it from the battery and plugged it back in, and the Photon just starts right back up again - so everything is fine except for the random shutdown.

For what reason does a Photon automatically shut off? And, is there any indication from my setup that could have caused this?

I was thinking maybe it got too hot, or maybe the battery voltage dipped below 3.3V somehow, or maybe it wasn’t able to wake up from the System.Sleep() so it just shut down? I have no clue, I’d be happy to hear any suggestions!

Thanks.

@schollz, how is your battery connected to the Photon? Most battery packs will shut down when too little current is drawn from their port. Putting your Photon to sleep may cause this condition. Can you post your code?

1 Like

The battery is connected via the micro USB. Its a Android phone battery so I don’t know the internal workings so its possible it could have shutdown temporarily, but still the Photon never woke up over the 8 hours after its possible shutdown.

Here’s my code:

#include "application.h"
#include "HttpClient/HttpClient.h"

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Content-Type", "application/json" },
    { "Accept" , "application/json" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

// switch to activate different modes
unsigned int SLEEP = 0;

void button_handler(system_event_t event, int duration, void* )
{
    if (!duration) { // just pressed
        RGB.control(true);
        if (SLEEP == 0) {
            RGB.color(255,0,0);
            SLEEP = 1; // sleep mode on
        } else if (SLEEP == 1) {
            RGB.color(0,255,0);
            SLEEP = 2; // undefined mode
        } else {
            RGB.color(0,0,255);
            SLEEP = 0;
        }
    }
    else {    // just released
        RGB.control(false);
    }
}

void setup() {
    Serial.begin(9600);
    System.on(button_status, button_handler);
}

void loop() {
    if (SLEEP == 1) {
        RGB.color(255,0,0);
    }
    if (SLEEP == 2) {
        RGB.color(0,255,0);
    }

    if (nextTime > millis()) {
        return;
    }

    // Serial.println();
    // Serial.println("Application>\tStart of Loop.");

    request.hostname = "ml2.internalpositioning.com"; 
    request.port = 80;
    request.path = "/track";


    request.body = "{\"group\":\"GROUPNAME\",\"username\":\"USERNAME\",\"location\":\"LOCATIONNAME\",\"wifi-fingerprint\":[";
    WiFiAccessPoint aps[20];
    int found = WiFi.scan(aps, 20);
    for (int i=0; i<found; i++) {
        WiFiAccessPoint& ap = aps[i];
        char mac[17];
        sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x",
         ap.bssid[0] & 0xff, ap.bssid[1] & 0xff, ap.bssid[2] & 0xff,
         ap.bssid[3] & 0xff, ap.bssid[4] & 0xff, ap.bssid[5] & 0xff);
        request.body = request.body + "{\"mac\":\"" + mac + "\",";
        float f = ap.rssi;
        String sf(f, 0);
        request.body = request.body + "\"rssi\":" + sf + "}";

        if (i < found -1 ) {
            request.body = request.body + ",";
        }
    }
    request.body = request.body + "]}";

    // Serial.println(request.body);
    http.get(request, response, headers);

    // Serial.print("Application>\tResponse status: ");
    // Serial.println(response.status);

    // Serial.print("Application>\tHTTP Response Body: ");
    // Serial.println(response.body);

    nextTime = millis() + 3000;
    if (SLEEP == 1) {
        System.sleep(3);
    } else {
        delay(3000);
    }
}

@schollz, what do you expect System.sleep() to do? I assume you read this in the docs:

System.sleep(long seconds) does NOT stop the execution of application code (non-blocking call). Application code will continue running while the Wi-Fi module is in standby mode.

I didn’t realize its non-blocking! Thanks.
Do you think its not worth it to use System.sleep() for saving battery in this case? I thought it would save on battery to turn off the WiFi, since I only need to make calls every 5-10 seconds and the only application code involves scanning WiFi / transmitting over WiFi. Actually, I was using the battery to test this when the Photon shutdown - I was going to see how long the same battery would last doing either a simple delay (SLEEP==0) or using the System sleep (SLEEP==1).

@schollz, turning wifi on and off in such a short interval won’t gain you much due to the overhead of reconnecting to wifi every time. Deep sleep would give you the best power conservation. Each time it wakes, it resets the processor so you have to write you code accordingly. Any reason you need a 5-10 sec interval?

@peekay123 Yes, the 5-10 sec interval is important so I can do somewhat realtime positional tracking (by classifying the WiFi signals on the server), so deep sleep is not really an option for this application. I think I’ll try my little experiment again with the sleep disabled and will update how it goes :smile:

2 Likes

So I tested again, without the System.sleep(), and I got strangely similar results. The Photon turned off after 2 hours, 3 minutes and 40 seconds (versus 2 hours, 33 seconds with System.sleep() enabled)…so now I’m thinking that this is because of my Motorola P4000 power pack - maybe it has some internal circuit that shuts itself off if the connected peripheral draws too little power for ~2 hours?

I don’t know what’s going on. I’ll try doing the control experiment - I’ll run the Photon off wall supply to see if it lives longer than 2 hours…then I’ll try a different type of power pack.

1 Like

In the end, it seems that the 2 hour shutoff was due to the Motorola P4000 power pack and not due to the Photon. I’ve been running the Photon for >12 hours using the same sketch but a different power pack - the Inisgna NS-MB2601 with absolutely no problem.

2 Likes