Preface

This book describes the role that JavaScript plays in embedded devices. Driven by Arduino and Raspberry Pi, there is an ongoing democratization of hardware development processes. New boards and software toolchains make hardware development more accessible, the same way full stack JavaScript’s usability made programming easier to approach.

Inspired by open feedback and worldwide collaboration, there are evolving communities on the Web where people join hands to build weather stations, robots, or spectrometers (for a nice overview of this trend, see the entries for the 2015 Hackaday Prize).

Compared to those “hobby” projects, professional hardware development often comes with high costs of special computer-aided design (CAD) software products or additional toolchains. This is very different from web development, where the free and open source tools hobbyists use to build are often the same tools used by professionals. In open source software, the lines frequently blur between the work of professional and hobbyist: thousands of examples can be found on GitHub, where many software developers (often joined by “hobbyists”) build solutions for business or private needs.

The Arduino and Raspberry Pi ecosystems already heavily depend on open source projects hosted on GitHub. But for building embedded systems, GitHub is not a complete solution because it is not built for collaboration on hardware, and change detection between revisions of a hardware board can be very messy. (There are web collaboration platforms specifically designed for building open source hardware, including Upverter and Autodesk Circuits; however, these platforms are in very early stages and not yet ready for primetime).

GitHub is not the only place where you can find the ingredients to embedded systems with JavaScript. You can also turn to Hackster.io (where you can post projects and compare your design with those created by other members), or OSH Park and Tindie (where you can find ideas to assemble circuits or buy—and sell—directly from and to other makers). In addition, companies such as Seeed Studio, SparkFun, Adafruit, Amazon, and eBay offer a variety of kits and ready-made devices. If you are stuck with building hardware, you can find instructional videos on YouTube or Vimeo about basic and advanced electronics.

When building hardware, you also need some special equipment to solder, laser cut, or 3D print objects. Fab Labs are one solution to this problem. Fab Labs are low-cost fabrication laboratories (hence the name “Fab Lab”) where you can rent equipment to print, assemble, and debug hardware for your own needs. (Neil Gershenfield from MIT is one of the main inventors of Fab Labs. Watch his talk to find out more.) It is amazing to see how blueprints from the Internet can transform into physical objects in a Fab Lab. It is equally amazing to see how your hardware skills and know-how will evolve if you visit a Maker space regularly.

No doubt, building hardware can be fun. But much of the secret sauce in embedded development is in the software—and this is where the opportunity for JavaScript developers arises. Because they are already familiar with the importance of events, interactions, and user experience, JavaScript developers can potentially build physical experiences with embedded devices that feel more natural and human than similar experiences with other languages.

To achieve this goal, a prospective embedded developer must master elements from both web technologies and robotics. In web technologies, you will need skills such as JavaScript, CSS, and SVG to build graphical user interfaces for devices, as well as web servers and protocols to manage communication across networks. On the physical side, you need to understand the different approaches for controlling physical systems and robots themselves.

JavaScript will play a major role in a more connected society because JavaScript is the programming language for the Web. Once you become comfortable with its asynchronous programming model, you’ll find that JavaScript offers elegant solutions to dealing with data and functions in networks and embedded systems too.

In addition to JavaScript and web technologies, this book will touch on some ideas about electronic circuits and their applications to the Internet of Things (IoT). There are other books that are much more focused on how to build these types of products, but this book provides a starting point.

It won’t be too long before we will be using web browsers to monitor traffic in a city, the contents of a warehouse, or progress from a personal workout where data is captured from sensors in clothes. And, of course, building these systems makes sense only if we can share the results with friends, colleagues, and peers. As embedded developers for the web, you’re on the leading edge, inventing this new world.

If you’ve worked with open source software before, you’re probably familiar with the GitHub slogan: “Build better software, together.” Hopefully this book can help you to promote a new idea: “Build connected systems, together.”

Who This Book Is For

This book is primarily written for those who want to connect embedded devices to the Internet using Node.js, particularly engineers and designers who want to simplify configuration or monitoring of a device. For reasons we will discuss later, JavaScript is the language of choice in this book. But a number of ideas would also apply to other programming languages (e.g., Ruby, Python, or Arduino’s C++).

