Using attachInterrupt() with WiFi.listen()

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.

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) or STARTUP()).


You might want to look into other SYSTEM_MODEs.

1 Like

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.

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?

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

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.

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.

Yes, but that can be done by just calling it via STARTUP().

And if you have a look in the docs you'll also find which pins can be used.

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?

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 ;-))

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
}