Reading from the sensor
Now that everything's connected, let's take a reading from our sensor. The DHT11 sensor gives us a 40-bit reading over the time span of 2 seconds. Timing and reading each bit can be a complex task. Fortunately for us, there are libraries present to make our lives easier.
Node.js does not have the required support for the DHT11 sensor, and we need to install the bcm2835
library in order to interface with the sensor.
To install the library, run the following commands:
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.52.tar.gz tar -xvzf bcm2835-1.52.tar.gz cd bcm2835-1.52 ./configure make sudo make check sudo make install
Next, go to the ~/sensor-project/server
directory and create a new file called obtain-readings.js
.
Install the node-dht-sensor
library:
npm install --save node-dht-sensor
Populate the contents of the obtain-readings.js
file:
//Import the sensor library const sensor = require('node-dht-sensor') //The first argument is the sensor number. In...