This book is limited to basic electronics. If you want to go beyond simple outputs such as the “blinking LED” or simple sensors for inputs, you will need to check out other books on embedded systems after reading this one (we’ll provide pointers on where to go next).

Finally, this book assumes some basic experience with JavaScript. It is OK if you didn’t enjoy JavaScript in the past, and maybe preferred working with Ruby, C, or Python. Hopefully, this book can help you to reconsider that viewpoint and make you want to experiment with JavaScript to build new kinds of connected devices.

How to Use This Book

This book is organized into the following chapters:

Chapter 1, Connecting Worlds

Here we’ll explore JavaScript’s role in connecting devices. After a general overview, this chapter delves into more technical details. For those new to JavaScript or Node.js, this chapter will provide some basic information about npm, Node.js modules, buffers, and streams.

Chapter 2, Blink with Arduino

An LED that blinks is part of many embedded systems. It is also the “Hello, World!” of hardware. Using an Arduino as an example, we’ll look deeper into the functional building blocks of a microcontroller and how they can be controlled with JavaScript from the outside.

Chapter 3, Espruino

This chapter discusses Espruino, which lets you program a microcontroller directly with JavaScript without the need for a host computer. Espruino provides a very lean JavaScript parser that only requires a couple of kilobytes of RAM and flash memory.

Chapter 4, The Tessel 2

Next, we’ll review how to get started with Tessel 2, a development platform for prototyping Internet of Things devices.

Chapter 5, Particle Photon

In this chapter, we’ll investigate JavaScript toolchains for connecting a microcontroller to the Internet. Our discussion will focus on the Particle Photon.

Chapter 6, Single-Board Computers

The Raspberry Pi is a popular choice for media-related use cases. This chapter looks at different approaches to running Node.js on single-board computers. Various boards will be covered, including the Intel Edison.

Chapter 7, Components for Prototyping

This chapter discusses simple electronic circuits. As outputs, LEDs often indicate the state of a system. For inputs, different components, such as buttons and potentiometers, are usually required. Sensors can capture physical data, and you’ll learn some basics about them in this chapter.

Chapter 8, Node.js Libraries for Hardware

Now that you have a strong foundation in boards and components, we’ll move on to look closer at working with Node.js libraries for embedded devices. This chapter explores some of the abstractions for hardware. Good examples for talking to hardware with JavaScript can be found in the Johnny-Five library or serialport library for serial communication with Node.js.

Chapter 9, Exploring Network Protocols

Node.js comes with support for a number of different network protocols. After an overview of the Hypertext Transfer Protocol (HTTP), other network protocols are presented. The WebSocket protocol, which is used to push messages over a network, is particularly interesting.

Chapter 10, Web Frontends for Things

Many hardware projects require a user interface (UI). With JavaScript, you can easily build a UI in a web browser. For this, you’ll set up a project structure to work with a static HTML page. Then we’ll add some JavaScript and SVG for graphics.

Chapter 11, Entering the Cloud

This chapter takes a closer look at sending data with message brokers and MQTT. With MQTT, you can subscribe to updates from sensor devices. In addition, you get an overview of other cloud services for working with messages from devices.

Chapter 12, Making Robots with Node.js

Here we’ll cover some basics for building robots with JavaScript. We’ll also discuss different approaches for moving a robot using JavaScript.

Chapter 13, Wireless Data with Bluetooth

Bluetooth wireless communication is an important building block to remotely control devices. Some JavaScript libraries for Bluetooth Low Energy will be discussed.

Chapter 14, Toward the Physical Internet

This chapter includes general information about building physical experiences and the role of JavaScript. Instead of delving into technical aspects, the chapter provides a general picture of building digital interfaces in an analog world.

Chapter 15, From Products to Toolkits

As a summary, this chapter takes a broader look at the role of Node.js and embedded devices. Developing modular hardware and software is the main topic of this chapter. You will also learn more about open source hardware.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

This element signifies a tip or suggestion.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Using Code Examples

Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/embeddednodejs. Also, you can join the book community online by submitting issues at the book website. Additional resources for the book are published at http://embeddednodejs.com.

