Interrupt pin causing solid RED LED

Hi -

I have red through some posts but could not find one similar in cause to this. I know it is not the Interrupt pin itself causing trouble as I have used it before, but probably the way it is implemented in my code :slight_smile:

Here is a link to the .ino file

When I uncomment the following lines, it causes solid RED led as soon as I trigger D7 which is opening the relay. the device then disconnects and can only be restored by manually turning the power off/on.

pinMode(A2, INPUT);      //Interrupt pin - 4k7 series resistor  CURRENTLY RESULTS IN HARD FAULT
attachInterrupt(A2, Listen_Mode, FALLING);

The Listen mode is fairly simple;

void Listen_Mode() {        //  Enter Listem mode when interupt is called
     WiFi.listen();
}

I have no idea where the problem lies, but as soon as I disable the Interrupt by commenting out either of the two lines, the device works as expected.

ps: I know I still need to write a function for the two startup commands as this is not ideal, but I have disable one and it has no affect.

Many thanks in advance!
Friedl.

You cannot do that.
WiFi.listen() is a very heavy call and ISRs are absolutely not allowed to be that heavy.

You should rather set a global flag inside the ISR and check the state of the flag in loop() to engage LM when needed.

1 Like

@ScruffR -

Hhmm, ok, let me google and YouTube all of that, learn and try to implement :smile:

Will do my best.

Thanks!!

HI @ScruffR -

Sorry if I am getting this wring still, but I have implemented some code in the following way as I am not sure how to implement

So I created:

