본문 바로가기

개발자/Raspberry Pi

Nodejs 를 사용하여 mcp3208 ADC 컨버터 사용하여 데이터 읽어오기

반응형




Nodejs 환경에서 MCP3208 ADC 컨버터를 사용하여 ADC 데이터를 읽어오는 방법을 설명한다.


일단 nodejs 설치한다. 


Node 의 최신 버전을 설치한다. Node 6.x.

  • $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

The previous command updates our Debian apt package repository to include the NodeSource packages.

설치화면은 아래와 같다.


pi@raspberrypi:~ $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -


## Installing the NodeSource Node.js v6.x repo...



## Populating apt-get cache...


+ apt-get update

Hit http://mirrordirector.raspbian.org jessie InRelease

Hit http://archive.raspberrypi.org jessie InRelease

Hit https://deb.nodesource.com jessie InRelease

Hit http://mirrordirector.raspbian.org jessie/main armhf Packages

Hit http://archive.raspberrypi.org jessie/main armhf Packages

Hit https://deb.nodesource.com jessie/main Sources

Hit http://archive.raspberrypi.org jessie/ui armhf Packages

Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages

Hit https://deb.nodesource.com jessie/main armhf Packages

Get:1 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages

Get:2 https://deb.nodesource.com jessie/main Translation-en [162 B]

Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages

Get:3 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:4 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:5 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:6 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:7 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:8 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:9 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Ign https://deb.nodesource.com jessie/main Translation-en_US

Get:10 https://deb.nodesource.com jessie/main Translation-en [162 B]

Ign https://deb.nodesource.com jessie/main Translation-en

Ign http://archive.raspberrypi.org jessie/main Translation-en_US

Ign http://archive.raspberrypi.org jessie/main Translation-en

Ign http://archive.raspberrypi.org jessie/ui Translation-en_US

Ign http://archive.raspberrypi.org jessie/ui Translation-en

Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en

Ign http://mirrordirector.raspbian.org jessie/main Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/main Translation-en

Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en

Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en

Reading package lists... Done


## Confirming "jessie" is supported...


+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_6.x/dists/jessie/Release'


## Adding the NodeSource signing key to your keyring...


+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -

OK


## Creating apt sources list file for the NodeSource Node.js v6.x repo...


+ echo 'deb https://deb.nodesource.com/node_6.x jessie main' > /etc/apt/sources.list.d/nodesource.list

+ echo 'deb-src https://deb.nodesource.com/node_6.x jessie main' >> /etc/apt/sources.list.d/nodesource.list


## Running `apt-get update` for you...


+ apt-get update

Hit http://archive.raspberrypi.org jessie InRelease

Hit http://mirrordirector.raspbian.org jessie InRelease

Hit http://archive.raspberrypi.org jessie/main armhf Packages

Hit http://mirrordirector.raspbian.org jessie/main armhf Packages

Get:1 https://deb.nodesource.com jessie InRelease [3,914 B]

Hit http://archive.raspberrypi.org jessie/ui armhf Packages

Hit http://mirrordirector.raspbian.org jessie/contrib armhf Packages

Get:2 https://deb.nodesource.com jessie/main Sources [762 B]

Hit http://mirrordirector.raspbian.org jessie/non-free armhf Packages

Hit http://mirrordirector.raspbian.org jessie/rpi armhf Packages

Get:3 https://deb.nodesource.com jessie/main armhf Packages [967 B]

Get:4 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:5 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:6 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:7 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:8 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:9 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:10 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Get:11 https://deb.nodesource.com jessie/main Translation-en [162 B]

Get:12 https://deb.nodesource.com jessie/main Translation-en_US [162 B]

Ign https://deb.nodesource.com jessie/main Translation-en_US

Get:13 https://deb.nodesource.com jessie/main Translation-en [162 B]

Ign https://deb.nodesource.com jessie/main Translation-en

Ign http://archive.raspberrypi.org jessie/main Translation-en_US

Ign http://archive.raspberrypi.org jessie/main Translation-en

Ign http://archive.raspberrypi.org jessie/ui Translation-en_US

Ign http://archive.raspberrypi.org jessie/ui Translation-en

Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/contrib Translation-en

Ign http://mirrordirector.raspbian.org jessie/main Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/main Translation-en

Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/non-free Translation-en

Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en_US

Ign http://mirrordirector.raspbian.org jessie/rpi Translation-en

Fetched 5,643 B in 16s (348 B/s)

Reading package lists... Done


## Run `apt-get install nodejs` (as root) to install Node.js v6.x and npm


pi@raspberrypi:~ $


마지막줄에 아래 명령을 실행하면 Node.js v6.x and npm을 동시에 설치한다고 나온다. 



