Making Interactive Node.js Console Apps That Listen for Keypress Events

keypress

In preparing for an upcoming Node.js IoT tutorial focused on creating¬†notifications based on sensor values (for example, audible alerts), I wave of brilliance came over me. ūüôā ¬†Wouldn’t it be awesome if these notifications could be interactively acknowledged and silenced from the console? Lo and behold, I discovered that Node.js CLI (console) applications can indeed respond to keystrokes, ushering in a whole new realm of possibilities. ¬†In today’s tutorial, I will teach you how to build interactive Node.js console applications that listen for keypress events. We’ll build a simple stock quote application to bring this interactivity to life. Continue reading

Share

Node.js IoT: Tracking the ISS through the Sky

ISS

We’re back today to embark on another¬†cool Node.js IoT project. ¬†This time, we’re going to interact with the¬†International Space Station (ISS) and track it as it flies through the sky. ¬†We’ll eventually work with physical sensors that sit¬†right on our desk, but at this stage we won’t need to buy parts or read resistor¬†color codes in order to retrieve values from the ISS GPS “sensor” in the cloud‚ÄĒor actually 250 miles above the clouds.

While our tutorials are geared toward creating awesome Node.js IoT projects on the Raspberry Pi, any Node.js-capable machine will suffice for today’s tutorial. ¬†Other useful articles to help you may include my¬†Beginner‚Äôs Guide to Installing Node.js on a Raspberry Pi. ¬†You can also see my article on Using Visual Studio Code with a Raspberry Pi if you are in need of¬†a development environment.

Let’s get started and¬†progressively build a solution so we can track the ISS and ultimately¬†monitor its location relative to¬†our location on earth. Continue reading

Share

Node.js IoT – Data Visualization of Sensor Values

Sensor Data Viz

In today’s article, we’re moving beyond printing numbers in the console and creating¬†some data visualization plots in both the terminal and in a graphical window. We’re also going to have fun! ¬†ūüėÄ

Today, I’m going to make the inductive leap that you’re making all of this happen using a Raspberry Pi. You may be able to¬†implement¬†these amazing ASCII terminal plots in the Windows world using¬†Bash on Windows, but I have not tested in that context. ¬†In addition to Raspbian, these steps will¬†also generally work for other Linux distros as well as OS X. ¬†If you are not running Node.js on your Raspberry Pi, please see my¬†Beginner‚Äôs Guide to Installing Node.js on a Raspberry Pi. ¬†You can also see my article on¬†Using Visual Studio Code with a Raspberry Pi if you are seeking to set up a development environment. ¬†For this tutorial, the Leafpad text editor, installed by default¬†with Raspbian, may suffice. Continue reading

Share

Node.js IoT – Create Local Module for CPU Sensor

CPU Sensor Local Module

We’re back and ready to do some refactoring of our CPU sensor so we can learn about Node.js modules and how to¬†create them. ¬†Building small, focused modules is one of the key tenets of the Node.js philosophy as summarized in The Node Way:

Building small, single-purpose modules is at the heart of the Node.js philosophy. Borrowing from Unix, Node.js encourages composing the complex and powerful out of smaller, simpler pieces. This idea trickles down from entire applications (using the best tool for the job vs. a full suite) to how the tools themselves are built.

Continue reading

Share

Node.js IoT – Build a Cross Platform CPU Sensor

cross platform cpu sensor

I took a little hiatus in our series to take my family on a trip to Japan with layovers on each end of the trip in China which included a ride on the Shanghai Maglev Train, the fastest train in the world.  We had a fantastic time, and it was a great educational experience for the kids.  It is also good to be back home!

We are back again with our Node.js IoT tutorial series and ready to continue developing our “CPU sensor” as¬†CPU loading/utilization is a “sensor” we can measure, record, and ultimately stream to other locations. ¬†Today, we will expand our CPU sensor and make it cross platform‚ÄĒand learn more about Node.js in the process.¬†In future tutorials, we will harness¬†the power of¬†Node.js to interact with physical sensors that live outside of our computing environment.¬† Continue reading

Share

Node.js Learning through Making – Build a CPU Sensor

cpu sensor
We are back with our LTM (Learning through Making) tutorials and ready to hit the ground running and write some real Node.js code! ¬†In this series, we will learn about Node.js in the context of creating IoT (Internet of Things) projects. ¬†We will build¬†a “CPU Sensor” in this first project since CPU loading/utilization is a “sensor” we can measure, record, and ultimately stream to other locations. ¬†In future tutorials, we will harness¬†the power of¬†Node.js to interact with physical sensors that live outside of our computing environment. Continue reading

Share

Using Visual Studio Code with a Raspberry Pi (Raspbian)

Learning through Making (LTM) logoWe’re back¬†with¬†our LTM (Learning through Making) series of Node.js tutorials and we’re gearing up and getting ready to write some code! ¬†We’ve learned how to build a Raspberry Pi from the ground up including Node.js, we’ve created¬†a web server in Node without code, and we’ve even¬†managed to get¬†this web server on the Internet¬†as a cool trick.

So how are we going to write Node.js code? There’s nothing that would stop us from simply jumping onto our RasPi and using the Leafpad text editor or even the nano console-based editor to write our code. ¬†I propose that we use some more robust tools in the form of an IDE (Integrated Development Environment) to help us along in our coding journey.

