Hands-On Robotics with JavaScript

Book description

Leverage Raspberry Pi 3 and different JavaScript platforms to build exciting Robotics projects

Key Features

  • Build robots that light up and make noise
  • Learn to work with Raspberry Pi 3 and JavaScript
  • Connect your Johnny-Five projects to external APIs and create your own IoT

Book Description

There has been a rapid increase in the use of JavaScript in hardware and embedded device programming. JavaScript has an effective set of frameworks and libraries that support the robotics ecosystem.

Hands-On Robotics with JavaScript starts with setting up an environment to program robots in JavaScript. Then, you will dive into building basic-level projects such as a line-following robot. You will walk through a series of projects that will teach you about the Johnny-Five library, and develop your skills with each project. As you make your way through the chapters, you'll work on creating a blinking LED, before moving on to sensors and other more advanced concepts. You will then progress to building an advanced-level AI-enabled robot, connect their NodeBots to the internet, create a NodeBots Swarm, and explore MQTT.

By the end of this book, you will have gained hands-on experience in building robots using JavaScript

What you will learn

  • Install and run Node.js and Johnny-Five on Raspberry Pi
  • Assemble, code, and run an LED project
  • Leverage JavaScript libraries to build exciting robots
  • Use sensors to collect data from the world around you
  • Employ servos and motors to make your project move
  • Add internet capabilities to your Johnny-Five project

Who this book is for

