rwaldron / johnny-five

JavaScript Robotics and IoT programming framework, developed at Bocoup.
http://johnny-five.io
Other
13.3k stars 1.77k forks source link

On executing first.js from php in owner user I get board closed. #1441

Closed arzelcm closed 6 years ago

arzelcm commented 6 years ago

I want to run a webapp which controls the arduino in raspberry (LAMP), so I started developing this app but on the first step I get an error, it wasn't initializing the program so I changed the user php was executing commands, then the program is being executed but johnny-five is closing board immediately so it's impossible to run first.js properly. Does anyone know what can I do to fix it?

dtex commented 6 years ago

@arzel33 I'm not a Pi expert, but I know whoever helps you will need more info. Can you post or link to the code you are running and show us what is output to the conosole when you run the code?

arzelcm commented 6 years ago

That's php running after ajax connection: if (isset($_POST["position_off"])) { $command = shell_exec("node /home/pi/Projects/raspDuino/tests/first.js"); } echo json_encode($command); And that's node.js:

var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
    console.log("Ready!");
    var led = new five.Led(8);
    this.repl.inject({
        led: led
    });
    led.blink(500);
});

If I run this file it's working at nomal way but in the way I want it's not working.

dtex commented 6 years ago

Do you get anything back in $command? That could be helpful.

arzelcm commented 6 years ago

@dtex Yes, I get: 1-Board connected; 2-Initializing; 3-Ready (the console.log); 4-Board Closing; That's literally what I get.

dtex commented 6 years ago

I'm just speculating, but I wonder if your attempt to use the REPL is at the root of the problem. Can you try this code:

var five = require("johnny-five");
var board = new five.Board({ repl: false });
board.on("ready", function() {
    led.blink(500);
});
arzelcm commented 6 years ago

Now it works, thanks a lot!! Could you tell me, please why I was getting this error? And why I can only use repl giving commands on normal terminal? Thanks a lot!!

dtex commented 6 years ago

Again, I'm just guessing, but I figure that when you use shell_exec you don't get the same functionality from node's Readline.Interface which the repl depends on.

It would be nice if it gave a meaningful error on the console but this is such an unusual case that I don't think anyone will be tripping over themselves to submit a PR.

I'm curious, why would you need a repl if you are running the program from a shell in PHP? It seems a little convoluted and I wonder if maybe there is an easier way to achieve your desired functionality.

arzelcm commented 6 years ago

Hi! @dtex sorry for the waiting. What I wanted to do with repl was sync webapp and nodeJs and avoid doing it with a websocket. I had an other problem, when you open the board and the script, the board keeps alive but the script not so timeouts an other stuff will be useless. If someone have this problem... I fixed it using forever package.

dtex commented 6 years ago

If someone have this problem... I fixed it using forever package.

Cool, thanks for noting that.

avoid doing it with a websocket

I've always just used Websockets (socket.io) for having a web app talk to my J5 app.

It sounds like I can close this. If there are any other issues feel free to re-open, or open a new one.

Cheers