Posted in raspberrypi, discord, dht11, nodejs

In my first blog post, I created a Discord Bot that runs on Raspberry Pi Zero and turned a LED on and off. Today I want to continue using this combo but instead of a LED, I will use a DHT11 sensor that reads the humidity and temperature. This information will be posted in a channel from Discord and will be read from a room from my house (1 to 1 relationship here). 

The coolest thing about using Discord is that you can use any device or invite anyone to use the functionality that is made without making complicated setups.

raspberry_pi_temperature_humidity.gif

Hardware specification and setup for DHT11

The temperature and humidity sensor is not very fast, the data must be read once at ~5s. It needs a resistor with a value between 5.1K-10K that will be placed between VCC (Pin 1) and Pin 2.

dht11.png

The table below shows the connections that I made between Raspberry Pi and DHT11. 

DHT pinSignalRaspberry Pi physical pin
13.3V ~ 5.5V 4 (VCC)
2Data 7 (BCM 4) and 4 (VCC) with resistor
3not used 
4Ground 6 (GND)

Below I created the schema that I will use to read the values from DHT11 sensor using fritzing. The resistor that I have used has 10K (ohm). I chose to connect the resistor directly between pin 7 and 4.

schema.png

Reading the sensor and publishing to discord 

To read the DHT11 sensor from nodejs I will use the node-dht-sensor library. 

The file package.json for this project is pretty simple, contains the library mentioned above and discord.js library in order to publish the values read from the sensor to a channel.

{
  "name": "home-manager-sensor",
  "version": "0.0.2",
  "description": "A bot that helps me to automize my home.",
  "main": "bot.js",
  "author": "TheHumbleCode",
  "dependencies": {
    "discord.js": "^11.3.2",
    "node-dht-sensor": "^0.0.34"
  }
}

Mentioning the Discord Bot, don't forget to create the auth.json file and add the token for the bot.

{
 "token": "PUT_YOUR_TOKEN_HERE"
}

To read the sensor every 5 seconds for this example, in a real case every hour will make more sense in order to avoid spamming. In Discord, I created a channel named living-room and use it to send the updates periodically. 

The code is very tight, but it can very easily be implemented commands to stop or to continue publishing the temperature and humidity.

The below code is written to a file named, bot.js

const auth = require('./auth.json');
const Discord = require('discord.js');
const sensor = require('node-dht-sensor');

const DHT = 11;
const SENSOR_PIN = 4;

notifyChannel = function(channel) {
    const sensorResult = sensor.read(DHT, SENSOR_PIN);
    const message = `The current temperature is ${sensorResult.temperature.toFixed(2)} °C and humidity ${sensorResult.humidity.toFixed(2)} %`;
    channel.send(message);
}    

// Initialize Discord Bot
const bot = new Discord.Client();
bot.on('ready', (evt) => {
    console.log('Connected');

    let livingRoomChannel = bot.channels.find('name', 'living-room');
    
    setInterval(() => {
        notifyChannel(livingRoomChannel);
    }, 5000)
});

bot.login(auth.token);

In order for node-dht-sensor library to work on the project, it needs the following prerequisites. I described the steps I used below.

1. Install Broadcom bcm2835 following the steps below (if the version will get obsolete, search for the latest on their website).

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

2. Install node-gyp to compile native nodejs modules (nodejs addons).

sudo npm install node-gyp -g

3. At the top of the project folder install the dependencies needed (discord.js and node-dht-sensor).

npm install

Now we are ready to start the Discord Bot

node bot.js

Conclusion

Although for this type of data (timestamp, value) makes more sense to be published in a service where it can be seen as a line chart.

The way I like to look at this is like having an audit. Now I need to remote control the AC from far.. far away in order to set up the perfect room temperature.

IMG_20180405_230114.jpg

Thank you for reading!

Comments

Victoria Martinez

This project gives me a better idea of how thermostats constructed. Thanks for sharing the article. <a href="http://www.cosmopolitanmechanical.com/heating-and-cooling-kitchener.html">Heating and Cooling Kitchener</a>

Victoria Martinez

Post a comment