본문 바로가기

개발자/Raspberry Pi

라즈베리 파이에서 node js 로 온습도 센서 dht22에서 데이터 읽어오기





nodejs 로 온습도 센서 dht22에서 데이터 읽어오기


일단 nodejs 가 설치되고, npm(node package manager) 가 설치된 상태에서 무작정

$ npm install node-dht-sensor 을 실행 시키면 아래와 같은 에러가 발생한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
pi@raspberrypi:~/node_modules $ npm install node-dht-sensor
/
> node-dht-sensor@0.0.12 preinstall /home/pi/node_modules/node-dht-sensor
> ./preinst
 
+--------------------------------------------------------------------------------+
| ERROR: Library BCM2835 not found!                                              |
| Make sure you follow instructions from http://www.airspayce.com/mikem/bcm2835/ |
| to install the BCM2835 library on your system first then install this addon.   |
+--------------------------------------------------------------------------------+
node-dht-sensor@0.0.12 node-dht-sensor
└── nan@2.4.0
pi@raspberrypi:~/node_modules $
 
cs


BCM2835 관련 라이브러리를 먼저 설치한다.


$curl -O http://www.airspayce.com/mikem/bcm2835/bcm2835-1.36.tar.gz

$tar zxvf bcm2835-1.36.tar.gz

$cd bcm2835-1.36

$./configure

$make

$sudo make check

$sudo make install


위 명령어를 수행하는 과정을 아래에 나타내었다.


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
pi@raspberrypi:~ $ curl -O http://www.airspayce.com/mikem/bcm2835/bcm2835-1.36.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  234k  100  234k    0     0   130k      0  0:00:01  0:00:01 --:--:--  130k
 
pi@raspberrypi:~ $ tar zxvf bcm2835-1.36.tar.gz
 
bcm2835-1.36/
bcm2835-1.36/config.sub
bcm2835-1.36/ChangeLog
bcm2835-1.36/doc/
bcm2835-1.36/doc/Makefile.am
bcm2835-1.36/doc/Makefile.in
bcm2835-1.36/doc/Doxyfile.in
bcm2835-1.36/aclocal.m4
bcm2835-1.36/README
bcm2835-1.36/src/
bcm2835-1.36/src/bcm2835.h
bcm2835-1.36/src/Makefile.am
bcm2835-1.36/src/bcm2835.c
bcm2835-1.36/src/test.c
bcm2835-1.36/src/Makefile.in
bcm2835-1.36/INSTALL
bcm2835-1.36/depcomp
bcm2835-1.36/config.h.in
bcm2835-1.36/config.guess
bcm2835-1.36/configure.in
bcm2835-1.36/NEWS
bcm2835-1.36/COPYING
bcm2835-1.36/Makefile.am
bcm2835-1.36/ltmain.sh
bcm2835-1.36/configure
bcm2835-1.36/missing
bcm2835-1.36/install-sh
bcm2835-1.36/Makefile.in
bcm2835-1.36/AUTHORS
bcm2835-1.36/examples/
bcm2835-1.36/examples/blink/
bcm2835-1.36/examples/blink/blink.c
bcm2835-1.36/examples/i2c/
bcm2835-1.36/examples/i2c/i2c.c
bcm2835-1.36/examples/input/
bcm2835-1.36/examples/input/input.c
bcm2835-1.36/examples/gpio/
bcm2835-1.36/examples/gpio/gpio.c
bcm2835-1.36/examples/spin/
bcm2835-1.36/examples/spin/spin.c
bcm2835-1.36/examples/event/
bcm2835-1.36/examples/event/event.c
bcm2835-1.36/examples/spi/
bcm2835-1.36/examples/spi/spi.c
bcm2835-1.36/examples/pwm/
bcm2835-1.36/examples/pwm/pwm.c
 
pi@raspberrypi:~ $ cd bcm2835-1.36
 