It turns out that the RasPi 2 and RasPi 3‚ÄĒin spite of the¬†significant CPU/memory boost they offer over their predecessors‚ÄĒwill run most IDEs¬†a bit sluggishly and will be¬†frustrating for any serious Node.js work. ¬†I¬†offer¬†here a creative alternative we will use to expedite the software development lifecycle and run the Node.js code natively on the Pi. Continue reading

Share

Visual Studio Code Jumpstart for Node.js Developers

VS Code iconVisual Studio Code is an amazing, lightweight code editor and works great for Node.js development. I like it so much that I am recommending it for those doing the free Learning through Making (LTM) tutorial series I am creating.  My Using Visual Studio Code with a Raspberry Pi (Raspbian) article is closely coupled to this article; however, this article is intended to help you develop with VS Code whether you are using a RasPi or not.

Now, let’s get started with first things first. This article is geared toward awesome, cutting edge people who want to develop with¬†Node.js and leverage the latest¬†ES6 (ES2015) features rather than being content living in yesteryear.¬† ūüôā ¬† Am I resonating with you and describing who you are or who you want to be? ¬†If so, let’s get started. Continue reading

Share

Make Your Raspberry Pi Web Server Available on the Internet with Node.js

web server imageIn Create a Web Server in Node without any Code, we used the http-server npm module to create a web server and serve up files in record time.  Today I will share a way you can take the web server you built and make it available beyond your local network and on the public Internet.  This is pretty cool trick, but I recommend that you use this with caution as this will open up a hole in your router/firewall when you make your web server available to the world.

Once again, we are using Node.js and so this will work on a variety of platforms. ¬†I am focusing here on making this work in the context of Raspberry Pi which is an amazing little machine for IoT applications and everything else under the sun.¬†Ok, let’s get rolling.

Log into your RasPi from a Windows system by launching a Windows Remote Desktop Connection.  (Hit the Windows key on your keyboard and type Remote Desktop Connection to get started.)  This method of remote connection works since we installed xrdp on the RasPi as part of our Beginners Guide to Installing Node.js on a Raspberry Pi 2. Of course, you can connect the RasPi directly to a monitor/keyboard/mouse or use other remote options such as an X Server and come in through Linux, Mac OS X, etc. too.

After you are in the RasPi, launch a terminal session and type the following:

$ sudo npm install -g localtunnel

This will install the npm package called localtunnel globally so it is available as a command anywhere on the RasPi rather than just in the current directory.  The localtunnel package npm page summarizes the mission of this package well:

localtunnel exposes your localhost to the world for easy testing and sharing! No need to mess with DNS or deploy just to have others test out your changes.

Now that we have localtunnel installed, let’s see what kind of parameters it accepts. ¬†You will notice that we invoke localtunnel with “lt” which is the command that is created as part of the global installation.

$ lt --help
Usage: lt --port [num] <options>

Options:
  -h, --host        Upstream server providing forwarding  [default: "http://localtunnel.me"]
  -s, --subdomain   Request this subdomain
  -l, --local-host  Tunnel traffic to this host instead of localhost, override Host header to this host
  -o, --open        opens url in your browser
  -p, --port        Internal http server port  [required]
  --help            Show this help and exit  [boolean]
  --version         Show version number  [boolean]

We see a very nice, simple help page. ¬†I’m going to use the single-character options¬†(e.g. -p) instead of the long¬†option parameter names (e.g. –port). ¬†Please note that you must use two dashes (–port) when using long option names rather than one dash in order for localtunnel to understand and honor the long option name parameters you are passing.

We first need to get our Web server back up and running so that we can make it available to the world.  Launch a new terminal session (leave the other first terminal session open still) and enter the following commands:

$ cd Public
$ http-server

This will make the index.html file and any other files you have residing in your /home/pi/Public directory available to be served up by the web server.

Next, jump back over to the original terminal session you launched so we can get localtunnel working. ¬†Let’s tunnel through to our web server running on port 8080 and launch the local web browser to view our web server from the outside world. ¬†(You can follow along by reviewing¬†the options that are listed in the help info above.)

$ lt -p 8080 -o

Fantastic! ¬†Upon launching the above command, we are assigned a random subdomain such as http://jxloprztp.localhost.me and we now have our web server available on the real Internet. ¬†Go to another machine besides your RasPi such as a laptop/desktop or cell phone so that you can see for yourself that your¬†website¬†has hit the big time and is available everywhere! ūüôā

You can now hit Ctrl-C in the terminal window running localtunnel to break the tunnel connection. Pretty amazing technology, eh?

Local tunnel has some other options. For example, you can choose your own subdomain rather than being assigned a random subdomain. ¬†Let’s try that now:

$lt -p 8080 -s mypizzaweb -o

Assuming nobody is actively using the mypizzaweb subdomain, you can now navigate to it by typing “http://mypizzaweb.localtunnel.me” in your Web browser address bar.

Once again, be sure to hit Ctrl-C in the terminal window running localtunnel to break the tunnel connection. ¬†It’s the wild west out there.

That’s it¬†until next time!

Follow Dave Johnson on Twitter to stay up to date on the latest tutorials and tech articles.

Share