# node-mysensor

> MySensor serial gateway communication interface for Nodejs

Latest version **2.1.13** (published 2019-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-mysensor
pnpm add node-mysensor
yarn add node-mysensor
bun add node-mysensor
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.13 |
| Published | 2019-09-23 |
| First published | 2014-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 28.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | aokihu |
| Maintainers | aokihu |
| Keywords | mysensor, iot, gateway |

## Links

- npm: https://www.npmjs.com/package/node-mysensor
- Repository: https://github.com/aokihu/node-mysensor
- Homepage: https://github.com/aokihu/node-mysensor#readme
- Issues: https://github.com/aokihu/node-mysensor/issues
- npm.io page: https://npm.io/package/node-mysensor

## Dependencies (3)

- [serialport](https://npm.io/package/serialport.md) ^7.1.5
- [@types/serialport](https://npm.io/package/@types/serialport.md) ^7.0.4
- [@serialport/parser-readline](https://npm.io/package/@serialport/parser-readline.md) ^2.0.2

## Recent versions

- 2.1.13 (latest) — 2019-09-23
- 2.1.12 — 2019-09-23
- 2.1.11 — 2019-09-23
- 2.1.10 — 2019-09-23
- 2.1.9 — 2019-09-23
- 2.1.8 — 2019-09-22
- 2.1.7 — 2019-09-22
- 2.1.6 — 2019-09-22
- 2.1.5 — 2019-09-22
- 2.1.4 — 2019-09-22
- 2.1.3 — 2019-09-22
- 2.1.2 — 2019-09-22
- 2.1.1 — 2019-09-21
- 2.1.0 — 2019-09-21
- 1.0.1 — 2014-12-13
- … 1 more at https://npm.io/package/node-mysensor/versions

## README

# node-mysensor

## Install

You can install `node-mysensor` with `npm` or `yarn`

```javascript
npm install -s node-mysensor
```

OR

```javascript
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`


```javascript
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](https://www.mysensors.org/download/serial_api_20#message-structure)

## 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

|   key    | type |     value     |
|---------|-------|---------------|
| nodeID  | Number| The unique id of the node that sends or should receive the message (address)|
| childID | Number|Each node can have several sensors attached. This is the child-sensor-id that uniquely identifies one attached sensor|
| command | Number|Type of command sent |
| ack     | Number|The 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 |
| type    | Number|Depending on command, the type field has different meaning. |
| payload | Number OR String| The payload holds the message coming in from sensors or instruction going out to actuators. |

---
_Source: https://npm.io/package/node-mysensor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
