How to Control Relay using ESP8266 Wi-Fi NodeJS & Johnny Five on Windows 10

How to Control Relay using ESP8266 Wi-Fi NodeJS & Johnny Five on Windows 10 may sound like a major project, but it’s really not that hard. Although there are quite a few steps, come with me and this post will tell you everything you need to do it (I hope).

But first, like every upcoming entrepreneur ☺ , I also have available a PDF book with a bit more detail and every thing in one place (here).

For this project we are going to flash a custom version of Firmata… first, we will create it with Firmata Builder, set some Wi-Fi and debug variables to our ESP8266, and connect up a Relay. Then we will Install NodeJS, Johnny Five and some other libraries on Windows 10, write a controlling script and run it from the Windows command line to turn the Relay On or Off.

Simple…

The world of IoT, Robotics, JavaScript applications, Remote Control etc., can sometimes be really complicated. I find the method I’m about to show you of Wi-Fi control simple, yet functional, allowing me to concentrate on what I want my device to achieve and not get bogged down with the interface.

What you will need

ESP8266 Development Board

5V 1 channel relay module for Arduino

USB to Micro USB cable

Small breadboard plugboard (optional but handy)

Wi-Fi enabled Internet connection

Computer with Windows 10 operating system

Power Options

Connected to the USB port on your computer

5V 1000mA USB plug pack

For portable, I use a Power Bank Portable Charger, USB External Battery or 3.7v rechargeable if the ESP8266 supports it.

All from your favorite supplier.

NodeJS & NPM install

You will need NodeJS & NPM installed on your Windows PC

Short version… Open your favorite Web Browser, navigate to https://nodejs.org/en/download/

Download Node.js Installer

Run the installer, accepting all the defaults, until it finishes.

All should be well.

Or, should you need more detailed instructions, the following link will give an in depth look at how to do this.

www.iotxplain.com.au/how-to-install-node-js-and-npm-on-windows-10

If you type. node -vat Admin Command prompt you should get a version number as a response.

Flash Firmata to ESP8266 Device

You will need to Flash Firmata WiFi to your ESP8266 Device

To do this I used Firmata Builder.

The following link gives an in depth look at how to do this.

www.iotxplain.com.au/how-to-use-firmata-builder-wi-fi-with-esp8266

You will need to do this step before proceeding, as your esp8266 device must be flashed with a firmata Wi-Fi for anything to work.

Install Windows build tools

Unless you already have a current version of Microsoft Visual Studio or the Windows Build tools installed, you will need to install them.

Note we are talking about Microsoft Visual Studio not Visual Studio Code.

The link to download the installer is here (link auto downloads 1.433k installer exe )

The version at the time of writing was – vs_buildtools__270069729.1627509256.exe

After it downloads, run the installer.

Under workloads, select Desktop Development with C++

To your right it should say “MS Build Tools”.

Click Install, and be very patient, It will take a while.

Wiring up our ESP8266 Device

Note I’m using a relay that I know triggers fine with 3.3 volts.

You need to make your own checks on your relay to ascertain its capabilities.

Create working directory

We need to create a working directory, a place to install our control script, nodejs, it’s libraries, modules etc.

From a Windows 10 Command prompt or Powershell prompt with Administrative Privileges type:

md c:espctrl <enter>

cd c:\espctrl <enter>

Note: espctrl can be any name you wish. and of course so can the c: drive letter.

So from now on our working directory is c:\espctrl.

Install J5 & Nodejs Modules

We need to install Johnny-Five and Node.Js Modules in our working directory. So let’s get that done next.

Open a Command Prompt as Administrator in the directory you have chosen to install to.

In my case I am using. c:\espctrl

Now Enter the following commands.

npm init -y <enter>

npm install johnny-five <enter>

npm install etherport-client <enter>

npm install keypress <enter>

If everything went well you are ready to write a Nodejs script to control your ESP8266 device.

Write NodeJs Script

We need to write a Node.Js script to use Johnny-Five.JS and our other modules, which will turn the Relay On or Off on our ESP8266 via Wi-fi.

Using your favorite text editor, create a file in the c:\espctrl directory or your working directory.

For the sake of this blog I will call this script espctrl.js but you can use any name you like, just don’t forget the .js extension.

Add the following code -.

const { EtherPortClient } = require(“etherport-client”)

const { Board, Led, Pin } = require(“johnny-five”)

const keypress = require(“keypress”)

const board = new Board({

port: new EtherPortClient({

host: “192.168.0.189”,

port: 3030,

}),

repl: false,

})

keypress(process.stdin)

const LED_PIN = 2

const RLY_PIN = 12

board.on(“ready”, () => {

console.log(“Board ready”)

var led = new Led(LED_PIN)

const pin = new Pin(LED_PIN);

pin.high();

var rly = new Led(RLY_PIN)

const rlypin = new Pin(RLY_PIN);

rlypin.low();

console.log(“Use Up and Down arrows for On and Off. Q Key to Quit.”)

process.stdin.resume();

process.stdin.setEncoding(“utf8”)

process.stdin.setRawMode(true)

process.stdin.on(“keypress”, (ch, key) => {

if (!key) {

return

}

if (key.name === “q”) {

console.log(“Quitting”)

process.exit()

} else if (key.name === “up”) {

console.log(“on”)

rlypin.high();

} else if (key.name === “down”) {

console.log(“off”)

rlypin.low();

}

})

})

*** Note the EtherPortClient IP needs to be the IP for your ESP8266 WI-FI device as found in the steps above. Save this file as c:\espctrl\espctrl.js

Testing & Operation

All the hard work is done, let’s have some fun.

Connect your ESP8266 that has been flashed with your Firmata code, ensure it can access your wifi network and has a working relay board with a signal source of GPIO pin 12. And of course, don’t forget to make sure it’s powered up.

Open a command prompt (Admin) at

c:\espctrl Then Type..

node espctrl.js <enter>

YAY! You did it!…

Yeah I know, we’re not sending another billionaire into space, but guess what, everything to do with digital devices starts with On or OFF.

The benefits are that we did not have to compile our code to run it, just type the script and go and you see the result right away.

Go on, have a play, press up and down keys, then Q for quit.

Type node espctrl.js <enter> and it just does it again, really it’s just like magic.

Alright enough of this silliness, but I do like the simplicity of this type of scripting.

Add another relay or sensor and do it again. And remember, if you are using a power bank the whole thing is portable, as long as it is within Wi-Fi range. Great FUN! Not a big investment of money and gets the brain working. What’s not to like?

Have Fun..Cheer’s Al

PS Don’t forget to checkout the book…. ESP8266 Wi-Fi Control Relay Using NodeJS & JohnnyFive