0.0.8 • Published 10 years ago

aot-rpi v0.0.8

Weekly downloads
4
License
Apache2
Repository
github
Last release
10 years ago

AoT-node.js-Raspberry

AoT와 연동하기 위한 오픈 Raspberry PI 개발용 Node.js 기반 라이브러리 및 예제 소스입니다. MQTT 통신을 이용하여 센서값을 Publish 하고 명령어(command)를 subscribe 합니다.

This is the AoT Python library and sample source for developing open hardware. The content is published and command is subscribed through MQTT protocol.

Getting Started

본 스크립트는 node.js를 기반으로 작성되었습니다. 따라서 사전에 node.js가 설치되어있어야합니다.

This API is based on node.js. Therefore you need to install the node.js in advance.

Installing

파일을 다운 받은 후, 다음 명령어를 실행하여 모듈을 설치합니다.

After you download the packages, type the command below on your command line to download dependency automatically.

npm install 

Running the tests

Example 디렉토리에 있는 샘플 코드를 다음 명령어를 입력하여 실행합니다.

You may run the sample code in example directory by typing below command.

node INSTALLED_PATH/examples/example.js

정상적으로 실행 될 경우, 아래와 같은 로그를 출력합니다.

The log written in below will appear if the code works properly.

[log] on_connect:: connection succeed.
[log] aot.js:publishDeviceContent:: Successfully published.
[log] aot.js:publishNodeContent:: Successfully published.

Simple Tutorial

Device ID and Key

Device ID와 Key 값을 설정합니다. Device ID와 Key 값은 all of things 홈페이지에서 발급 받습니다. Device ID와 Key 값은 config.js파일에서 값을 수정합니다

The device ID and Key SHOULD be set. You can issue the device ID and Keys on all of things portal. The config.jsfile needs to be updated to set the Device ID and Device password.

module.exports = {
    serverIP: "mqtt://api.allofthings.com",
    port: "1883",
    deviceID: "YOUR Device ID",
    devicePassword: "YOUR Device Password"
}

Data publishing

데이터는 디바이스에서 직접 수집한 정보를 전송하는 기능과 센서에서 수집한 정보를 전송하는 기능이 있습니다.

Device has two operations that publishing the data from itself and the data from its sensor.

Publishing device content

publishDeviceContent(content)

Parameter

  • content: 자료형에 의존성이 없습니다. 즉, 어느 값이라도 전송 가능합니다.

  • content: Any value. It supports any data type.

Example

// Need to import module.
var aotMqtt = require('../lib/aot.js');

// Codes will run with 2 seconds interval.
setInterval(function() {
    // The connection will be automatically progressed.
    // Publish the integer value 55.
    aotMqtt.publishDeviceContent(55)
}, 2*1000);

Publishing sensor content

publishNodeContent(nodeID, content)

Parameter

  • content: 자료형에 의존성이 없습니다. 즉, 어느 값이라도 전송 가능합니다. 단, 홈페이지에서 그래프 형태로 값을 보고싶다면, 홈페이지에서 제공하고 있는 센서종류를 참고하여 그에 맞는 자료형을 사용해야합니다.
  • nodeID: 센서 노드의 ID 값을 명시해야합니다.
  • ===================================
  • content: Any value. But if you want to see the graph in allofthings.com the value should be assigned by type of sensor.
  • nodeID: Write sensor ID that defined yourself or allofthings.com defined.

Example

// Need to import module.
var aotMqtt = require('../lib/aot.js');

// Codes will run with 2 seconds interval.
setInterval(function() {
    // The connection will be automatically progressed.
    // Publish the integer value 55 that originated from nodeID 1.
    aotMqtt.publishNodeContent(1, 55)
}, 2*1000);

Data subscribe

사용자 정의 Callback 함수를 이용하여 데이터를 수집합니다.

In AoT Node.js library, received messages are controlled by customized callback function.

How to control the Command

// Define the callback function.
client.on('message', function(topic, message){
    // Get the command message.
    var command = aotMqtt.getCommand(message)

    if (command == null) return

    if (command.message_type == "device"){
        /* Do something depends on your service... */
        console.log("[Test] Subscribed command: " + command.request_value)
    }else{
        /* Command for node */
        /* Do something depends on your service... */
        console.log("[Test] Command for node"+ command.node_id)
        console.log("[Test] Subscribed command: " + command.request_value)
    }
})

Parameter

  • topic: subscribed 된 topic을 의미합니다
  • message: 입력받은 명렁어를 의미합니다. Message는 디바이스 명령어와 node 명령어로 나뉩니다.

  • topic: Incoming topic.

  • message: Incoming command message. The message divided into two commands, device and node.

Return

  • 자료형에 의존성이 없습니다. 즉, 어느 값이라도 결과로 반환 가능합니다.

  • Any value.

Example with Sornic Sensor

본 예제는 초음파 센서를 Raspberry-pi에 연결하고 데이터를 AoT에 전송합니다.

mmm-usonic 모듈은 node.js에서 HC-SR04 초음파 센서에서 데이터를 읽어 올 수 있도록하는 모듈입니다.

Configuration

config 폴더 안에 있는 us-sonic-config.js에 값을 설정해야합니다.

  • echoPin: GPIO echo pin
  • triggerPin: GPIO triggerPin
  • timeout: Set timeout (default: 750)
  • delay (optional): set delay (default: 60)
  • rate (optional): set rate (default: 5)

Publish the data

var aotMqtt = require('../lib/aot.js');
var ultraSonic = require ('./usonic.js');
aotMqtt.publishNodeContent(nodeID,ultraSonic.getDistance())

노드 ID에 상응하는 초음파 센서 데이터가 AoT 홈페이지에 기록됩니다.

The ultra sonic distance value in cm will publish to AoT server with nodeID.

Execution

sudo node YOUR_INSTALL_PATH/examples/example-ultra-sonic.js

Authors

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago