Johnny-Five is a JavaScript framework designed for creating applications in embedded systems and robotics.
This means we can easily interact with a development board like an ESP32 using JavaScript.
Key Features
- JavaScript: Allows developers already familiar with JavaScript to work with hardware without having to learn a new language.
- Active community: Johnny-Five has a wide community and a large number of examples.
- Multiple hardware: It can be used on different hardware platforms.
- Integration with Node.js: This allows building web and IoT applications.
Johnny Five is not for programming your device in JavaScript. Johnny Five provides an easy-to-use API to interact with hardware.
The program runs in Node.JS and communicates with the hardware (through firmware). The advantage is that it’s easy to integrate into web and IoT projects.
Learn to develop with Node.js step by step
Installing Johnny-Five
To start using Johnny-Five with the ESP32, we need to set up our development environment.
Install Node.js: If you don’t have it installed, download and install it following the instructions for your operating system.
Set up your Project
- Create a new folder for your project and navigate to it in the terminal.
- Initialize a new Node.js project with the following command:
npm init -y
Install Johnny-Five: Run the following command to install Johnny-Five
npm install johnny-five
Loading Example
To verify the installation is correct, let’s load a simple example that will blink an LED connected to the ESP32.
Load Firmata onto the ESP32
Johnny-Five communicates with hardware through the Firmata protocol, so it’s necessary to upload the firmware to the ESP32.
Here is a Firmata library for the ESP32
Node.js Code
Create a file named index.js in your project folder and add the following code:
const { Board, Led } = require("johnny-five");
const board = new Board();
board.on("ready", () => {
const led = new Led(13); // Pin 13 for the LED
led.blink(500); // Blink every 500 ms
});
Run the code
In the terminal, run the following command:
node index.js
If everything is set up correctly, the LED should start blinking.
Johnny-Five Usage Examples
Once you have Johnny-Five installed and working, you can start interacting with different hardware components.
GPIO Interaction
Manipulating GPIO (General Purpose Input/Output) pins is the first thing we’ll need to do in almost any project.
const { Board, Led } = require("johnny-five");
const board = new Board();
board.on("ready", () => {
const led = new Led(13); // Set pin 13 as output
// Turn on the LED
led.on();
// Turn off the LED after 2 seconds
board.wait(2000, () => {
led.off();
});
});
Reading Sensors
Johnny-Five also makes it easy to read sensor values. Let’s assume we connect a temperature sensor.
const { Board, Sensor } = require("johnny-five");
const board = new Board();
board.on("ready", () => {
const temperatureSensor = new Sensor("A0"); // Sensor on pin A0
temperatureSensor.on("data", () => {
console.log(`Temperature: ${this.value}`); // Print the read value
});
});
