Let's Build a Temperature Sensor with a Raspberry Pi - Part 1

Raspberry Pi Temperature Sensor - Part 1

This is a two part project. In part one, we connected a raspberry pi to a temperature & humidity sensor so that we could read the current temperature. In Part 2, we setup the pi to emit the temperature and humidity data using BLE. We also made an iPhone app that can read that data.

YouTube Video

How To Build

Required Hardware

Steps

Install raspbian onto the raspberry pi.

Wire up the pi to the sensor.

You can use the diagram and instructions on this page https://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor.

  • Plug the left pin (red pin) into 5v.
  • Plug the right pin (black pin) into ground.
  • Plug the inner left pin (green pin) into a gpio pin, I chose 4.

Raspberry pi pins

Plug in the pi, and connect to it using ssh.

I used the following link to do this before I plugged in the pi: Prepare SD card for Wifi on Headless Pi.

Here’s the official documentation on how to connect to a pi using ssh: https://www.raspberrypi.org/documentation/remote-access/ssh/

Install node on the pi.

You can use nvm Node Version Manager to do this.

You might need to run source ~/.bashrc before nvm works.

Make sure node is working on the pi.

npm install -g cowsay
cowsay hi

Do the setup for node-dht-sensor

Before you can use the node-dht-sensor module, you have to do some setup. You can follow the instructions on npm, but here’s what I did. On the pi, run the following code:

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.56.tar.gz
tar zxvf bcm2835-1.56.tar.gz
cd bcm2835-1.56
./configure
make
sudo make check
sudo make install

Make the app and write the code.

  • Create a new node project, so just mkdir sensor or something.
  • Inside that directory, npm install node-dht-sensor, might take a while.
  • I’ve included my code below, you can just copy and paste it:
const sensor = require('node-dht-sensor');

const sensorNumber = 22;
const pinNumber = 4;
sensor.read(sensorNumber, pinNumber, (err, temperature, humidity) => {
  if (err) {
    console.log("AHHHHHHHH error", err);
    return;
  }

  console.log('temp: ' + temperature.toFixed(1) + '°C, ' + 'humidity: ' + humidity.toFixed(1) +  '%');
});

Run the app.

node app.js

🤗