Photon flashing red!

Having an issue with a photon flashing red when it executes the code.

Code:

    // This #include statement was automatically added by the Particle IDE.
    #include <Adafruit_BMP280.h>

    // IMPORTANT: When including a library in a firmware app, a sub dir prefix is needed
    // before the particular .h file.

    #include "Adafruit_Sensor.h"
    #include "Adafruit_BMP280.h"

    #define BMP_CS A2
    #define BMP_SCK A3
    #define BMP_MISO A4
    #define BMP_MOSI A5 

    //Adafruit_BMP280 bmp; // I2C
    Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
    //Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

    Timer timer(5000, checkEnvironment);

    // Update to reflect current pressure at sea level
    float seaLevelhPa = 1035.7;

    void setup() {

      Particle.publish("DEBUG", "starting...");

      if (bmp.begin()) {
        Particle.publish("DEBUG", "starting the environment timer...");
        timer.start();
      }
      else {
        Particle.publish("WARN", "Could not find a valid BMP280 sensor, check wiring!");
      }

      Particle.publish("DEBUG", "started!");
    }

    void loop() {

    }


    void checkEnvironment() {
      Particle.publish("DEBUG", "checking the environment...");


      Particle.publish("environment/temperature", String(bmp.readTemperature()));
      //Particle.publish("environment/temperature", String(temp);
      
      Particle.publish("environment/pressure", String(pToHg(bmp.readPressure())));
      Particle.publish("environment/altitude", String(bmp.readAltitude(seaLevelhPa)));
    }

    float cToF(float c) {
      return c * 9/5 + 32;
    }

    float pToHg(float p) {
      return p/3389.39;
    }

Console:

Video of red flashes:

The red blinking is an SOS message https://docs.particle.io/guide/getting-started/modes/electron/#red-flash-sos

The number of blinks after the SOS signal is important. I counted 11 before the video cuts off which corresponds to “invalid case”

Also the battery charging led is blinking which may or may not be an issue, but it can be disabled if you are not using the battery by calling PMIC().disableCharging();
https://docs.particle.io/datasheets/electron-(cellular)/electron-datasheet/#led-status

Shucks, video may be short, I think it’s actually 13? What does that mean, I dont see 13 in the list.

13 is stack overflow. I can’t see how you would be overflowing the stack from this sketch though…

1 Like

@r0b0tn1k, Software Timers have a small stack and should be treated as Interrupt Service Routines - short, fast and without complex function calls like Particle.publish(). Set a flag in your timer and test it in loop() to then trigger checkEnvironment().

Another approach is to use a millis() non-blocking timer in loop() to call checkEnvironment() every 5000ms. You don’t need a Software Timer for that. You may also want to test your code by outputting to the serial port for debugging instead of using Particle.publish() to make sure everything works. Again, the same rules apply for the Software Timer code.

3 Likes

Yes, it was an example from the BMP library, was wondering the same thing.

Ok, I’m not much of an expert, so I’ll try cramming as much of the code in the loop section and setting a 5 second delay and hope for the best.

@r0b0tn1k, another thing is that you might want to build a single c-string (array of chars) using snprintf() and do a single publish instead of 4 in a row.

1 Like

Awesome! I didn't know Software Timers had their own stack.

@r0b0tn1k you can use something like this in your loop instead of the software timer

static unsigned int previousCheckEnvironment;
if(millis() - previousCheckEnvironment > 5000){
  checkEnvironment();
  previousCheckEnvironment = millis();
}

@bveenema, think of software timers as a set of daisy-chained timers running on a single, separate FreeRTOS task. If one timer takes too long or locks up, the other timers will not get any runtime! The stack size is 512 bytes so Timer code needs to be lean and simple. :wink:

1 Like

I"m actually getting an error if I put in that command:

PMIC().disableCharging();

Does this go in the code? It says firmware command? Do I have to embed the Asset tracker library to make it work?

There is apparently more to the PMIC library.
https://docs.particle.io/reference/firmware/electron/#pmic-power-managment-ic-
https://docs.particle.io/datasheets/electron-(cellular)/electron-datasheet/#pmic-power-management-integrated-circuit-

I haven’t used it myself so I’m not too much help.

1 Like

Usually the error message gives away the reason, hence showing it would be advisable.

But I guess you need to drop the first set of parenthesis. PMIC is an object and can’t be called like a function.
Try PMIC.disableCharging() instead.