pi@raspberrypi:~ $ sudo apt install -y nodejs

Reading package lists... Done

Building dependency tree

Reading state information... Done

You might want to run 'apt-get -f install' to correct these:

The following packages have unmet dependencies:

 mysql-server : Depends: mysql-server-5.5 but it is not going to be installed

E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

pi@raspberrypi:~ $


My Sql 버전이 맞지 않아 설치가 안된다는 말인거 같은데, 라즈베리 파이에는 이미 설치가 되어 있으므로 버전 확인만 하고 넘어간다.


pi@raspberrypi:~ $ node -v

v4.5.0


pi@raspberrypi:~/node_modules $ npm -v
2.15.9


npm init 명령어로 package.json 파일을 생성하는 과정을 거쳐야 한다. 이러한 과정을 설명한 다음 링크를 참고한다.


라즈베리 파이에서 node js 로 온습도 센서 dht22에서 데이터 읽어오기  http://fishpoint.tistory.com/1899



npm을 사용하여 ADC 컨버터 패키지를 설치한다.


pi@raspberrypi:~/nodejs-test $ npm install mcp-adc --save


> spi@0.2.0 install /home/pi/nodejs-test/node_modules/spi

> node-gyp rebuild


make: Entering directory '/home/pi/nodejs-test/node_modules/spi/build'

  CXX(target) Release/obj.target/_spi/src/spi_binding.o

../src/spi_binding.cc: In static member function ‘static void Spi::New(const v8::FunctionCallbackInfo<v8::Value>&)’:

../src/spi_binding.cc:105:49: warning: ‘v8::Local<v8::Object> v8::Function::NewInstance() const’ is deprecated (declared at /home/pi/.node-gyp/6.3.1/include/node/v8.h:3243): Use maybe version [-Wdeprecated-declarations]

     args.GetReturnValue().Set(cons->NewInstance());

                                                 ^

../src/spi_binding.cc: In member function ‘void Spi::full_duplex_transfer(v8::Isolate*, const v8::FunctionCallbackInfo<v8::Value>&, char*, char*, size_t, uint32_t, uint16_t, uint8_t)’:

../src/spi_binding.cc:199:3: warning: missing initializer for member ‘spi_ioc_transfer::cs_change’ [-Wmissing-field-initializers]

   };

   ^

../src/spi_binding.cc:199:3: warning: missing initializer for member ‘spi_ioc_transfer::tx_nbits’ [-Wmissing-field-initializers]

../src/spi_binding.cc:199:3: warning: missing initializer for member ‘spi_ioc_transfer::rx_nbits’ [-Wmissing-field-initializers]

../src/spi_binding.cc:199:3: warning: missing initializer for member ‘spi_ioc_transfer::pad’ [-Wmissing-field-initializers]

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

  COPY Release/_spi.node

make: Leaving directory '/home/pi/nodejs-test/node_modules/spi/build'

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

└─┬ mcp-adc@0.1.0

  └─┬ spi@0.2.0

    └── bindings@1.2.1


npm WARN nodejs-test@1.0.0 No repository field.

pi@raspberrypi:~/nodejs-test $



아래는 MCP3208 ADC 컨버터의 핀맵과 스팩, 사용된 회로도를 나타내었다.



MCP3208 Pin Map




MCP3208 Pin Description



라즈베리 파이와 ADC 간의 연결도



adc0-sample.js 파일을 작성한다.

프로그램 소스는 아래와 같다.


pi@raspberrypi:~/nodejs-test $ nano adc0-sample.js


var McpAdc = require('mcp-adc');

var adc = new McpAdc.Mcp3208();

var channel = 0;

adc.readRawValue(channel, function(value) {
        console.log("Raw value:\t" + value);
});

adc.readVoltage(channel, function(voltage) {
        console.log("Voltage:\t" + voltage);
});

adc.readNormalizedValue(channel, function(normValue) {
        console.log("Percents:\t" + (normValue * 100));
});

아래와 같이 실행하게 되면 결과를 볼 수 있다.


pi@raspberrypi:~/nodejs-test $ sudo node adc0-sample

{ MODE: { MODE_0: 0, MODE_1: 1, MODE_2: 2, MODE_3: 3 },

  CS: { none: 64, high: 4, low: 0 },

  ORDER: { msb: false, lsb: true },

  Spi: [Function] }

{ _spi: _spi {}, device: '/dev/spidev0.0' }

Raw value:      1935

Voltage:        1.5625641025641026

Percents:       47.27716727716727

pi@raspberrypi:~/nodejs-test $









반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

귀사가 성공하기까지의 긴 고난의 시간을 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