1.0.0 • Published 3 years ago

node-mysensor-lib v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

node-mysensor

Install

You can install node-mysensor with npm or yarn

npm install -s node-mysensor

OR

yarn add node-mysensor

Usage

There is ONLY class NodeMysensor. It will be constructed by 2 params port and baudRate

port is the serial communication port, like /dev/ttyUSB0 on Linux OR COM1 on Windows

baudRate is the speed of serial, it usualy is 115200

const Mysensor = require('./index.js');

const sensor = new Mysensor("/dev/tty.usbserial-A800H5SE", 115200);

sensor.on('message', console.table); 

/**
┌─────────┬─────────────────────────────┐
│ (index) │           Values            │
├─────────┼─────────────────────────────┤
│ nodeID  │              0              │
│ childID │             255             │
│ command │              3              │
│   ack   │              0              │
│  type   │             14              │
│ payload │ 'Gateway startup complete.' │
└─────────┴─────────────────────────────┘
 */

sensor.on('presentation', (message) => {
  console.log("Device presentation", message);
}) // it will show when device send presentation information

sensor.on('internal', (message) => {
  console.log("Device presentation", message);
}) // it will show internal command

sensor.on('set', (message) => {...})
sensor.on('req', (message) => {...})
sensor.on('stream', (message) = {...})

sensor.send(15,1,2,3,0,1) // Send message to node

Static variable

  • MySensor.DEBUG The switch to show debug message

Methods

  • instance.send(nodeID, childID, command, type, ack, payload)

    Send raw data to node

    • nodeID Target node's id
    • chidID Target node's child id
    • command Send command type
    • type Date type
    • ack Force node replay message
    • payload Real set data

    You can read detail document from offical site at here

Events

  • message
  • presentation
  • internal
  • req
  • set
  • stream

All events supply only one param for callback function, it is parsed message, the struct of message you can SEE BELOW Message Section

Message

The message is parsed by MySensor protocol 2.0

keytypevalue
nodeIDNumberThe unique id of the node that sends or should receive the message (address)
childIDNumberEach node can have several sensors attached. This is the child-sensor-id that uniquely identifies one attached sensor
commandNumberType of command sent
ackNumberThe ack parameter has the following meaning: Outgoing: 0 = unacknowledged message, 1 = request ack from destination node, Incoming: 0 = normal message, 1 = this is an ack message
typeNumberDepending on command, the type field has different meaning.
payloadNumber OR StringThe payload holds the message coming in from sensors or instruction going out to actuators.