1

I'm currently working on an arduino project. Were the arduino is communicating with a NodeJS server via web sockets.

The socket connection is working fine and has no problems. But the problem I currently have is that I want to be able interupt an infinite while loop with a socket emit from the NodeJS server.

I found a page that had a solution for this problem but only with a button attached to the arduino.

Link to page (Interrupt with button)

This is the loop I want to be able to interrupt with a socket:

bool loopRunning = true;

void rainbow(int wait) {
while(loopRunning == true) {

      for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
        for(int i=0; i<strip.numPixels(); i++) { 
          int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
          strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
        }
        strip.show(); 
        delay(wait);  
      }
    }
}

I want to set loopRunning to false when I recieve a socket emit.

Anyone have any ides how I can implement this?

Page to the socket functionality that I use

jfriend00
  • 683,504
  • 96
  • 985
  • 979
Robin Fors
  • 83
  • 7

1 Answers1

1

It seems there are two main functions you need to use:

  • SocketIoClient::on(event, callback) - bind an event to a function
  • SocketIoClient::loop() - process the websocket and get any incoming events

I can't test this easily, but based on the documentation it seems like something like this should work:

bool loopRunning = true;
SocketIoClient webSocket;

void handleEvent(const char* payload, size_t length) {
  loopRunning = false;
}

void setup() {
  // ...
  // general setup code
  // ...

  webSocket.on("event", handleEvent); // call handleEvent when an event named "event" occurs
  webSocket.begin(/* ... whatever ... */);
}

void rainbow(int wait) {
  while(loopRunning == true) {
    for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
      for(int i=0; i<strip.numPixels(); i++) { 
        int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
        strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
      }
      strip.show();
      webSocket.loop(); // process any incoming websocket events
      delay(wait);
    }
  }
}
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • I already use both of the two main functions you mentioned. The problem is that when the while loop gets called I can no longer receive incoming sockets from the server. – Robin Fors Dec 03 '19 at 19:47
  • 1
    @RobinFors, the main addition to the code is `webSocket.loop();` in the while loop – Juraj Dec 03 '19 at 20:39
  • @RobinFors If your code already includes the use of those functions (the code you shared doesn't have `webSocket.loop()` inside it) and it is still not working, please share the entire relevant code snippet – Michelle Tilley Dec 03 '19 at 21:56
  • I tried doing what you said but still no luck, when the loop get's called I can no longer receive sockets. – Robin Fors Dec 04 '19 at 14:04
  • @MichelleTilley we can still not get any socket emits from the server. – Jonathan Thunberg Dec 10 '19 at 12:26