This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Node.js for Embedded Systems by Patrick Mulder and Kelsey Breseman (O’Reilly). Copyright 2017 Patrick Mulder and Kelsey Breseman, 978-1-491-92899-8.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of plans and pricing for enterprise, government, education, and individuals.

Members have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and hundreds more. For more information about Safari Books Online, please visit us online.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

  • O’Reilly Media, Inc.
  • 1005 Gravenstein Highway North
  • Sebastopol, CA 95472
  • 800-998-9938 (in the United States or Canada)
  • 707-829-0515 (international or local)
  • 707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at http://bit.ly/node-js-for-embedded-systems.

To comment or ask technical questions about this book, send email to .

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

From Patrick

The first ideas for this book resulted from conversations with Denise Jacobs. While I enjoyed working on my first book on building web applications with full-stack JavaScript, I unexpectedly was working with electronics and hardware again. Thanks to Denise, I could find common ground from both worlds. Also, thanks to Denise for submitting that O’Reilly SolidCon talk proposal!

SolidCon was helpful for this book in many ways. Thanks to Jon Bruner for organizing the conference and setting the frame for this book at O’Reilly. SolidCon provided a fantastic platform to discuss how machines can be built, printed, assembled, and programmed. On a philosophical note, SolidCon gave me new ideas for how hardware evolves into a medium for digital expression.

The workshop and discussion with Michael McCool helped me to better structure the material for this book—actually, the current structure of this book is strongly influenced by the workshop Michael gave at SolidCon about the Internet of Things. Also, thanks to Rex St. John, Alexander Tereschenko, and Matthias Hahn for their feedback on early versions of this book.

The book has benefited a lot from discussions I had with Kelsey Breseman from the Tessel project. Thank you, Kelsey, for joining this project as coauthor! Your holistic view on technology improved this book in many ways, and better prepares the reader to tackle the challenges that development of IoT devices pose.

My shift from software to hardware (and back to software) was triggered by a consulting project in Munich. Special thanks to Thorsten Bucksch, who hired me for this project. And thanks to Rainer Brunn, Philip Thurner, Willy Bristiel, and Ralph Mueller-Eschenbach for helpful discussions about electronics.

Many interesting discussions about the Internet of Things happen in makerspaces. Thanks to all friends and meetup members from the Munich Arduino meetup. The mix of learning, teaching, and experimenting shows the power of peer-to-peer networks. In particular, thanks to Thomas Schütt, Augusto Redolfi, Carlos Morras, Rüdiger Freese, Horst Altmann, and Erhard Waretzi for answering “stupid” questions. While I studied electronics many years ago, a lot of its fun came back with exploring Arduino projects. Thanks to Massimo Banzi and his team for building Arduino. And thanks to Dan Hienzsch for building the I2C Education shield.

As the JavaScript community for embedded systems is just evolving, thanks to Rick Waldron for the work on many Node.js libraries that simplify working with embedded systems. Thanks to Ron Evans and Adrian Zankich for presenting Cylon.js at ScottlandJS in 2014. Thanks to James Halliday for publishing fantastic Node.js modules and for providing early feedback on this book’s draft.

Thanks to Jonathan Carter for contributing a discussion about the usage of the Node-Red library.

Without a doubt, this book could not have been written without an “invisible hand.” In this case, I want to thank Nan Barber. Writing is hard to plan. Writing is also slow at times, and it’s easy to get lost in details. Thanks to Nan for helping to keep us on track. Also thanks to Jasmine, Colleen, and the O’Reilly production team for turning the book draft into a finished book.

For the technical review and feedback on the draft of this book, I want to thank Kevin Sidwar, Rick Waldron, Guido Burger, Gordon Williams, Sandeep Mistry, and Kelsey Breseman.

Last, but not least, I want to thank Béatrice for her love, design inspirations, and support in my writing ambitions.

From Kelsey

My acknowledgments are short because I joined the book quite late in its creation. Primarily, I want to thank Patrick for inviting me into this adventure. It has been a delight to pass ideas and musings back and forth about Node, hardware, and where the Internet might be going.

Thanks also to Jon McKay for always reading over my shoulder, fielding my technical fact-check questions, and generally being supportive.

Get Node.js for Embedded Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.