Interrupt problem with push button and websocket

Hi everyone,
I’m getting some troubles with my Photon and the web socket library. I’ve to add to my software the possibility to detect the press of buttons, usually with interrupt, within a firmware that uses the web socket library to communicate with a remote server.

  void loop()
  {
    client.monitor();
    if(welcome){
      sendWelcomeMessage();
      welcome = false;
    }
  }

This is my code and in the setup there is settings about the interrupt, but it seems it is not detected, whereas without the lines of code from the web socket library, it works. What can I do to fix this problem?

@zebrone92, having the details of your interrupt setup and how your button is wired would be a good start.

Thank you for your reply.
This is setup code:

 void setup()
{
  pinMode(D2, INPUT_PULLUP);
  attachInterrupt(D2, blink, RISING);
}

The wired is fine, since this simple code works correctly:

    void blink(void);
int ledPin = D7;
volatile int state = LOW;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(D2, INPUT_PULLUP);
  attachInterrupt(D2, blink, RISING);
}

void loop()
{
  digitalWrite(ledPin, state);
}

void blink()
{
  state = !state;
}

@zebrone92, so the button is either connected to GND and the interrupt will fire on bounce or when the button is released OR the button is NC (normally connected) to GND and when you press the button opens and allows the pull-up to go high?

I also noticed that you have no debouncing in your code. Buttons and switches tend to “bounce”, causing your interrupt to fire multiple times. You may want to consider adding debounce code. Several great examples of such code are available through a search of the forum.

Thank you for your reply. I know the problem about bouncing, but the problem is different. The interruput seems not fired, with the real firmware with the web socket implementation and I don’t know to move on about this issue. Can you help me?

@zebrone92, are you running with SYSTEM_THREAD(ENABLED)? What SYSTEM_MODE() are you using? It may be time to post some of your code.

How do you distinguish between not firing at all and bouncing an even number of times?

With your current ISR you can't.

1 Like

Thank you for your reply. I’m not using the SYSTE_THREAD(ENABLED) but I’ve set SYSTEM_MODE(SEMI_AUTOMATIC); is it fine?

The push button should trigger a method to communicate with a remote server and there are no messages sent to it. Also with Serial I can’t see anything trigged.

I can’t really see the connection between your first and your second code.
The first does not show anything about a button and the second doesn’t do any publishing or printing.
So your topic title doesn’t really fit either of them and hence it’s really difficult to give you any meaningful advice.

I'm attaching to you the draft version of the real code, where I'm getting some problems:

//SYSTEM_MODE(SEMI_AUTOMATIC);
#define PIXEL_PIN D0
#define PIXEL_COUNT 60
#define PIXEL_TYPE WS2812B
// Convenient 2D point structure
struct Point {
  float x;
  float y;
};

float phase = 0.0;
float phaseIncrement = 0.01;  // Controls the speed of the moving points. Higher == faster. I like 0.08 ..03 change to .02
float colorStretch = 0.11;    // Higher numbers will produce tighter color bands. I like 0.11 . ok try .11 instead of .03
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int brightness = 100;
char server[] = "10.10.10.5";
int tableN = 8;
boolean on = false;

WebSocketClient client;

boolean status = false;
boolean welcome = false;

SYSTEM_MODE(SEMI_AUTOMATIC);

void onOpen(WebSocketClient client){
  Serial.print("Connection Opened on port ");
  welcome = true;
}

void onMessage(WebSocketClient client, char* message) {
  Serial.print("Received: ");
  Serial.println(message);

}

  void onClose(WebSocketClient client, int code, char* message) {
    Serial.println("Connection closed");
    Serial.print("reopening connection on port");
    welcome = true;
  }


  /* This function is called once at start up ----------------------------------*/
  void setup()
  {
    Serial.begin(9600);
    Serial.println("Setup");
    WiFi.connect();
    waitUntil(WiFi.ready);
    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
    strip.setBrightness(brightness);
    rainbow();
    client.onMessage(onMessage);
    client.onOpen(onOpen);
    client.onClose(onClose);
    client.connect(server,8080,NULL,"");
    pinMode(D2, INPUT_PULLUP);
    attachInterrupt(D2, pushButton, RISING);

  }

  void pushButton(){
      client.send("Trigged");
  }



  void loop()
  {
    client.monitor();
    if(welcome){
      sendWelcomeMessage();
      welcome = false;
    }
  }

  void sendWelcomeMessage(){
    client.send("myMessage");
  }

@zebrone92, without digging into the websocketclient code, I don’t recommend calling client.send("Trigged"); from the ISR. Instead, set a flag that you can sample and reset in loop() to then call client.send("Trigged");. This is most likely what is causing your problems.

Thank you for your reply @peekay123! Do you mean the problem is about the usage of client.send method? With a boolean or int value set in the interrupted method and then in the loop() to use the client.send method?

@zebrone92, you got it! Set a bool to true in the ISR then check for it being true in loop(). When true, reset it to false and run client.send(). Make sure to make that flag a volatile bool variable to reduce conflicts between the ISR and loop().

1 Like