Ultimate Game
I’m not just calling this an Internet of Things (IoT) article because it mentions Raspberry Pi, but I can confidently say this is an Internet Automation project that solves a small problem without extra actions on my part.
In this article, I will provide a wealth of useful external resources to help us quickly familiarize ourselves with the Node.js and JavaScript parts involved in the project.
Problem Description
In the cold winter, we want to use a heater to keep our home/office at a warm and comfortable temperature. However, using a heater in a closed environment affects indoor humidity. We solved this problem with a humidifier! In this article, I will use a portable humidifier connected to a socket.
My goal is to automatically adjust the humidifier’s switch based on the humidity level in the room.
How to Achieve This?
Hardware: If we can achieve automatic control of the socket (or switch) that the humidifier is connected to, then the problem is solved. Therefore, we need a socket that can be perfectly controlled via the internet. For this functionality, I chose the Belkin Wemo Switch.
(Note: Many portable humidifiers also come with an automatic switch or timer, but we are focusing on DIY, so we won’t discuss that here.)
The IFTTT Maker platform provides us with (registration required to use their services):
1. Connect the Wemo switch;
2. Use the Maker API to control the Wemo switch state (created based on your account and device).
Clearly, we also need a sensor that can connect to the Raspberry Pi. I strongly recommend using the Raspberry Pi SenseHAT instead of separately connected sensors, as the SenseHAT integrates many sensors and acts like a hat on the Raspberry Pi. It also provides a rich Python library to read sensor data.
Finally, we need to connect the Raspberry Pi to the internet. Simply plug the WiFi module into the USB port of the Raspberry Pi to achieve this. Of course, WiFi needs to be set up on the Raspberry Pi. (Tip: Use Linux commands or an IDE)
Software: Since the Raspberry Pi can run operating systems like Linux and Windows, it is easy to install Node.js on it. Therefore, Node.js is the backbone we use to read data from the sensor and call the API to control the Wemo Switch state through the IFTTT Maker platform.
As mentioned earlier, we will also rely on a lot of Python scripts provided by SenseHAT. We won’t introduce these Python scripts here, as they can be found in the SenseHAT API manual. However, we will discuss how to use Node.js applications to drive these Python scripts.
Node.js App, this application is implemented in three steps:
Drive the Python script to read sensor data (which can be temperature or humidity data) at a fixed frequency Compare the read data with preset conditions to determine whether to turn off the Wemo SwitchCall the IFTTT Maker interface to turn the Wemo Switch on or off,Next, let’s look at the specific code for each step:
Step 1: Read Sensor Data
Node.js provides the child_process module, and one of its methods, exec, can execute operating system commands. This step requires this module to drive the Python script. Remember, we want to execute the Python script at a fixed frequency, so we need to wrap it in JavaScript’s setInterval function.
setInterval(function(){
require(‘child_process’).exec(command, function(error, stdout, stderr) {
if (error == null) {
var data = stdout.replace(“\n”,””);
// … Call Stage 2
}
else {
console.log(“Error occurred. ” + error);
}
}); child_process.exec
}, 60000); // frequency = 60 seconds
Step 2: Determine Conditions
Here we simply compare the information read in the previous step. However, you can add other functionalities here, such as storing data in a database, keeping track of the last few read values, determining whether the recent values are consistently increasing or decreasing, etc. Here we will base our judgment on the most recent sensor data.
// assumption: We are looking for Temperature in Stage 1
var YOUR_API_KEY = “?????”; // obtain this from IFTTT
var wemoState = “off”; // or as defined in IFTTT Maker
if(data > 35) { // deg. Celsius is what SenseHat APIs return
wemoState = “on”;
}
// … Call Stage 3
Step 3: Call IFTTT Maker Interface
As mentioned earlier, we will not discuss how to create an IFTTT Maker API to control the state of a device or call another API. You can refer to the manual or this blog.
Next, it’s time to actually use Node.js to call the interface, and we will use Node.js’s https module, which provides the request method.
var makerAPI_host = “maker.ifttt.com”;
var makerAPI_path = “/trigger/”+wemoState+”/with/key/”+YOUR_API_KEY;
var https = require(‘https’);
var optionsget = {
method : “GET”,
host : makerAPI_host,
port : 80,
path : makerAPI_path
};
var reqGet = https.request(optionsget, function(resp) {
var str = “”;
resp.setEncoding(‘utf8’);
resp.on(‘data’, function(d) { // data chunk
str += d;
});
resp.on(‘end’, function() { // all data sent
console.log(str); // We are Done!
});
});
reqGet.on(‘error’, function(e) {
error = {
message : “Error occurred”,
error : e
};
console.log(error);
});
reqGet.end();
Alternatively, you can also use the child_process.exec() method from the second step.
var callAPI = “curl -X GET https://maker.ifttt.com/trigger/”+wemoState+”/with/key/”+YOUR_API_KEY;
require(‘child_process’).exec(callAPI, function(error, stdout, stderr) {
if (error == null) {
console.log(stdout); // We are Done!
}
else {
console.log(“Error occurred. ” + error);
}
}); child_process.exec
Summary
In conclusion, this article aims to demonstrate the different modules of Node.js on the Raspberry Pi, and the DIY project leaves room for expansion. One reuse method I thought of is to use the same device to control any internet-connected device, such as a Nest thermostat, creating more interesting Internet Automation DIY projects.
Disclaimer: This article is a network reprint, and the copyright belongs to the original author. If there are copyright issues, please contact us, and we will confirm copyright and pay remuneration or delete content based on the copyright certificate you provide.