pi@raspberrypi:~/bcm2835-1.36 $ ./configure
 
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking dependency style of gcc... gcc3
checking for clock_gettime in -lrt... yes
checking for doxygen... no
configure: WARNING: Doxygen not found - continuing without Doxygen support
checking for ranlib... ranlib
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking dependency style of gcc... (cached) gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating doc/Makefile
config.status: creating config.h
config.status: executing depfiles commands
 
pi@raspberrypi:~/bcm2835-1.36 $ make
 
make  all-recursive
make[1]: Entering directory '/home/pi/bcm2835-1.36'
Making all in src
make[2]: Entering directory '/home/pi/bcm2835-1.36/src'
gcc -DHAVE_CONFIG_H -I. -I..     --O2 -MT bcm2835.o -MD -MP -MF .deps/bcm2835.Tpo --o bcm2835.o bcm2835.c
mv -f .deps/bcm2835.Tpo .deps/bcm2835.Po
rm -f libbcm2835.a
ar cru libbcm2835.a bcm2835.o
ranlib libbcm2835.a
make[2]: Leaving directory '/home/pi/bcm2835-1.36/src'
Making all in doc
make[2]: Entering directory '/home/pi/bcm2835-1.36/doc'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/pi/bcm2835-1.36/doc'
make[2]: Entering directory '/home/pi/bcm2835-1.36'
make[2]: Leaving directory '/home/pi/bcm2835-1.36'
make[1]: Leaving directory '/home/pi/bcm2835-1.36'
 
pi@raspberrypi:~/bcm2835-1.36 $ sudo make check
 
Making check in src
make[1]: Entering directory '/home/pi/bcm2835-1.36/src'
make  test
make[2]: Entering directory '/home/pi/bcm2835-1.36/src'
gcc -DHAVE_CONFIG_H -I. -I..     --O2 -MT test.o -MD -MP -MF .deps/test.Tpo --o test.o test.c
mv -f .deps/test.Tpo .deps/test.Po
gcc  --O2   -o test test.o ./libbcm2835.a -lrt
make[2]: Leaving directory '/home/pi/bcm2835-1.36/src'
make  check-TESTS
make[2]: Entering directory '/home/pi/bcm2835-1.36/src'
PASS: test
=============
1 test passed
=============
make[2]: Leaving directory '/home/pi/bcm2835-1.36/src'
make[1]: Leaving directory '/home/pi/bcm2835-1.36/src'
Making check in doc
make[1]: Entering directory '/home/pi/bcm2835-1.36/doc'
make[1]: Nothing to be done for 'check'.
make[1]: Leaving directory '/home/pi/bcm2835-1.36/doc'
make[1]: Entering directory '/home/pi/bcm2835-1.36'
make[1]: Leaving directory '/home/pi/bcm2835-1.36'
 
pi@raspberrypi:~/bcm2835-1.36 $ sudo make install
 
Making install in src
make[1]: Entering directory '/home/pi/bcm2835-1.36/src'
make[2]: Entering directory '/home/pi/bcm2835-1.36/src'
 /bin/mkdir -'/usr/local/lib'
 /usr/bin/install --644  libbcm2835.a '/usr/local/lib'
 ( cd '/usr/local/lib' && ranlib libbcm2835.a )
 /bin/mkdir -'/usr/local/include'
 /usr/bin/install --644 bcm2835.h '/usr/local/include'
make[2]: Leaving directory '/home/pi/bcm2835-1.36/src'
make[1]: Leaving directory '/home/pi/bcm2835-1.36/src'
Making install in doc
make[1]: Entering directory '/home/pi/bcm2835-1.36/doc'
make[2]: Entering directory '/home/pi/bcm2835-1.36/doc'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/home/pi/bcm2835-1.36/doc'
make[1]: Leaving directory '/home/pi/bcm2835-1.36/doc'
make[1]: Entering directory '/home/pi/bcm2835-1.36'
make[2]: Entering directory '/home/pi/bcm2835-1.36'
make[2]: Nothing to be done for 'install-exec-am'.
make[2]: Nothing to be done for 'install-data-am'.
make[2]: Leaving directory '/home/pi/bcm2835-1.36'
make[1]: Leaving directory '/home/pi/bcm2835-1.36'
pi@raspberrypi:~/bcm2835-1.36 $
 
