# Using attachInterrupt() with WiFi.listen()

**URL:** <https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019>\
**Category:** Troubleshooting\
**Created:** [October 30, 2015, 10:50pm UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019 "2015-10-30T22:50:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikelo](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mikelo](https://community.particle.io/u/mikelo)\
**Post date:** [October 30, 2015, 10:50pm UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/1 "2015-10-30T22:50:04Z")

</div>

hi I am using an external button to get photon into setup mode to setup the WiFi. I wonder how to make WiFi.listen( ) to work with interrupt or any other way to get photon into listening mode before the LED is in breathing Cyan. I have tried attachInterrupt(D6, listen, LOW); but nothing is working. I also use WiFi::listen or &WiFi::listen, but none of them is working.

---

<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 31, 2015, 8:43pm UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/2 "2015-10-31T20:43:14Z")

</div>

First, it would be `attachInterrupt(D6, listen, FALLING);` not `LOW`.  
Next you’d set `pinMode(D6, INPUT_PULLUP);` before `attachInterrupt()` to avoid wrong triggers due to floating signal on open button.  
Next, you need to write your own ISR which calls `WiFi.listen()` and not use `WiFi.listen` as ISR direct.

But if you are running in `SYSTEM_MODE(AUTOMATIC)` (=default) then your `setup()` where you would attach your button would not be executed before breathing cyan anyhow (unless you use [`SYSTEM_THREAD(ENABLED)`](https://docs.particle.io/reference/firmware/photon/#system-thread) or [`STARTUP()`](https://docs.particle.io/reference/firmware/photon/#startup-)).

* * *

You might want to look into other [`SYSTEM_MODE`s](https://docs.particle.io/reference/firmware/photon/#system-modes).

---

<div class="post-metadata">

**Author:** ![mikelo](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mikelo](https://community.particle.io/u/mikelo)\
**Post date:** [November 11, 2015, 12:42am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/3 "2015-11-11T00:42:13Z")

</div>

Thank you for more clear explanation.  
I am using SYSTEM\_THREAD(ENABLED); If I see any light before the breathing cyan that should be considered as the programming is running in setup( ). Am I correct about it?

I have try many times to get it into Setup mode before the LED get into breathing Cyan. Here is my code I don’t know I have done anything wrong with it.

```cpp
void buttonHoldToSetupMode()
{

	buttonEvent = button.checkButton();
	if(button.isHold(buttonEvent) || button.isLongHold(buttonEvent))
	{
		WiFi.listen();
	}
}

void goToSetupMode()
{
	pinMode(D6,INPUT_PULLUP);
	attachInterrupt(D6,toSetupMode,FALLING);
	buttonHoldToSetupMode();
}
STARTUP(goToSetupMode);
void setup(void)
{
	//getIntoSetupMode();
	goToSetupMode();
}

```

BTW, after I get into listening mode which is listening flashing blue for a several second. The system get into breathing blue. What is breathing blue mean?

---

<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:** [November 11, 2015, 9:01am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/4 "2015-11-11T09:01:18Z")

</div>

> [@mikelo](#):
>
> If I see any light before the breathing cyan that should be considered as the programming is running in setup( ).

Nope, the RGB-LED is under system control, unless your code takes over by means of `RGB.control(true);`. So any light you see is by the system.  
If you want an indication of your oen code running you can use the little blue D7 LED or take over RGB control.

BTW: Why do you do `goToSetupMode()` twice? (`STARTUP` & `setup()`)

Could you also show the declaration of `button` and `buttonEvent`?

For breathing blue (or any other issue that has catchy search terms) you could have used the forum search, which e.g came up with this  
[After some time Photon breathes blue](https://community.particle.io/t/after-some-time-photon-breathes-blue/16073)

---

<div class="post-metadata">

**Author:** ![mikelo](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mikelo](https://community.particle.io/u/mikelo)\
**Post date:** [November 12, 2015, 1:04am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/5 "2015-11-12T01:04:49Z")

</div>

I just realized an issue. Does Photon has specific pins that can work with external interrupt? In this case I am trying to use D6 pin to do the interrupt but I didn’t realized that external Interrupt should highly work with certain pins.

---

<div class="post-metadata">

**Author:** ![mikelo](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mikelo](https://community.particle.io/u/mikelo)\
**Post date:** [November 12, 2015, 1:07am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/6 "2015-11-12T01:07:18Z")

</div>

The reason I do goToSetupMode() in STARTUP( ) and setup( ) is because I need to make sure the user can get the photon into setup mode when they plug in their device.

---

<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:** [November 12, 2015, 6:22am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/7 "2015-11-12T06:22:29Z")

</div>

> [@mikelo](#):
>
> I need to make sure the user can get the photon into setup mode when they plug in their device.

Yes, but that can be done by _ **just** _ calling it via [`STARTUP()`](https://docs.particle.io/reference/firmware/photon/#startup-).

And if you have a look in the [docs](https://docs.particle.io/reference/firmware/photon/#attach-) you'll also find which pins can be used.

---

<div class="post-metadata">

**Author:** ![mikelo](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mikelo](https://community.particle.io/u/mikelo)\
**Post date:** [November 13, 2015, 12:04am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/8 "2015-11-13T00:04:16Z")

</div>

The following code is how I test it. When I let the i \> 100. The WiFi.listen() will get Photon into listening/setup mode when the chip start. However, if I set i \> 200 it won’t get into listening/setup mode until the Photon is in breathing cyan. Do you know what happen to this?

void setup(void)  
{  
int i; i=0;

```
while(1){
	i++;
	// getIntoSetupMode();
	// goToSetupMode();
	if(i>100){
		Serial.println("Jolt listen");
		WiFi.listen();
	}else{
		Serial.println("Jolt");
	}
}

```

}

my button object is copied from the Internet. It work with Arduino, so I modified it to make it work with Photon. Do you want me to post the code?

---

<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:** [November 13, 2015, 12:42am UTC](https://community.particle.io/t/using-attachinterrupt-with-wifi-listen/17019/9 "2015-11-13T00:42:26Z")

</div>

I’m not quite sure what problems you are seeing, or what you exactly need

- do you want to enter Listening Mode at any time?
- Or only during a short periode at the beginning?
- Have you considered `SYSTEM_MODE(SEMI_AUTOMATIC)`?

But give this a try (tested and working - I think ;-))

```cpp
SYSTEM_MODE(SEMI_AUTOMATIC)
STARTUP(attachButton())

void attachButton()
{
  pinMode(D6, INPUT_PULLUP);
  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH); // inidcate that from now on you can use the button
  attachInterrupt(D6, toSetupMode, FALLING);
  Particle.connect();
}

void toSetupMode()
{
  if (digitalRead(D7)) // allow control over the interrupt and button debounce
  {
    digitalWrite(D7, LOW); // indicate not to 
    WiFi.listen();
  }
}

uint32_t msTimeout;

void setup()
{
  msTimeout = millis();
}

void loop()
{
  if (millis() - msTimeout > 15000) // 15sec after setup()
    digitalWrite(D7, LOW); // prevent button press from entering Listening Mode
}

```
