Fundamentals 13 min read

Build a Smart Desk: Real‑Time Temp/Humidity & Presence Monitoring with Raspberry Pi

Learn how to create a smart workstation that continuously tracks temperature, humidity and occupancy using Raspberry Pi, DHT and ultrasonic sensors, logs data to Alibaba Cloud SLS, visualizes it, and explores IoT fundamentals, hardware components, and Node‑based front‑end integration.

Goodme Frontend Team
Goodme Frontend Team
Goodme Frontend Team
Build a Smart Desk: Real‑Time Temp/Humidity & Presence Monitoring with Raspberry Pi

Smart Desk Environment Monitoring

To create a comfortable working environment, the solution monitors temperature, humidity, and occupancy of a desk using a Raspberry Pi gateway, DHT temperature‑humidity sensor, and ultrasonic distance sensor.

Requirements

Real‑time temperature and humidity data.

Alert when temperature exceeds 30 °C or humidity drops below 40%.

Count the number of people passing the desk each day.

Technical Solution

Temperature and humidity data are collected by the DHT sensor, read by Node.js code on the Raspberry Pi, and sent to Alibaba Cloud SLS for storage and analysis. A scheduled Alibaba Cloud task aggregates the logs. For occupancy detection, an ultrasonic sensor measures distance, determines whether a person is present, and logs the result to SLS.

Materials

Raspberry Pi

DHT temperature‑humidity sensor

Ultrasonic distance sensor (HC‑SR04)

Code – Temperature & Humidity

<code>// Import DHT sensor library
const sensor = require("node-dht-sensor");
// Import SLS SDK
const { SendLog } = require("./sendLog.js");
const sls = new SendLog('sensor-show', 'sensor_log');
const DELAY_TIME = 6 * 1000; // 6 seconds
const SENSOR_TYPE = 11; // DHT11
const GPIO_PIN = 21;
setInterval(() => {
  sensor.read(SENSOR_TYPE, GPIO_PIN, function (err, temperature, humidity) {
    if (!err) {
      const contents = [
        { key: 'temperature', value: temperature.toString() },
        { key: 'humidity', value: humidity.toString() }
      ];
      sls.logPush(contents);
    }
  });
}, DELAY_TIME);
</code>

Code – Occupancy Detection

<code>// Import GPIO library
const Gpio = require("pigpio").Gpio;
const { SendLog } = require("./sendLog.js");
const trigger = new Gpio(20, { mode: Gpio.OUTPUT });
const echo = new Gpio(16, { mode: Gpio.INPUT, alert: true });
const sls = new SendLog('sensor-show', 'sensor-hcsr04_log');
const MICROSECONDS_PER_CM = 1e6 / 34321;
let startTick;
watch = () => {
  echo.on('alert', (level, tick) => {
    if (level === 1) {
      startTick = tick;
    } else {
      const endTick = tick;
      const diff = endTick - startTick;
      const distance = diff / 2 / MICROSECONDS_PER_CM;
      const contents = [
        { key: 'distance', value: distance.toFixed(2).toString() },
        { key: 'isAnyBody', value: distance < 20 ? "1" : "0" }
      ];
      sls.logPush(contents);
    }
  });
};
watch();
setInterval(() => {
  trigger.trigger(10, 1);
}, 1000);
</code>

Final Products

Charts display real‑time temperature and humidity; alerts are sent via SMS and email when thresholds are crossed. Adding an infrared emitter could allow automatic air‑conditioner control.

Ultrasonic data provides distance measurements, though accuracy can be affected by temperature, humidity, and surface reflectivity. For precise ranging, radar or infrared sensors are recommended.

What Is IoT?

IoT connects ordinary devices to a local network, forming a larger network that enables interaction with people.

Four‑Layer IoT Model

Perception & Device Layer : Sensors collect physical data; devices are the physical objects (e.g., smart bulbs, AC).

Network Layer : Transports data from perception/device layer to the next layer.

Platform Layer : Acts as the brain, handling device onboarding, management, messaging, monitoring, and security.

Application Layer : Processes collected data for business use and sends commands back to devices.

Front‑End Development for IoT

1. Writing IoT Code with Node.js

Node can call native C/C++ modules via the ABI and Native Addon mechanism. Example:

<code>const addon = require('./build/Release/greet.node');</code>

Typical DHT sensor usage in Node:

<code>var sensor = require("../build/Release/node_dht_sensor");
var promises = {
  initialize: sensor.initialize,
  setMaxRetries: sensor.setMaxRetries,
  readSync(type, pin) { return sensor.read(type, pin); },
  read(type, pin) {
    return new Promise((resolve, reject) => {
      sensor.read(type, pin, (err, temperature, humidity) => {
        if (err) reject(err);
        else resolve({ temperature, humidity });
      });
    });
  }
};
module.exports = { initialize: sensor.initialize, read: sensor.read, setMaxRetries: sensor.setMaxRetries, promises };
</code>

2. GPIO Programming

GPIO pins on Raspberry Pi are used for input/output. After wiring power, ground, and signal pins, install the

pigpio

library:

<code>yarn add pigpio</code>

Example of PWM control:

<code>const Gpio = require('pigpio').Gpio;
const led = new Gpio(17, { mode: Gpio.OUTPUT });
let dutyCycle = 0;
setInterval(() => {
  led.pwmWrite(dutyCycle);
  dutyCycle += 5;
  if (dutyCycle > 255) dutyCycle = 0;
}, 20);
</code>

3. Embedded V8 Engines

Projects like JerryScript provide a trimmed V8 engine for low‑memory devices (≈160 KB), enabling JavaScript on microcontrollers, though performance and compilation complexity remain challenges.

4. Transpiling Node.js to Other Platforms

Libraries such as Johnny‑Five offer cross‑platform hardware control and robot frameworks, suitable for beginners.

Common IoT Hardware

Sensors

Temperature sensor – converts ambient temperature to an electrical signal.

Humidity sensor – converts ambient humidity to an electrical signal.

Human presence sensor – detects whether a person is present.

Light sensor – converts light intensity to an electrical signal.

Microcontroller Units (MCU)

MCUs integrate CPU, memory, USB, LCD drivers, etc., into a single chip, forming a compact computer.

Communication Modules

Bluetooth – short‑range wireless communication.

Wi‑Fi – network connectivity via standard Wi‑Fi routers.

GPRS – cellular connectivity using SIM cards.

Summary

Sensors act as the eyes and skin of an IoT system, MCUs serve as the brain processing data, and communication modules act as the mouth and hands transmitting information.

Node.jsIoTRaspberry PiSensorsTemperatureHumidityUltrasonic
Goodme Frontend Team
Written by

Goodme Frontend Team

Regularly sharing the team's insights and expertise in the frontend field

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.