cs


아래 소스코드를 입력하여 humitemp.js 파일을 생성한다.


$sudo nano humitemp.js


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var sensorLib = require('node-dht-sensor');
 
var sensor = {
  initialize: function() {
    return sensorLib.initialize(114);
  },
  read: function() {
    var readout = sensorLib.read();
    console.log('Temperature: '+readout.temperature.toFixed(2)+'C, humidity: '+readout.humidity.toFixed(2)+'%');
    setTimeout(function() {
      sensor.read();
    }, 1500);
  }
};
 
if (sensor.initialize()) {
  sensor.read();
else {
  console.warn('Failed to initialize sensor');
}
cs


온도 습도 관련 node 패키지를 설치하기 전에 반드시  명령어로 package.json파일을 만들어 준다.

작업 폴더는 nodejs-test 이다. 아래 순서대로 엔터를 입력하여 


pi@raspberrypi:~/nodejs-test $ npm init


This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.


See `npm help json` for definitive documentation on these fields

and exactly what they do.


Use `npm install <pkg> --save` afterwards to install a package and

save it as a dependency in the package.json file.


Press ^C at any time to quit.

name: (nodejs-test)

version: (1.0.0)

Invalid version: " "

version: (1.0.0) 1.0.0

description: server side javascript sleepcare test

entry point: (dht11.js)

test command:

git repository:

keywords:

author:

license: (ISC)

About to write to /home/pi/nodejs-test/package.json:


{

  "name": "nodejs-test",

  "version": "1.0.0",

  "description": "server side javascript sleepcare test",

  "main": "dht11.js",

  "dependencies": {

    "node-dht-sensor": "^0.0.17"

  },

  "devDependencies": {},

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"

  },

  "author": "",

  "license": "ISC"

}



Is this ok? (yes) yes

pi@raspberrypi:~/nodejs-test $



참고로 작업 폴더를 보여준다.


pi@raspberrypi:~/nodejs-test $ ls

bcm2835-1.36.tar.gz  dht11.js  helloworld.js  humitemp-2.js  humitemp-3.js  humitemp.js  node_modules


다음 온습도 센서 관련 node 패키지를 설치한다.


pi@raspberrypi:~/nodejs-test $ npm install node-dht-sensor --save


> node-dht-sensor@0.0.17 preinstall /home/pi/nodejs-test/node_modules/.staging/node-dht-sensor-446602ba

> ./check-lib.sh


Library bcm2835 found.


> node-dht-sensor@0.0.17 postinstall /home/pi/nodejs-test/node_modules/node-dht-sensor

> node-gyp rebuild


make: Entering directory '/home/pi/nodejs-test/node_modules/node-dht-sensor/build'

  CXX(target) Release/obj.target/node_dht_sensor/node-dht-sensor.o

  SOLINK_MODULE(target) Release/obj.target/node_dht_sensor.node

  COPY Release/node_dht_sensor.node

make: Leaving directory '/home/pi/nodejs-test/node_modules/node-dht-sensor/build'

nodejs-test@1.0.0 /home/pi/nodejs-test

└── node-dht-sensor@0.0.17


npm WARN nodejs-test@1.0.0 No repository field.



마지막으로 작성한 파일을 실행하면 잘된다.


pi@raspberrypi:~/nodejs-test $ sudo node humitemp.js

Temperature: 27.30C, humidity: 42.70%

Temperature: 27.30C, humidity: 43.60%

Temperature: 27.30C, humidity: 43.60%

Temperature: 27.30C, humidity: 43.50%