Advertisement
Guest User

Cloud function and deep sleep

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