# Watchdogs on the Photon

**URL:** <https://community.particle.io/t/watchdogs-on-the-photon/18254>\
**Category:** Libraries\
**Created:** [December 14, 2015, 12:50am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254 "2015-12-14T00:50:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ampunix](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ampunix/32/8050_2.png) [@ampunix](https://community.particle.io/u/ampunix)\
**Post date:** [December 14, 2015, 12:50am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/1 "2015-12-14T00:50:29Z")

</div>

Hi,

we just published a new library for using the two HW watchdogs on the photon! For details look on on [our blog](http://stasheddragon.com/2015/watchdog-library-for-particle-photon/) or on the [GitHub repository](https://github.com/raphitheking/photon-wdgs).

Live long, and prosper!

---

<div class="post-metadata">

**Author:** ![peter\_a](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@peter\_a](https://community.particle.io/u/peter_a)\
**Post date:** [December 14, 2015, 9:43am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/2 "2015-12-14T09:43:34Z")

</div>

Your watchdog library may just be a nice fix for a problem I’m having.  
What should happen in my program is :-  
Wake up from 4 and half minutes of deep sleep , keep checking the clock to see if the minutes is now MOD 5 or 0, read a load of sensor data , send it to my server and back to sleep again , but a i2c sensor is fleezing my code.  
So I m having to do a manual reset every few hours or days

What happens on a fleeze , a reset ?

---

<div class="post-metadata">

**Author:** ![ampunix](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ampunix/32/8050_2.png) [@ampunix](https://community.particle.io/u/ampunix)\
**Post date:** [December 14, 2015, 9:52am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/3 "2015-12-14T09:52:55Z")

</div>

Yeah, your code needs to call the `tickle()` function within a configured time period so the watchdogs will reset properly. If your code breaks/halts/gets stuck, the watchdogs will reset. Using deep sleep and the independent watchdog (IWDG) won’t work as far as I know (runs on own RC, cannot be stopped). So you can only depend on the window watchdog (WWDG). It is possible to pause and resume the watchdog, [we already created and enhancement issue for that](https://github.com/raphitheking/photon-wdgs/issues/1), and will implement it within the next week or so.

---

<div class="post-metadata">

**Author:** ![specialcircumstances](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@specialcircumstances](https://community.particle.io/u/specialcircumstances)\
**Post date:** [February 3, 2016, 7:54pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/4 "2016-02-03T19:54:06Z")

</div>

Very useful. Thanks folks!

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [February 12, 2016, 7:38am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/5 "2016-02-12T07:38:02Z")

</div>

@ampunix Did the pause and resume the watchdog feature even get tested successfully and added to your library?

I want to use the Watchdog to reset the Photon or P1 if it ever hangs or does not come out of deep sleep.

How exactly could the Window Watchdog be used to do that?

Any info is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![frlobo](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/frlobo/32/1279_2.png) [@frlobo](https://community.particle.io/u/frlobo)\
**Post date:** [February 12, 2016, 8:55am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/6 "2016-02-12T08:55:59Z")

</div>

Also interested in a watchdog to reset in case of deep sleep wake up failure…

Any ideas on how this should be implemented would be welcomed!

---

<div class="post-metadata">

**Author:** ![developer\_bt](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/developer_bt/32/1311_2.png) [@developer\_bt](https://community.particle.io/u/developer_bt)\
**Post date:** [March 12, 2016, 5:54pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/7 "2016-03-12T17:54:13Z")

</div>

Hello,  
Does the library is designed to works only with Photon?  
I imported the example in WebIDE from libraries.  
While compiling the example for Electron I get the following error.

![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/8/89c35a4ecefbc8d70ee0e30f9c41183c28b63046.JPG)

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 12, 2016, 7:47pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/8 "2016-03-12T19:47:33Z")

</div>

First, how have you imported the sample? By use of the USE THIS EXAMPLE button?  
Since when I do this, I actually get a missing `#include "SparkIntervalTimer/SparkIntervalTimer.h"`.  
And after importing that library some system files are not found for the Electron but (revolving around `stm32f2xx_wwdg.h` and `STM32F2xx_Peripheral_Libraries.a`).  
But for the Photon they seem to be present.

So maybe @mdma might be able to help with that.

---

<div class="post-metadata">

**Author:** ![flavio.ferrandi](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@flavio.ferrandi](https://community.particle.io/u/flavio.ferrandi)\
**Post date:** [October 21, 2016, 1:52pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/9 "2016-10-21T13:52:21Z")

</div>

Hi! I’m trying this library but I have problem with OTA…  
Do you have the same problem? How do you fix it?  
I try to start the update via curl but I never receive the start update event, but only a timeout after a few time.  
Thanks, Flavio

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 21, 2016, 5:04pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/10 "2016-10-21T17:04:29Z")

</div>

> [@flavio.ferrandi](#):
>
> I'm trying this library but I have problem with OTA.... Do you have the same problem?

That'd be hard to tell, since we don't know _ **what** _ your problem with OTA is.

---

<div class="post-metadata">

**Author:** ![flavio.ferrandi](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@flavio.ferrandi](https://community.particle.io/u/flavio.ferrandi)\
**Post date:** [October 21, 2016, 5:22pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/11 "2016-10-21T17:22:38Z")

</div>

I’m sorry, you are right 😃 I can’t flash the photon via OTA. Flashing always end up with time out.  
I can flash it only in DFU mode. If I remove the watchdog I can flash via OTA again.  
It seems that the library doesn’t catch the updatesPending, or the disableUpdates doesn’t work, so it doesn’t disable the timer…  
Thank, Flavio

---

<div class="post-metadata">

**Author:** ![flavio.ferrandi](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@flavio.ferrandi](https://community.particle.io/u/flavio.ferrandi)\
**Post date:** [October 22, 2016, 8:48am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/12 "2016-10-22T08:48:56Z")

</div>

It seems that the problem is if(System.updatesPending()), not called on the last firmware.  
I fixed in this way:  
removed this piece of code from \_tickleWDGs:

```
// deactivate WWDG if OTA updates pending
If(System.updatesPending()) {
   if(PhotonWdgs::_wwdgRunning) {
       WWDG_DeInit();
   }
   System.enableUpdates();
   PhotonWdgs::_wdgTimer.end();
}

```

Added this line at the bottom of begin()

```
System.on(firmware_update_pending, handle_update);

```

Added this method:

```
void handle_update(system_event_t event, int param)
{
    Serial.println("UPDATE READY WDGS");
    if(PhotonWdgs::_wwdgRunning) {
        WWDG_DeInit();
    }
    System.enableUpdates();
    PhotonWdgs::_wdgTimer.end();
}

```

It seems to work, let me know if this is working also for you.  
Flavio

---

<div class="post-metadata">

**Author:** ![NielsClausen](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nielsclausen/32/15091_2.png) [@NielsClausen](https://community.particle.io/u/NielsClausen)\
**Post date:** [February 12, 2018, 4:10pm UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/13 "2018-02-12T16:10:52Z")

</div>

Worked for me, as per [https://github.com/raphitheking/photon-wdgs/issues/2](https://github.com/raphitheking/photon-wdgs/issues/2)

---

<div class="post-metadata">

**Author:** ![eLumaLite](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@eLumaLite](https://community.particle.io/u/eLumaLite)\
**Post date:** [September 13, 2018, 7:59am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/14 "2018-09-13T07:59:51Z")

</div>

> [@flavio.ferrandi](#):
>
> Added this line at the bottom of begin()
> 
> System.on(firmware\_update\_pending, handle\_update);

Hi flavio, I must be doing something wrong. Where do you add that line exactly?

---

<div class="post-metadata">

**Author:** ![flavio.ferrandi](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@flavio.ferrandi](https://community.particle.io/u/flavio.ferrandi)\
**Post date:** [September 13, 2018, 10:53am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/15 "2018-09-13T10:53:25Z")

</div>

```auto
void PhotonWdgs::begin(bool _enableWwdg,bool _enableIwdg,unsigned long _timeout, TIMid id)
{
    if(!_enableWwdg && !_enableIwdg) {
        // nothing to do ...
        return;
    }
    PhotonWdgs::_aliveCount = 0;
    PhotonWdgs::_timeoutval = _timeout / 10;

    RCC_LSICmd(ENABLE); //LSI is needed for Watchdogs

    PhotonWdgs::_wdgTimer.begin(PhotonWdgs::_tickleWDGs, 20, hmSec, id);
    // OTA updates won't work with watchdog enabled
    System.disableUpdates();
    PhotonWdgs::_wwdgRunning = _enableWwdg;
    if(_enableWwdg) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
        WWDG_SetPrescaler(WWDG_Prescaler_8);
        WWDG_SetWindowValue(0x7F);
        WWDG_Enable(0x7F);
    }
    PhotonWdgs::_iwdgRunning = _enableIwdg;
    if(_enableIwdg) {
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_SetPrescaler(IWDG_Prescaler_256);
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_SetReload(0xFFF);
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
        IWDG_Enable();
    }
    System.on(firmware_update_pending, handle_update);
    System.on(setup_begin, handle_setup_begin);
    System.on(setup_end, handle_setup_end);
}

```

---

<div class="post-metadata">

**Author:** ![eLumaLite](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@eLumaLite](https://community.particle.io/u/eLumaLite)\
**Post date:** [October 9, 2018, 1:40am UTC](https://community.particle.io/t/watchdogs-on-the-photon/18254/16 "2018-10-09T01:40:21Z")

</div>

Thank you!