void Listen_Mode() {        //  Enter Listem mode when interupt is called

int Atwo = analogRead(A2);
          if (Atwo < 50) 
            WiFi.listen();

Particle.publish("A-two", String(Atwo), PRIVATE);          // debug

Then in void loop(); simply called;

void loop() {
    
    Blynk.run();
    CS();
    temperature();
    connect();
    Listen_Mode();
}

I have commented out the Interrupt section but here lies my problem. It seems if BLYNK does not authenticate with this server, it blocks the rest of the code from running. Which leads me to a problem I had before when attempting all of this with code;

If the device has connected successfully and thereafter it gets disconnected, the Listen_Mode implementation works like a charm. IF however the device has been powered down or restarts and cannot connect to WiFi... It never enters the loop, despite of SYSTEM_MODE(SEMI_AUTOMATIC);. I do not have threads enabled. I tried it, but had some undesired 'consequences' :relaxed:

I will try to figure out how to proceed and would appreciate some further advice if you can spare the time.

Many Thanks!
Regards, Friedl.

You must avoid calling any Blynk stuff as long you have not established at least a WiFi connection otherwise these calls will fail - often note gracefully.

1 Like

Thanks @ScruffR ā€¦

I will implement in the morning, for now I need to get studying for Calculus exam as this kept me busy all day :see_no_evil:

Regards, Friedl.

Good luck! :+1:

1 Like

Thanksā€¦ not sure for which I need it more, Calculus or these damn interrupts lol

2 Likes

HI @ScruffR -

I really am supposed to be studying but have this nasty habit of not being able to leave unsolved problems at that :see_no_evil:

So what I have done now is;

  1. Moved as much of the BLYNK code as I could, but it seems the following needs to be before almost anything else and this is where it is blocked;
char auth[] = "***My-Blynk-Auth-Code***";     
  1. Added the following:
boolean Listen_ModeFlag = false;   
  1. declared the ISR
attachInterrupt(A2, Set_Listen_ModeFlag, FALLING);
  1. The last part;
void loop() {
    
    Blynk.run();
    CS();
    temperature();
    connect();
    Listen_Mode();
}

    
void Set_Listen_ModeFlag() {        //  Set Listen mode FLAG  = true
     Listen_ModeFlag = true;
}

void Listen_Mode() {                //  Enter Listen mode
        if (Listen_ModeFlag == true) {
            WiFi.listen();      
            
    }
}

As far as I can tell, the loop is now handling the entering to Listen mode part??

I tested on a breadboard version, seems ok except for a hard fault each time it exists listing mode, but it corrects and then connects to Particle Cloud. Of course I will need to test it on an actual unit with all the bells and whistles, will do this in the morning. Also still need to test if it still works when the unit starts up without connecting to WiFi at all.

Thanks for the help!
Friedl.

Your Listen_ModeFlag needs to be volatile and you should put Bynk.run() in a if (WiFi.ready()) block.
You also want to reset Listen_ModeFlag before you call WiFi.listen() in order to not fall back into it once you came out.

1 Like

@ScruffR -

Seems I couldnā€™t get it more wrong if I tried :rofl: :rofl:

Thanks!

On this... I got severely reprimanded on there BLYNK forum for what they see as a non-empty void loop(). After this, I decided to keep it as 'clean' as possible :joy:

I will try to implement, than you for your patience :slight_smile:

Regards, Friedl.

No idea how they would argue a ā€œreprimandā€ for a non-empty loop().
Would they expect your code to run nothing but their Blynk stuff? That doesnā€™t make sense.

Have you got a link to that thread?
If their logic would be resilient against lack of a valid connection you could probably get away without the WiFi.ready()-check but IIRC their library is not.

I once also had some discussion about style reagarding some of their functions require non-const parameters where there would definetly (IMHO) be call for const but they refused to consider the proposal entirely :confused:
Hence Iā€™m somewhat reluctant to just take their word for it :speak_no_evil:

1 Like

@ScruffR -

I will check if I still have the link. I rarely get annoyed with people online, but I did not have a great experience in the forum at all. They seem quite set in their ways and believe nothing else can work. they had an issue as I had a 1 second delay in my loop.
Here is what I have so farā€¦ the only way I could get Blynk to authenticate without having the call in void setup()

Before void setup()

char auth[] = "Friedl's auth Token goes here"; 

and then laterā€¦

void connect() {
     if(connectToCloud) {
        if(Particle.connected() == false) {  
             Particle.connect();
             connectToCloud = false; 
             Blynk.begin(auth);
        }
    }
}

void BLYNK() {
   
    if (!WiFi.ready()) {
        Blynk.run();
        
        }else {
    }
}

void loop() {
    
    BLYNK();    
    CS();
    temperature();
    connect();
    Listen_Mode();
}

    
void Set_Listen_ModeFlag() {        
     Listen_ModeFlag = true;
}

void Listen_Mode() {                //  Enter Listem mode when interupt is called
        if (Listen_ModeFlag == true) {
            Listen_ModeFlag = false;
            WiFi.listen();     
    }
}

Seems to work, just need to test in the case where device starts up without being able to connect to WiFi (new client)

Thanks for the help. Need to take my Calc exam now, will test afterwards :slight_smile:

Regards, Friedl.

You may still want a waitFor(WiFi.ready, someTimeout) between the Particle.connect() call and Blynk.begin(auth).
like so

  WiFi.connect();
  if (waitFor(WiFi.ready, 60000)) {
    Blynk.begin(auth);
    Particle.connect();
    connectToCloud = false;
  }
  else {
    // take some action to deal with missing WiFi connection
  } 

BTW, you are negating the WiFi.ready() check here, but you want Blynk.run() to be called only when this is true :wink:


Should you not be taking you exam? Good luck!

1 Like

@ScruffR -

Let me take my test, should be done in couple of hours. I will check afterwards and let you know!.

A million thanks in advance :wink:

Regards, Friedl.

1 Like

Hi @ScruffR -

EDIT:

Ok, so after hours and hours of trying to figure this out, I decide to try a more simplistic approach with a small ā€˜illegalā€™ sketch on a breadboard, calling Listen mode directly from a ISR. Seems to work. :). I then loaded this onto a device and the error started again.

I then replace listen mode with simple LED high when A2 is called and you wont believe itā€¦ it is called even when I trigger D7 HIGH :exploding_head:

I have gone through the schematic endless times and cannot seem how in the world D7 and A2 would be connected. I will assemble a new unit today and incrementally test to see when the problem occurs.

Thank you for your patience :raised_hands:

Friedl.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.