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

While I napped, we got a new apt – Debian apt command cheat sheet

aptI’m now awake, everyone!  I must have been asleep (and perhaps you were too) since the Debian apt ecosystem now includes an “apt” binary that vastly improves the way we interact with the Debian package management ecosystem.  You can now use the single apt executable instead of apt-get, apt-cache, etc. for many common use cases.  These are groundbreaking changes for the better! My hope is that this post will accelerate your productivity whether you are using Ubuntu, Raspbian, or one of the many other other Linux distros based on Debian.

Scenario: You want to install the cowsay package because you know that a Linux system is not complete without it. 🙂


Given this context, let’s get started using the new, simplified apt binary:

apt update

Updates the local APT package index, a database of available packages available from the central Debian repositories configured for your machine.  It is a good idea to run this command periodically, especially before installing packages or doing upgrades.  You will probably need to add a “sudo” before this command since higher privileges will be required to complete this operation.

apt list cow*

List all packages (whether installed on your system or not) containing the word “cow” followed by anything.  If you run “apt list cow", it will only find packages with an exact match of “cow”.

apt search cow

Searches for “cow” in both package names and descriptions and returns the package name and a brief description.  Note that you don’t need to use a wildcard with this command since it will find “cow” contained in strings such as “cowsay” and “cowbell”.

apt show cowsay

Shows package details including a detailed description. You can use wildcards, but this command works best if you specify the exact package name obtained from a command such as apt list above.

The following commands will most likely need to be prepended with “sudo“.  I am not including “sudo” to avoid line noise so we can focus on the essence of each of the commands.

apt install cowsay

Installs the cowsay package. As a bonus, the new apt command also includes a progress bar in your terminal so you can more easily visualize progress.

apt remove cowsay

This command will remove the cowsay package.  I’m not sure why you’ve ever want to remove this very essential package though. 😉

apt full-upgrade

This command upgrades your system by removing, installing, and upgrading packages.  This is typically the command most people will want to run to get their systems up to date.  It is often used with the “apt update" command and can be combined into one command as follows if you want to show off your command line prowess:

sudo apt update && sudo apt full-upgrade

apt upgrade

This command upgrades your system by installing and upgrading packages.  It will automatically install, but will not remove any packages.  The apt full-upgrade discussed previously will be the command of choice for most in doing upgrades.


With these simple commands above as a reference, you should be able to accomplish 99% of what you need to work with packages in the Debian Linux world.  Spread the word about these new apt command options.  They are quite handy!

Follow @thisDaveJ on Twitter to stay up to date on the latest tutorials and tech articles.

Related Articles

Beginner’s Guide to Installing Node.js on a Raspberry Pi

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

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

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

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>

  -h, --host        Upstream server providing forwarding  [default: ""]
  -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 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 “” 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.

Create a Web Server in Node without any Code

Learning through Making (LTM) logoWelcome back!  In a previous tutorial (Beginners Guide to Installing Node.js on a Raspberry Pi), we installed Node.js on a Raspberry Pi. We are now ready to continue our LTM (Learning through Making) tutorial series and build a simple web server without any code.  While this tutorial is geared toward the RasPi, the instructions can be easily mapped over to other platforms such as Windows, Linux, and OS X.  Let’s get started! Continue reading

Solution for Can’t Ping Raspberry Pi Hostname on the Network

In preparing for my upcoming tutorial which is a beginner’s guide to installing Node.js on a Raspberry Pi 2, I ran into an issue. After getting the RasPi is up and running on the network, I was not able to ping its hostname (raspberrypi, by default) from another machine and find it so I could connect to it through Putty, xrdp, VNC, etc.  After all, I wanted to be able to run headless and disconnect the monitor, USB keyboard/mouse, and still connect to it from another machine on my network.

One option was to run ifconfig on the RasPi and take note of the IP address for eth0 (if connected through Ethernet) or wlan0 (if connected through Wi-Fi).  I could then hard code this IP address in the hosts file on the Windows (or other) machine. The problem is that the RasPi retrieves its IP address through DHCP by default from my local router at home, and this IP address is not guaranteed to remain the same.  I could log into my router and note the static IP address range and reconfigure the RasPi to use one of these static IP address rather than DHCP.  However, I am preparing a beginner’s tutorial and not all of my readers want to become Linux TCP/IP networking experts.

I discovered an elegant solution if you are trying to ping and connect to the RasPi from another Windows machine on the same network.  Samba to the rescue! Continue reading