I get a "usage fault" SOS code for commenting out a serial print?

I’m new to a lot of this stuff, so my project actually started with someone else’s example code. That example code included some serial.print lines that I don’t actually need because I’m not connected via USB and I’m never looking at the serial output. So I thought I could just comment them out. But doing so makes my photon boot into a flashing red SOS code followed by 5 red flashes to indicate “usage fault”. Even though it compiled just fine. Just for not using the serial print?

Here’s the earliest version of my code that is doing this to me:

    #include "Ubidots/Ubidots.h"
    #include "Adafruit_BME280/Adafruit_Sensor.h"
    #include "Adafruit_BME280/Adafruit_BME280.h"
    
    #define BME_SCK D4
    #define BME_MISO D3
    #define BME_MOSI D2
    #define BME_CS D5
    #define SEALEVELPRESSURE_HPA (1013.25)
    
    #define TOKEN "ix9hooYdMoBXnxoCNP65gUTNKjBG1S"  // Put here your Ubidots TOKEN
    
    Ubidots ubidots(TOKEN);
    
    
    Adafruit_BME280 bme; // I2C
    //Adafruit_BME280 bme(BME_CS); // hardware SPI
    //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);
    
    void setup() {
      Serial.begin(9600);
      Serial.println(F("BME280 test"));
    
      if (!bme.begin()) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
      }
    }
    
    void loop() {
            delay(20000);
              RGB.control(true);
    float temp1;
    temp1 = (bme.readPressure() / 100.0F);
    float temp2;
    temp2 = (bme.readTemperature() - 1);
        Serial.println();
        
        ubidots.add("Temperature", temp2);
        ubidots.add("Humidity", bme.readHumidity());
        ubidots.add("Pressure", temp1);
        ubidots.sendAll();
    
    }

Sorry about the crappy tab formatting, again I’m new to this. If I comment out every line beginning with “serial”, the three in the void setup and the one in the void loop, I get the red SOS. My code won’t work until I add them back. That doesn’t seem normal to me, seeing as how flashing a completely blank code with absolutely nothing but an empty void setup and void loop works perfectly fine. And I’m pretty sure nothing else in my included code is relying on the serial output. I have since greatly expanded this code to output to a little TFT LCD screen and everything, but the same issue remains - as soon as I remove the serial lines, it all breaks down as an SOS.

I’ve since moved from the Web IDE to the Local IDE, so I could write my own libraries, but the issue remains there too. Is this normal behaviour? It’s not a huge pressing issue as the code works perfectly fine with the serial print lines included, it’s more of a curiosity at this point.

SOS+5 usually indicates a DIV/0 exception, which I can’t see, but it might happen inside a library.

1 Like

Have you tried all the various combinations of commenting out 1, 2, or 3 of the four statements to see if all 4 really need to be commented out to see the problem (or adding them back one at a time to see if one addition will fix the problem)?

BTW, try leaving Serial.begin(9600) in there.

And what device and system version are you targeting?

In earlier versions and on the Core you got SOS when trying to Serial.print() (which might also happen inside any library) without a proper Serial.begin() call.