Hands-On Robotics with JavaScript is for individuals who have prior experience with Raspberry Pi 3 and like to write sketches in JavaScript. Basic knowledge of JavaScript and Node.js will help you get the most out of this book.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Robotics with JavaScript
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Setting Up Your Development Environment
    1. Technical requirements
    2. What is the Raspberry Pi?
      1. Microcontrollers
      2. General-Purpose Input/Output (GPIO) pins
      3. Debian and Raspbian
      4. Johnny-Five and Raspi-IO
      5. So, the Pi is technically a microcontroller...
      6. ...but it is also a computer!
    3. How we will use the Raspberry Pi
      1. Taking advantage of all that the Raspberry Pi has to offer!
      2. Johnny-Five – letting us code hardware in Node.js
    4. Installing the operating system
      1. Downloading Raspbian Lite
      2. Burning the image to an SD card
      3. Editing files on the SD card
      4. Booting up the Pi
        1. SSHing from a Linux or Mac
        2. SSHing from Windows
    5. Setting up your password and hardware interfaces
      1. First things first – change your password!
      2. Updating the Raspberry Pi
      3. Turning on the hardware interfaces
    6. Installing Node.js, Johnny-Five, and Raspi-IO
      1. Installing Node.js and npm
        1. Detecting your version of ARM processor
      2. Installing Johnny-Five and Raspi-IO
    7. Summary
    8. Questions
    9. Further reading
  8. Creating Your First Johnny-Five Project
    1. Technical requirements
    2. Creating a project folder
      1. Setting up npm to manage our modules
    3. Getting started with Johnny-Five and Raspi-IO
      1. Gathering resources and documentation
      2. Taking a look at the LED-blink project
      3. Raspberry Pi pin numbers
    4. Wiring up an LED
      1. Putting together and attaching the cobbler
      2. Attaching the resistor and LED
    5. Making the LED blink
      1. Putting your code on the Raspberry Pi
      2. Running your code
    6. Summary
    7. Questions
    8. Further reading
  9. Building Interactive Projects with RGB LED
    1. Technical requirements
    2. Looking at the LED and LED.RGB API
      1. The LED object
      2. The Led.RGB object
    3. PWM pins and GPIO expanders
      1. How do PWM pins work?
      2. Why we need a GPIO expander
      3. Wiring up our GPIO expander and RGB LED
    4. Bringing in other node packages
      1. Project – building a rainbow
      2. Using the color npm module
      3. Getting our Johnny-Five code started
    5. The REPL – a powerful tool in Johnny-Five
      1. How does the REPL work?
      2. Adding our RGB LED to the REPL
      3. Controlling our LED from the command-line interface
    6. Summary
    7. Questions
    8. Further reading
  10. Bringing in Input with Buttons
    1. Technical requirements
    2. Using inputs in robotics projects
      1. Digital versus analog input
      2. How to handle analog input with the Raspberry Pi
        1. Analog GPIO expanders
        2. Using input devices with digital interfaces
      3. How Johnny-Five handles input
      4. The structure of a typical Johnny-Five project
        1. The beginning – including libraries and creating our board object
        2. The board ready event handler
        3. Constructing our component objects
        4. Input event handlers and output device manipulation
    3. The Johnny-Five button object
      1. The button object
      2. Button events
    4. Wiring up buttons
      1. Putting a button on a breadboard
      2. Using a pull-down resistor
    5. Adding buttons to our RGB LED project
      1. Wiring everything up
        1. Using the power and ground side rails
        2. Wiring up the buttons
      2. Button 1 – stop and start rainbow 
        1. Refactoring the rainbow cycle
      3. Button 2 – next color
    6. Summary
    7. Questions
    8. Further reading
  11. Using a Light Sensor to Create a Night-Light
    1. Technical requirements
    2. Using an analog sensor with the Pi
      1. Finding the right sensors for your Pi project
        1. I2C devices
        2. SPI 
        3. How to determine if your sensor will work with Johnny-Five
    3. The ambient light sensor
      1. Wiring up the sensor
      2. Writing a program to get readings and print them to the command line
        1. The Johnny-Five sensor events
          1. Handling sensor data in the event handler
        2. Using and formatting Johnny-Five sensor data
          1. Using .scaleTo() and .fscaleTo() to fine-tune measurements
        3. Printing sensor data to the command line
        4. Using barcli to make the data easier to see
          1. Importing barcli and constructing our barcli graph
          2. Getting the bar graph to update
    4. Creating our night-light
      1. Wiring up the LED
      2. Coding this project
    5. Summary
    6. Questions
    7. Further reading
  12. Using Motors to Move Your Project
    1. Technical requirements
    2. More about motors
      1. How to control a motor with a microcontroller
    3. Preparing for a motor-driven project with Raspberry Pi
      1. Putting the hat together
      2. Putting the hat on the Pi
    4. The Johnny-Five motor object
      1. Constructors for our hat
      2. Functions that move the motor
      3. Adding REPL control
      4. Loading and running your motor
    5. Troubleshooting your motorized projects
    6. Project – cat toy
      1. Putting a piece of paper on the motor shaft
      2. Coding the randomness to start/stop the motor
    7. Project – using two gearbox motors and the motors object
      1. Wiring up your TT motors
      2. The motors Johnny-Five object
      3. Writing some functions
      4. Running our motors project
    8. Summary
    9. Questions
  13. Using Servos for Measured Movement
    1. Technical requirements
    2. Differences between motors and servos
      1. Calculated movements
      2. Regular versus continuous servos
      3. Powering servos and motors
    3. Getting a servo working with Johnny-Five
      1. The Johnny-Five servo object
      2. Wiring the servo to our PWM hat
      3. Coding your first servo sweep
    4. Project – two servos and the REPL
      1. Wiring up a second servo
      2. Using the Johnny-Five servos object
      3. Adding the Servos object to our code
      4. Adding in REPL functionality
      5. Playing with our servos on the command line
    5. Project – light meter with the servo
      1. Adding in the light sensor
      2. Making the servo into a meter
      3. Coding the project
      4. Running and using our light meter
    6. Project – the continuous servo
      1. Wiring up the servo
      2. Continuous servo constructor and methods
      3. Using the REPL with the continuous servo
      4. Playing with the continuous servo in the REPL
    7. Summary
    8. Questions
  14. The Animation Library
    1. Technical requirements
    2. Animating movement
      1. Why we need the animation library
      2. Moving servos with true precision
      3. Implicit use of the animation library
        1. Using servo.to() to implicitly create an animation
        2. Playing with implicit animations
        3. Playing with implicit animations, take two
    3. The terminology of the animation library
    4. The construction of the animation object
      1. Creating the animation object
      2. Planning out the animation sequence
      3. Creating keyframes
        1. Using null and false as positions in keyframes
        2. Programming our keyframes 
      4. Setting cue points and duration
      5. Putting it all together to make an animation
      6. Watching your animation at work
    5. Easing into your servo animations
      1. How easing fits into an animation segment
      2. Adding easing to our first animation
      3. Easing an entire animation segment
    6. Learning more about queuing and playing animation segments
      1. Looping animation segments
      2. Changing the speed of animation segments
      3. Playing, pausing, and stopping animation segments
      4. Tying it all together in the REPL
    7. Summary
    8. Questions
  15. Getting the Information You Need
    1. Technical requirements
    2. Why connect your NodeBots to the internet?
      1. Using the power of npm modules
      2. Using the data you collect
      3. Some things to keep in mind
    3. Getting weather data on our Pi with OpenWeatherMap
      1. Getting an OpenWeatherMap API key
      2. Bringing in request
      3. Parsing the response
    4. Building a weather dashboard with an LCD
      1. Adding an LCD to the Pi
      2. The LCD object
        1. Constructing our LCD
        2. Setting up the LCD
        3. Printing to and clearing the LCD
      3. Coding it all together
    5. Project – scraping data from websites with your Pi
      1. Scraping downforeveryoneorjustme.com for johnny-five.io
      2. Making the HTTP request
      3. Using Cheerio to get the element we want
      4. Parsing the HTML and showing the result
    6. Summary
    7. Questions
    8. Further reading
  16. Using MQTT to Talk to Things on the Internet
    1. Technical requirements
    2. IoT device communications
      1. Long polling
      2. Websockets
    3. MQTT - an IoT PubSub protocol
      1. The basics of MQTT
    4. Setting up MQTT on the Pi with AdafruitIO
      1. Creating an account and a feed
      2. Subscribing to the feed using the mqtt npm module
        1. The mqtt module
        2. The dotenv module 
      3. Testing our connection
    5. Project – adding an LCD and button to see and send MQTT events
      1. Wiring it all up
      2. Coding it all together
    6. Project – social media notifier bot with IFTTT
      1. Getting started with IFTTT
        1. Linking IFTTT to Adafruit
      2. Setting up feeds for your social media MQTT messages in AdafruitIO
      3. Creating our IFTTT Applets
      4. Wiring up our project
      5. Coding our social media notifications to show on the LCD
      6. Running your social media bot
    7. Summary
    8. Questions
    9. Further reading
  17. Building a NodeBots Swarm
    1. Technical requirements
    2. Project – connecting multiple NodeBots
      1. Optional – setting up a second Raspberry Pi
      2. Setting up your project files and folders
        1. If you're using one Pi
        2. If you're using two Pis
      3. Adding a light sensor to the Pi
      4. Creating an MQTT broker on the Pi
      5. Programming the MQTT client – have the Pi Report Home
        1. If you're using one Pi
        2. If you're using two Pis
      6. Running our MQTT project
        1. If you're using one Pi
        2. If you're using two Pis
    3. Expanding your NodeBots knowledge
      1. Using Johnny-Five on other boards
        1. Figuring out pin numbers
        2. Checking the platform support page
      2. Other node robotics platforms
        1. The Tessel 2
        2. The Espruino ecosystem
      3. Programming graphically with Node-RED
    4. Continuing on your NodeBots adventure
      1. Figuring out what to build
      2. Reaching out to the NodeBots community
      3. Where to go to learn more about Electronics
    5. Summary
  18. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Robotics with JavaScript
  • Author(s): Kassandra Perch
  • Release date: August 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789342055