In the previous article we read the temperature and humidity from DHT11 sensor in python
Temperature and humidity reading on Beaglebone in python
We perform now the same operation in javascript. We download the dht module for node.js. The module is available on the website
Log on BeagleBone in ssh as debian (temppwd as password) and run
1 2 3 4 |
sudo npm install -g mocha sudo npm install -g chai sudo npm install -g beaglebone-dht@0.0.3 sudo npm install -g beaglebone-dht --save |
to install the module globally.
For the sensor reading create a file test.js with the following code inside
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
var dht = require('/usr/local/lib/node_modules/beaglebone-dht'), sensor = dht.sensor('DHT11'); var i=0; do { read=dht.read('P8_11'); try { valore=read['humidity']; if(valore<=100.00&&valore>=0.0) { console.log(read); break; } } catch(err) { console.log("Error in sensor reading!"); } sleep(2000); } while (i<15) function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ break; } } } |
Reading is performed running
1 |
sudo node test.js |
The result in our case is the following
The same code can be run from Cloud9 IDE. Access the IDE on BeagleBone with the link http: //beaglebone.local:3000 and create a file named dht.js with the following code inside
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
var b = require('bonescript'); var dht = require('/usr/local/lib/node_modules/beaglebone-dht'), sensor = dht.sensor('DHT11'); var i=0; do { read=dht.read('P8_11'); try { valore=read['humidity']; if(valore<=100.00&&valore>=0.0) { console.log(read); break; } } catch(err) { console.log("Error in sensor reading!"); } sleep(2000); } while (i<15) function sleep(milliseconds) { var start = new Date().getTime(); for (var i = 0; i < 1e7; i++) { if ((new Date().getTime() - start) > milliseconds){ break; } } } |
The script execution gives the following result
Here is the link to download the two sample files