Developing Machine Learning IoT Apps with Node-RED and TensorFlow.js
Tags: javascript, iot, tensorflow, nodered
Published: 2020-01-28 16:09:29

---
title: Developing Machine Learning IoT Apps with Node-RED and TensorFlow.js
published: true
tags: javascript,iot,tensorflowjs,nodered
canonical_url: https://medium.com/codait/a-low-code-approach-to-incorporating-machine-learning-into-your-iot-device-24f3f2a70717
---

> _cross-post from [medium.com](https://medium.com/codait/a-low-code-approach-to-incorporating-machine-learning-into-your-iot-device-24f3f2a70717)_

#### A low-code approach to incorporating machine learning into the Internet of Things

![](https://miro.medium.com/max/552/1*Qjix2iqRLBpH4cMTm-NfxQ.png)<figcaption>A comic where a guy, frowning, says to another, "My coffee machine has unfollowed me." <a href="http://geek-and-poke.com/geekandpoke/2012/3/14/the-internet-of-things.html">The Internet of Things — Geek & Poke</a></figcaption>

Gartner has predicted the number of connected devices will rise to over [25 billion by 2021](https://www.gartner.com/en/documents/3890506/top-strategic-iot-trends-and-technologies-through-2023). And given the variety of devices out there, getting started with IoT can appear daunting. Setting up communication with or between these devices is often non-trivial. Further challenges arise if you want to integrate machine learning! Solutions need to pull together different device APIs, services, and sometimes protocols.

[Node-RED with TensorFlow.js](https://developer.ibm.com/patterns/develop-a-machine-learning-iot-app-with-node-red-and-tensorflowjs/?utm_medium=OSocial&utm_source=Blog&utm_content=000019RS&utm_term=10004805&utm_id=CODAIT-medium-TFjs-Node-RED-CodePattern&cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-CODAIT-medium-TFjs-Node-RED-CodePattern&cm_mmca1=000019RS&cm_mmca2=10004805) brings machine learning to IoT in an easy, low-code way. It opens new, creative approaches to enable machine learning for the Internet of Things. Image recognition, audio classification, etc. all possible on device with minimal code.

### Enter Node-RED

[Node-RED](https://nodered.org/) is a flow-based visual programming tool. With its browser-based editor you can simply wire together hardware devices, APIs and online services to create your application. You develop powerful applications by connecting nodes instead of writing code. And you can deploy them with a single click.

{% youtube ksGeUD26Mw0 %}<figcaption><a href="https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6">Node-RED Essentials playlist</a></figcaption>

Node-RED runs on local workstations, the cloud, and edge devices. It has become an ideal tool for the Raspberry Pi and other low cost hardware.

The Node-RED runtime is lightweight and built on top of Node.js. It takes full advantage of Node.js’ event-driven, non-blocking I/O model. There is also the added benefit of tapping into the most used programming language — JavaScript!

With all the Node-RED community resources and vast NPM ecosystem, you can create IoT flows in a user-friendly manner that are [imaginative](https://github.com/johnwalicki/Node-RED-DroneViewer) and [help save lives](https://developer.ibm.com/callforcode/blogs/call-for-code-2019-finalist-prometeo/?utm_medium=OSocial&utm_source=Blog&utm_content=000019RS&utm_term=10004805&utm_id=CODAIT-medium-CallForCode-Prometeo&cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-CODAIT-medium-CallForCode-Prometeo&cm_mmca1=000019RS&cm_mmca2=10004805).

### Hello TensorFlow.js

[TensorFlow.js](https://www.tensorflow.org/js) is an open source JavaScript library. It permits you to build, train, and run [machine learning models in the browser](https://medium.com/codait/bring-machine-learning-to-the-browser-with-tensorflow-js-part-i-16924457291c) and Node.js.

{% youtube pbCExciEbrc %}<figcaption>TensorFlow.js intro</figcaption>

Often, enabling AI capabilities involves sending the data from a device to a server. The calculations happen on the server and the results returned to the device to take action. This is not ideal when data security or network reliability is a problem.

But, with TensorFlow.js there is an increase in privacy and data security. The data does not leave the device! Training and predictions can happen directly on the device collecting the data. This also makes it possible to run offline and in remote locations with no connectivity.

### Node-RED meet TensorFlow.js

The combination of Node-RED and TensorFlow.js means you can build IoT apps that use machine learning simply by dragging and dropping. Drag and drop a machine learning node, wire it up, and deploy to your device.

![](https://miro.medium.com/max/1200/1*Qsz62TxlYhrd7ZGYFHbvwg.png)<figcaption><a href="https://github.com/IBM/node-red-tensorflowjs">Node-RED node with a TensorFlow.js Object Detection model</a></figcaption>

But to get to that point you need to have the TensorFlow.js nodes available. TensorFlow.js nodes are starting to show up in the [Node-RED library](https://flows.nodered.org/search?term=tensorflow) and [across](https://github.com/vabarbosa/tfjs-node-red) [GitHub](https://github.com/tonanhngo/nodered-tfjs) [repos](https://github.com/yhwang/node-red-contrib-ds2-tfjs) and more are being released regularly. These nodes provide various machine learning functionality to add to your flow. But what if there isn’t a TensorFlow.js node for your machine learning task? You can [create it](https://nodered.org/docs/creating-nodes/)!

The extensibility of Node-RED allows you to create custom nodes for your needs. Packing Node-RED nodes is similar to packaging Node.js modules, but with some extra information.

A Node-RED node consists of three main files:

- **[package.json](https://nodered.org/docs/creating-nodes/packaging#packagejson)**: standard file used by Node.js modules, but with an added `node-red` section
- [JavaScript file](https://nodered.org/docs/creating-nodes/node-js) that defines the node’s behavior
- [HTML file](https://nodered.org/docs/creating-nodes/node-html) that defines the node’s properties, edit dialog and help text

The [JavaScript file is where you would wrap your TensorFlow.js code](https://github.com/IBM/node-red-tensorflowjs/blob/master/node-red-contrib-tfjs-object-detection/node/tfjs-object-detection.js#L24-L59). It would load the TensorFlow.js model and run the prediction.

Once bundled the custom node is available to wire into a flow and deploy.

### There may be challenges

As straightforward as it may appear, there can still be challenges and concerns to keep in mind.

Since, you are dealing with edge devices performance is an utmost priority. Models may be too big to load onto an edge device. Or may require specific optimizing to perform well in JavaScript.

Also, when in the node’s life-cycle should you load the model? Should you have a single node to process input/output and run prediction? Or split the work across two or three nodes?

The TensorFlow.js model you are using and the specific use case it addresses often dictate the approach and answers to a lot of these concerns.

### Getting started

Combining TensorFlow.js with Node-RED lowers the barrier to entry into machine learning. From the drag-and-drop interface to the one click deploy, IoT enthusiasts and developers can incorporate machine learning in an accessible and rapid manner.

Visit the [Node-RED and TensorFlow.js code pattern](https://developer.ibm.com/patterns/develop-a-machine-learning-iot-app-with-node-red-and-tensorflowjs/?utm_medium=OSocial&utm_source=Blog&utm_content=000019RS&utm_term=10004805&utm_id=CODAIT-medium-TFjs-Node-RED-CodePattern&cm_mmc=OSocial_Blog-_-Developer_IBM+Developer-_-WW_WW-_-CODAIT-medium-TFjs-Node-RED-CodePattern&cm_mmca1=000019RS&cm_mmca2=10004805) to check out a sample solution. Learn more by viewing the full code and deploying the tutorial.

![](https://miro.medium.com/max/2348/1*ONXzgEyVHm97hgFtAid4hA.png)<figcaption><a href="https://flows.nodered.org/add">Contribute to the Node-RED Library</a></figcaption>

Check out the [Node-RED library](https://flows.nodered.org/) for more TensorFlow.js nodes, flows, and collections as they become available. Anyone is [welcome to contribute there](https://flows.nodered.org/add) so that others can learn from your work.

* * *