0

I'm a little confused with some Process.h methods. I'm trying to run the command "nc 192.168.1.15 9911" to the Linux processor through a sketch. The appropriate netcat listener is waiting on another computer. Arduino Yun is on the same network with thie computer via WiFi.

When I execute this command through SSH connection on Arduino Yun it works fine, but I can't figure out how it's done through a sketch.

This is my code:

#include <Process.h>

Process p;

void setup() {
   Bridge.begin();
   Serial.begin(9600);
   delay(2000);
   p.begin("nc");
   p.addParameter("192.168.1.15 9911");
   p.run();
   //while (!p.running()) {};

 }

 void loop() {
    if (p.available() > 0) {
    Serial.println("test");
 }
}

This code doesn't seem to work, p.available() nerver executes even if I send data packets. I also tried p.runShellCommand("nc 192.168.1.15 9911"); with no luck.

It seems that the commands never reach the Linux command line.

Any insights?

0

There's a few things wrong with your program, some more obvious than others.

First, you have to put each parameter to your shell command in a separate addParameter() call. Otherwise it treats all the parameters as a single parameter and tries to connect you to an address of 192.168.1.15 9911 instead of to port 9911 of address 192.168.1.15.

Secondly you should use runAsynchronously() instead of run() otherwise your sketch blocks at that point until nc terminates. If that never happens then your sketch never gets to the point where it can read the data.

Here is an adaption of your sketch that works for me:

#include <Process.h>

Process p;

void setup() {
    Bridge.begin();
    Console.begin();
    p.begin("nc");
    p.addParameter("192.168.1.15");
    p.addParameter("9911");
    p.runAsynchronously();
}

void loop() {
    static uint32_t ts = millis();

    if (millis() - ts > 1000) {
        Console.println(millis());
        ts = millis();
    }

    if (p.running()) {
        while (p.available()) {
            Console.write(p.read());
        }
    } else {
        Console.println("Restarting process");
        delay(100);
        p.begin("nc");
        p.addParameter("192.168.1.15");
        p.addParameter("9911");
        p.runAsynchronously();
    }
}

It also re-runs nc if / when it terminates. There's also a "heartbeat" printing of millis() so that you can see the sketch is running. It uses the console, not Serial, for its output, so SSH in to the Yun and telnet localhost 6571 to view the output.

| improve this answer | |
  • Although right now I don't need to implement the above code because things have changed, your answer solves the current question. Thanks. – BrainTrance Jul 3 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.