Advertisement
Guest User

Deep Sleep and safe mode

a guest
May 16th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. #define WATCHDOG_TIMEOUT 10000  //10 000 ms for watchdog to reset the device in case of cloud failure
  2. #define INTERVAL 30
  3. #define EVENT_NAME "test_Safe"
  4.  
  5. bool eventPending;      // signal for receiving feedback from cloud on event we sent
  6.  
  7. // Handler for cloud event
  8. void eventHandler(const char *event, const char *data)
  9. {
  10.     eventPending = false;
  11. }
  12.  
  13. // Cloud function to send device to Safe Mode for user firmware upgrade
  14. int gotoSafeMode(String command){
  15.     Particle.publish(EVENT_NAME,"Going to Safe Mode");
  16.     Particle.process();
  17.     delay(3000);
  18.     Particle.process();
  19.     System.enterSafeMode();    
  20. }
  21.  
  22.  
  23. void setup()
  24. {
  25.     Particle.subscribe(EVENT_NAME, eventHandler);   // set up validaton mechanism
  26.     eventPending = true;                            // initialize flag for it
  27.     Particle.function("safe", gotoSafeMode);        // set up cloud function for DFU
  28. }
  29.  
  30.  
  31. void loop()
  32. {
  33.  
  34.     Particle.connect();             // reconnect to cloud (unnecessary)
  35.     if(!Particle.connected()){      // maybe not needed, but to be on safe side
  36.         RGB.color(255,0,0);         // show user red light indicator on no-connection state
  37.         Particle.process();         // this probably should never happen, maybe..
  38.     }
  39.  
  40.     RGB.control(true);              // take over rgb led control
  41.     RGB.color(0, 255, 0);           // show user green light indicator on ok state
  42.  
  43.     Particle.publish(EVENT_NAME, "Plop");
  44.  
  45.     long watchDogTimer = millis(); // establish watchdog to verify cloud got the data
  46.     while(eventPending){              // wait for signal that we received the event sent
  47.         RGB.color(255,255,0);         // show yellow indicator on waiting
  48.         Particle.process();         // while waiting, give OS time to process
  49.         if(millis() > watchDogTimer+WATCHDOG_TIMEOUT){ // watchdog resets system if takes too long
  50.                 System.reset();
  51.         }
  52.     }
  53.     delay(2000);
  54.     System.sleep(SLEEP_MODE_DEEP, INTERVAL);     // we get this far. DEEP SLEEP resets the device.
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement