Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WATCHDOG_TIMEOUT 10000 //10 000 ms for watchdog to reset the device in case of cloud failure
- #define INTERVAL 30
- #define EVENT_NAME "test_Safe"
- bool eventPending; // signal for receiving feedback from cloud on event we sent
- // Handler for cloud event
- void eventHandler(const char *event, const char *data)
- {
- eventPending = false;
- }
- // Cloud function to send device to Safe Mode for user firmware upgrade
- int gotoSafeMode(String command){
- Particle.publish(EVENT_NAME,"Going to Safe Mode");
- Particle.process();
- delay(3000);
- Particle.process();
- System.enterSafeMode();
- }
- void setup()
- {
- Particle.subscribe(EVENT_NAME, eventHandler); // set up validaton mechanism
- eventPending = true; // initialize flag for it
- Particle.function("safe", gotoSafeMode); // set up cloud function for DFU
- }
- void loop()
- {
- Particle.connect(); // reconnect to cloud (unnecessary)
- if(!Particle.connected()){ // maybe not needed, but to be on safe side
- RGB.color(255,0,0); // show user red light indicator on no-connection state
- Particle.process(); // this probably should never happen, maybe..
- }
- RGB.control(true); // take over rgb led control
- RGB.color(0, 255, 0); // show user green light indicator on ok state
- Particle.publish(EVENT_NAME, "Plop");
- long watchDogTimer = millis(); // establish watchdog to verify cloud got the data
- while(eventPending){ // wait for signal that we received the event sent
- RGB.color(255,255,0); // show yellow indicator on waiting
- Particle.process(); // while waiting, give OS time to process
- if(millis() > watchDogTimer+WATCHDOG_TIMEOUT){ // watchdog resets system if takes too long
- System.reset();
- }
- }
- delay(2000);
- System.sleep(SLEEP_MODE_DEEP, INTERVAL); // we get this far. DEEP SLEEP resets the device.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement