Search code examples
node-red

execNode in NodeRed doesn't execute a while 1 statement


I'm running an exec node to run C code to send and receive data via Lora.

The code runs perfectly without a while 1 loop.

If I put a while true loop it will keep running but won't work. The while 1 is used to read data every second.

I tried using daemon exec node also bigExec but none worked.


Solution

  • If the process doesn't ever exit (which is what I assume you mean by having a while 1 loop) then the daemon node is the correct answer.

    As long as your application prints it's output to stdout or stderr and includes a new line at the end of each output it will send a new message for each line of output. You might need to ensure that stdout is properly flushed, you can do this by adding fflush(stdout); after the printfs e.g.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char **argv) {
        int i = 0;
        int max = 0;
    
        if (argc == 2) {
            max = atoi(argv[1]);
        }
    
        while (1) {
            if (max) {
                if (++i > max) {
                    exit(0);
                }
            }
            printf("helloWorld\n");
            fflush(stdout);
            sleep(2);
        }
    
    }
    

    The exec node will wait until the process exits before sending a message so is not not what you need.