Book Image

Hands-On Robotics with JavaScript

By : Kassandra Perch
Book Image

Hands-On Robotics with JavaScript

By: Kassandra Perch

Overview of this book

JavaScript has an effective set of frameworks and libraries that provide support for embedded device programming and the robotics ecosystem. You’ll be able to put your JavaScript knowledge to work with this practical robotics guide. The book starts by guiding you in setting up an environment to program robots with JavaScript and Rasberry Pi 3. You will build beginner-level projects, such as a line-following robot, and then upgrade your robotics skills with a series of projects that help you get to grips with the Johnny-Five library. As you progress, you’ll learn how you can improve your projects by enabling advanced hardware components and programming concepts. You’ll even build an advanced AI-enabled robot, connect its NodeBots to the internet, create a NodeBots Swarm, and explore Message Queuing Telemetry Transport (MQTT). By the end of this book, you will have enhanced your robot programming skills by building a range of simple to complex projects.
Table of Contents (19 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Project – adding an LCD and button to see and send MQTT events


We can use the AdafruitIO dashboard to post messages to our MQTT feed, and so we'll use an LCD to show what we've sent. We'll also wire up a button that will send an MQTT message when pushed.

Wiring it all up

First, we'll wire our LCD to the I2C pins, and our button to GPIO #5, also known as P1-29:

 

Coding it all together

In a file in the same folder, create mqtt-button-lcd.js. Put in the usual Johnny-Five and Raspi-IO constructors, and in the board-ready handler:

Then, add the client constructor for AdafruitIO's MQTT connection from mqtt-test.js. We'll also set up our LCD and button objects here:

letLCD=newfive.LCD({
controller:"PCF8574",
rows:2,
cols:16
})
letbutton=newfive.Button('P1-29')

After that, we're ready to code the sending of messages on the press of the button, and the printing of messages received on the LCD:

client.on('connect', () => {
  console.log('Connected to AdafruitIO')
  client.subscribe(process.env.ADAFRUIT_IO_FEED...