Help, one Photon crash, other in safe mode

Hi there,

Yesterday I’ve had some troubles with one of my Photons, It was is error mode (5x red light) after I have flashed new FW into the Photon. It was running for days before. With CLI tried to recover, but still not able to connect to the cloud. It fails at verify device ownership when using the app, and via the CLI: Claiming device 2f003700154734XXXXXXXXXX
Failed to claim device, server said: Device is not connected

? Would you like to continue with the information shown above? Yes

> Obtaining device information...
> Setting up device id 2f003700154734XXXXXXXXXXXX
> Requesting public key from the device...
> Setting the magical cloud claim code...
> Telling the Photon to apply your Wi-Fi configuration...
> The Photon will now attempt to connect to your Wi-Fi network...

! It doesn't look like your Photon has made it to the cloud yet.

Now with an other Photon, I have flash it with my program, and it turns into safe mode. I’ll post my code below, maybe I’m doing something wrong:

    // This #include statement was automatically added by the Particle IDE.
    #include "sunset.h"
    #include "TimeAlarms.h"
    #include <application.h>

    #define OPEN 0x00
    #define CLOSE 0x01

    PRODUCT_ID(5XXX);
    PRODUCT_VERSION(4);


    unsigned long currentTime;
    unsigned long lastTime = 0UL;

    uint8_t DirPin = 1;
    uint8_t StepPin = 2;
    uint8_t SleepPin = 3;
    uint16_t Steps = 50000;
    uint8_t StepperDelay = 2;

    uint8_t flag_request;
    uint8_t flag_status;


    bool CalcFlag;


    void setup()
    {
      pinMode(DirPin, OUTPUT);
      pinMode(StepPin, OUTPUT);
      pinMode(SleepPin, OUTPUT);
      Time.zone(1);
      Serial.begin(9600);
      //Particle.syncTime();
      
      currentTime = now();
      
      CalcFlag = TRUE;

      flag_status = EEPROM.read(0);
      
      RGB.control(true);
      if (EEPROM.read(0) == 0x01){
          RGB.color(255,0,0);
      }
      else{
          RGB.color(0,255,0);
      }
      
      // create the alarms 
      Alarm.alarmRepeat(7,30,0, OpenAlarm);  // 8:30am, alarm is in UTC
      
      Alarm.alarmRepeat(4,0,0, SyncTime);  // 8:30am, alarm is in UTC
      //Function to run from the cloud
      Particle.function("DoorControl", DoorControl);
    }

    void  loop(){  

        CalcSunsetTime();
        CheckFlagStatus();
        SetColor();
        
        //Delay of 1 second
        Alarm.delay(1000);
    }

    void CheckFlagStatus(){
        volatile uint8_t local_flag = flag_request;
        if (flag_status /= local_flag){
            //Store the open/close flag in EEPROM
            EEPROM.write(0, local_flag);
            StepperMotor(local_flag, 300);
            
        }
    }

    void SetColor(){
        if (flag_status == OPEN){
            RGB.color(0,255,0);
        }
        else if (flag_status == CLOSE){
            RGB.color(255,0,0);
        }
    }



    // functions to be called when an alarm triggers:
    void OpenAlarm(){
        DoorControl("Open");
    }

    // functions to be called when an alarm triggers:
    void CloseAlarm(){
        DoorControl("Close");
    }

    void SyncTime(){
        CalcFlag = TRUE;
    }

    // Calculate the sunset time for my location
    void CalcSunsetTime(){
        if (CalcFlag == TRUE) {
            unsigned int SunSetHours;
            unsigned int SunSetMinutes;
            /*GPS Location @home, LAT/LON*/
            double latitude = 51.77;  
            double longitude = -5.82;
            float JD;
            int SunSet;
        
            currentTime = now();
            //Since time is running in GMT, only add 1 extra hour when DST is on.
            JD = calcJD(Time.year(),Time.month(),Time.day());
            
            SunSet = calcSunsetUTC( JD,  latitude,  longitude) + 60;

            SunSetHours = SunSet/60;
            SunSetMinutes = SunSet%60;
            
            Alarm.alarmRepeat(SunSetHours,SunSetMinutes,0, CloseAlarm);
            
            Particle.publish("Sunset calculation done!");
            
            CalcFlag = FALSE;
        }
    }

    //Particle funtion to open or close the door.
    int DoorControl(String Command){
        if (Command == "Open"){
            flag_request = OPEN;
            return 1;
        }
        else if (Command == "Close"){
            flag_request = CLOSE;
            return 1;
        } 
        else return -1;
    }



    //Funtion to control the steppermotor
    void StepperMotor(int Direction, int Steps){
        int i;
        //Wake up motor controller
        digitalWrite(SleepPin, HIGH);
        
        if (Direction == 1){
            digitalWrite(DirPin, LOW);
        }
        else{
          digitalWrite(DirPin, HIGH);   
        } 
            
        for (i=0; i < Steps; i++){
            digitalWrite(StepPin, HIGH);
            delay(StepperDelay);
            digitalWrite(StepPin, LOW);
            delay(StepperDelay);
        }
        //Put motorcontroller back in sleep
        digitalWrite(SleepPin, LOW);
    }

    /*
    //This function checks the Daylight Saving Time
    bool isDST()
    { // Central European Summer Timer calculation
      int dayOfMonth = Time.day();
      int month = Time.month();
      int dayOfWeek = Time.weekday() - 1; // make Sunday 0 .. Saturday 6

      if (4 <= month && month <= 9)
      { // month between April and September definetly DST
        return true;
      }
        else if (month < 3 || 10 < month)
      { // before March or after October is definetly normal time
        return false;
      }
       // March and October need deeper examination
      boolean lastSundayOrAfter = (dayOfMonth - dayOfWeek > 24);
      if (!lastSundayOrAfter)
      { // before switching Sunday
        return (month == 10); // October DST will be true, March not
      }
      
      if (dayOfWeek)
      { // AFTER the switching Sunday
        return (month == 3); // for March DST is true, for October not
      }
      
        int secSinceMidnightUTC = now() % 86400;
      boolean dayStartedAs = (month == 10); // DST in October, in March not
                                            // on switching Sunday we need to consider the time
      if (secSinceMidnightUTC >= 1 * 3600)
      { // 1:00 UTC (=2:00 CET/3:00 CEST)
        return !dayStartedAs;
      }

      return dayStartedAs;
    }
    */

