Introduction: Using JavaScript to Control NodeMCU

This manual is created as research over how you can control a servo motor using JavaScript and Node.js.

Supplies

Hardware

  • NodeMCU
  • Servo motor
  • USB cable

Software

  • Arduino IDE
  • Node.js
  • Johnny-five library
  • Keypress library
  • Code editor
  • johnny-five etherport-client

Step 1: Step 1: Setup Your Board

Add ESP8266 Board Support to Arduino IDE

  1. Launch ArduinoIDE
  2. Open Preferences Add http://arduino.esp8266.com/stable/package_esp8266com_index.json to the Additional Boards Manager URLs input. If you already have something in this field, you can add multiple URLs by delimiting with commas.
  3. Click OK
  4. Navigate to menu Tools > Board > Boards Manager
  5. Find esp8266 by ESP8266 Community on the list.
  6. Click Install.
  7. Once this is complete, click OK.

Connecting Servo motor

If you have the same servo motor that I am using, you can follow the steps below to connect the servo motor to your board.

To connect the servo motor to your board follow the steps:

  • Red (servo) on 3v
  • Orange (servo) on a D port (eg D5)
  • Brown (servo) on GND

Test if the servo motor is working by using standard code, follow the steps:

  1. Open Arduino software
  2. File > Examples > Servo (esp8266) > Sweep
  3. Indicate in the setup on which port your servo (orange wire) is located D5. By changing myservo.attach(2); to myservo.attach(D5);
  4. Upload the code and see if it's working

If the servo motor is working properly, go to your Arduino IDE and make sure that you have the correct board and port. You can find the board and port under tools in the Arduino IDE.

Use a standard sketch that comes bundled with the Arduino IDE

  1. Go to File > Examples > Firmata > StandardFirmataPlus
  2. Upload it to your board

Provided that all goes well, you can move on to the next step.

Step 2: Step 2: Install Node.js

You need to install Node.js if you don't already have it. Because you will need it to install Johnny-five library.

Install Node.Js, follow the steps:

  1. Go to https://nodejs.org/en/
  2. Download the latest version of Node.js
  3. Follow the installation process

That is all that you need to do to install Node.js.

Step 3: Step 3: Install Johnny-five Library

After that, you have installed Node.js. You will use it to download the Johnny-five library.

Install johnny-five library

  1. Open Terminal
  2. You will be installing the J5 library using the node package manager.
  3. Run npm init
  4. Click Enter until you see this codeIs this ok? (yes) then click enter one more time.
  5. Run npm install johnny-five
  6. Run npm install keypress (a standard library that let you use keypress from the keyboard to control your servo)

That is everything that you need to do for now. You can start with the next step.


Step 4: Step 4: Starting With JavaScript

I am using a code that you can find on by using this link: http://johnny-five.io/examples/servo-continuous/.

Start with JS:

  1. Copy the code from the previous link to your code editor
  2. Save it with the name servo.js and put it in a folder on your desktop
  3. Chang this code const servo = new Servo.Continuous(10); to const servo = new Servo.Continuous(5);

This code will let you use the up and down arrows to control the servo motor.

const {Board, Servo} = require("johnny-five"); const keypress = require("keypress"); const five = require('johnny-five'); keypress(process.stdin);

const board = new five.Board(),

board.on("ready", () => {

console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop.");

const servo = new Servo.Continuous(5);

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("CW"); servo.cw(); } else if (key.name === "down") { console.log("CCW"); servo.ccw(); } else if (key.name === "space") { console.log("Stopping"); servo.stop(); } }); });

Step 5: Step 5: Trying to Fix an Error

Go back to Terminal to run the Javascript code that you just made. Run node servo.js

At this point the code should be working but, it's also normal to get an error. In case you are having an error that looks like this:

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ca3323}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px} span.s1 {font-variant-ligatures: no-common-ligatures} span.s2 {font-variant-ligatures: no-common-ligatures; color: #000000}

A timeout occurred while connecting to the Board.

Please check that you've properly flashed the board with the correct firmware.

See: https://github.com/rwaldron/johnny-five/wiki/Gett...

If connecting to a Leonardo or Leonardo clone, press the 'Reset' button on the board, wait approximately 11 seconds for complete reset, then run your program again.

Try the steps below to fix it:

Flash Dev Board

  1. Use another example from Arduino IDE by opening File > Examples > Firmata > StandardFirmataWifi
  2. Modify this sketch by uncommenting line 85: #define SERIAL_DEBUG. This will allow you to view debug output in Arduino IDE's Serial Monitor.
  3. There will be a tab for a file wifiConfig.h. Click this tab to open wifiConfig.h.
  4. On line 119, enter the name of your WiFi network, e.g., char ssid[] = "WiFi network namer";
  5. On line 151, enter the WiFi network password, e.g. char wpa_passphrase[] = "Wifi network password";
  6. Upload this sketch.

To confirm that things are working go to your serial monitor. And don't forget to change the baud rate to 9600.

After that, you should see the following in your serial monitor.

IP will be requested from DHCP ...
Attempting to connect to WPA SSID: Ziggo46105 WiFi setup done .............SSID: Ziggo46105 IP Address: 192.168.178.31 signal strength (RSSI): -59 dBm

Note and/or copy the IP address. You'll need it later. This is important!

- Go back to Terminal and run npm install johnny-five etherport-client.

- Add the code below to servo.js

'use strict';
const {Board, Servo} = require("johnny-five"); const keypress = require("keypress"); const { EtherPortClient } = require('etherport-client'); const five = require('johnny-five'); keypress(process.stdin);

const board = new five.Board({ port: new EtherPortClient({ host: '10.0.0.45',

port: 3030 }), repl: false });

board.on("ready", () => {

console.log("Use Up and Down arrows for CW and CCW respectively. Space to stop.");

const servo = new Servo.Continuous(5);

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("CW"); servo.cw(); } else if (key.name === "down") { console.log("CCW"); servo.ccw(); } else if (key.name === "space") { console.log("Stopping"); servo.stop(); } }); });

- Replace 10.0.0.45 with the IP address you noted from the serial monitor.

- Save this file, and run it on Terminal using npm servo.js.


Step 6: Step 6: Did It Work?

After doing all of that I tried running node servo.js in Terminal. As you can see I was able to see in Terminal that I had a connection and that I wasn't having any errors. But still, the servo motor was not reacting to the arrow keys.

<p>Emmas-MacBook-Pro-4:eindopdracht emmayounan$ node servo.js</p><p>1571170181011 SerialPort Connecting to host:port: 192.168.178.31:3030  </p>1571170181014 Connected Connecting to host:port: 192.168.178.31:3030  
Use Up and Down arrows for CW and CCW respectively. Space to stop.
CW
CW
CW
CW
CW
CW
CW
CW
CCW  

I couldn't find out yet why it is not working, I will sure keep trying. But in the meanwhile, feel free to say your suggestions.

The End.