We are now going to see how to build the code for the thermostat, which will run on your Raspberry Pi Zero board. As the code is quite long, I will only highlight the most important parts here, but you can of course find the complete code inside this book's GitHub repository.
Start by importing the required modules:
var sensorLib = require('node-dht-sensor'); var express = require('express');
Then, we create an Express
app, which will allow us to easily structure our application:
var app = express();
Next, we define some variables that are important for our thermostat:
var targetTemperature = 25; var threshold = 1; var heaterPin = 29;
The threshold is here so the thermostat doesn't constantly switch between the on and off states when it is near the target temperature. A lower threshold means that you will have a temperature closer to what you want, but also that the heater will switch more frequently.
After that, we are going to define the routes that will structure our application...