See the project on GitHub:


But for now, enjoy the transcript. 🙂

Whether you like it or not, the internet of things, or IoT, is here to stay. This includes devices like smart thermostats, home speakers, and even our Kodi Android Remote. Our Raspberry Pi texting door sensor partially falls under this category because it uses the internet to communicate with us but we can’t talk back. In this case, talking back would be something like remotely arming or disarming the system, which is possible with a web server (using standard HTTP get or post requests) and an accompanying app.

It turns out Node.js with Express is a reliable, popular, and easy-to-use framework for not only web apps, but also for IoT.


What’s up guys, welcome to AR Tech Tuts and today, we’re going to IoT-ify our door.


— intro —


This is actually going to be a multipart series so stay tuned. In this first part, we’ll simply be checking out how to use the GPIO interface in Node.js while also setting up a basic web server. Ensuing parts will feature developing an Android app to the specifications set by the server.

Now, I’m not going to go over much of the electronics logic behind the sensor here so check out the original Python sensor video before continuing. For reference, we’re connecting a normally closed reed switch to ground and checking for a floating value, which will indicate an open door.

Assuming you’ve already installed the latest versions of Node.js and NPM, open up the terminal and type “npm init” which will let us enter some details about our project.

Once that’s done, we need to install several things using “npm install”: express, pigpio, debounce, and nodemailer.

For you rebels that didn’t watch the first video, I’ll get to you in a bit. For those that did watch it, please note the wiring corrections on screen. For everyone else, connect 2 wires to the ground and GPIO pins on the Raspberry Pi. Then in series connect, a resistor to ground, then connect that to the ground or common node on your reed switch. Then connect a wire from the normally closed node back to the GPIO pin. If you have a normally open switch, the digital values later on will be flipped.

Back to the code, let’s strive for more modular file organization by separating the code responsible for the GPIO and the texting. Let’s create the subfolders: API, and RPi, and create index.js. The code is almost a carbon copy of the old Python code with some notable differences. First, notice how we’re using interrupts instead using polling since Node.js will generally execute things quicker than Python. Also, we have a new variable named “armed” which if true, will allow us to perform a user-specified function called a callback. Then, we return 2 functions: destroyRpiInstance, which turns off the pull-up resistor, and setArmedState, which is self explanatory. However, none of this is visible when we import (or rather require) this file. So at the bottom, type “module.exports = createRpiInstance”, a function which is kind of like a constructor if you want to think about it that way

Now let’s create the texting or mailing module. Create another folder named mail inside api, and create another index.js. Also, at the root of the project, create cred.json — a file where we’ll store our email credentials including the SMTP server address, the port, your email, the recipient SMS gateway address, and your password. Please note that you might not be able to successfully log in if you use two-factor authentication.

Back to the mail index file, the code is mostly straight from the nodemailer documentation  with the exception of us reading the email information from the json file and allowing custom text to be sent in the function inside the JS object we’re exporting. For Node beginners, yes, you can export basically anything in module.exports and if we don’t need to instantiate anything like in RPi index, we can straight up export an object instead of a function.

Now for the good stuff. Create and open index.js in the root directory. Now let’s require and instantiate Express, which will help us with handling HTTP requests, and also simply require the two modules we just made. Then, let’s set up our message constants, an array to store all events which the user can request for, and the function to interact with the mail module.

Then set the GPIO scheme pin number constant and let’s instantiate our RpiInstance like a function, passing the arguments pin, and a new callback function.

Our server will allow 3 requests: one to just verify it’s up, one to retrieve the events, and one to set the arm state. First, just the verification. We handle the get request the following way:

Type: app.get(‘/’, function(req,res){ }); Then inside the function, we’ll send the response as JSON with res.status(200).json({msg:”Connected!”});

We use status 200 but that tells the browser that there was nothing wrong. Simple enough?

For the events, do the same thing except the URL will read ‘/events’ and we will be sending {events:events}

Setting the arm state is going to be a little bit different since we can have multiple state values. To read the values, the URL will be ‘/arm/:state’ then we can refer to it using req.params.state. Then using that value, we can set the armed state of our system.

Obviously, it’s not very secure now but whatever…

Next let’s do cleanup on exit.

Now let’s actually start the server on either the default port specified by your Node environment or port 8080 by default.

Then let’s test it using the command “node index.js”

First, let’s test getting the events. Now let’s test the arm state. As you can see, everything works as intended!

So thanks for watching this video guys! Hope it helped. Subscribe for part 2 coming soon. In the meantime, feel free to check a recent video, like, share, subscribe, and goodbye, don’t die, and stay dry.