Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Johnny-Five with React

I would like to use Johnny-Five and create an interface with React JS.

I used this React setup tutorial. I installed Johnny-Five in the same project: http://johnny-five.io/ (Johnny-Five works very well alone, in standalone apps. This allows me to control my Arduino).

My React index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import MainRouter from './routes';
import registerServiceWorker from './registerServiceWorker';
import five from 'johnny-five';

var board = new five.Board({
  port: "/dev/ttyACM0" 
});

board.on("ready", function() {
  var led = new five.Led(13);
  led.blink(500);
});

ReactDOM.render( <MainRouter />, document.getElementById('root'))
registerServiceWorker();  

When I run React, the site starts but the board does not react! While the same code without React works perfectly well. Any idea to make Johnny-Five work with React?

like image 925
Julien Avatar asked Oct 28 '17 21:10

Julien


People also ask

What is Johnny-five for JavaScript robotics?

There has been a rapid rise in the use of JavaScript in recent times in a variety of applications, and JavaScript robotics has seen a rise in popularity too. Johnny-Five is a framework that gives NodeBots a consistent API and platform across several hardware systems.

What is Johnny-five?

Originally created by Rick Waldron in 2012, Johnny-Five is maintained by a community of passionate software developers and hardware engineers. Over 75 developers have made contributions towards building a robust, extensible and composable ecosystem. The only kit designed for getting started with Johnny-Five!

What boards does Johnny-five work with?

Johnny-Five has been tested with a variety of Arduino-compatible Boards. For non-Arduino based projects, platform-specific IO Plugins are available. IO Plugins allow Johnny-Five code to communicate with any hardware in whatever language that platform speaks!

What can Johnny-five do with IOIO?

IO Plugins allow Johnny-Five code to communicate with any hardware in whatever language that platform speaks! JavaScript can be used to control hordes of small robots, creative maker projects, and IoT devices. With the Node.js ecosystem at hand, hardware prototyping gets fun, intuitive and fast.


1 Answers

The issue is, Johnny Five (J5) cannot be natively run from the browser. J5 needs access to ports on your computer which the browser (for security reasons) does not allow. My theory is when you say you "run without React", you are running via a NodeJS console (and not just removing the React portion of code and re-running in the browser). NodeJS applications run in a different context then browser scripts.

So how do you achieve your goal? You have a couple options that I can think of.

Option 1:

First, write a NodeJS Desktop application. Desktop applications give you the runtime context you need (access to ports) while providing you with the graphical user interface (GUI) you were hoping for with a web application. You can do this with Node Webkit, AppJS, or a few others (just google NodeJS Desktop Applications). The great thing about these Desktop applications is the GUI is written in HTML and CSS. So it's just like a web page, it just isn't run in the browser (or not the browser you'd expect).

Option 2:

Perhaps you really do want a web application (maybe you want a webpage others can access and control your Johnny Five bot). In this case, you will need to write two systems. The first is the web client (which you were already working on with your React code). The web client will not have any Johnny Five code. The second system you will need to write is the web server. The server will handle all the Johnny Five code. Your client will have an interface (buttons, text inputs, etc...). When the user clicks a button, you send a request to the server. The server handles this request by calling the proper Johnny Five code. Here's an extremely simplified example.

like image 120
Spidy Avatar answered Nov 07 '22 19:11

Spidy