You need to ensure your divisions will never cause a DIV/0 exception otherwise you’ll get a usage fault (SOS+5)

Especially this instruction looks wrong

 if (flag_status /= local_flag){

I guess this should rather be a == or != and not a div-assign /= (which probably causes your usage fault)

BTW, do use D1, D2, D3 for pin assignment - anonymous numbers are so Arduino :wink:

Thank you for your reply.

That was a very stupid mistake. It must be a != instead of /= :astonished:
The /= is a not equal in VHDL, what I write for my daily job :slight_smile:

Hopefully I can get my crashed Photon back online. any suggestions?
Now it is flashing blue constantly, also after power up. setup via CLI or app does not help.

Flashing blue indicates missing WiFi credentials which enters Listening Mode to set new creds.

Yes, but when I do that, he completes all the steps, and fails at "verify device ownership"

You don’t need to reclaim the device.

Just use particle serial wifi to reload the credentials instead of going through the whole first-time setup.

Thank you, I'll try it in the evening again. Yesterday I get the message:

Obtaining device information...
> Setting up device id 2f003700154734xxxxxxxxxx
> Requesting public key from the device...
> Setting the magical cloud claim code...
> Telling the Photon to apply your Wi-Fi configuration...
> The Photon will now attempt to connect to your Wi-Fi network...

! It doesn't look like your Photon has made it to the cloud yet.

I’d also avoid all the automatic detection stuff and go all manual.

Okay, I will try this evening. Thanks for helping.