Skip to content

Commit 9b9eef1

Browse files
Merge branch 'feature/refactor'
2 parents 9441677 + 9ddbd74 commit 9b9eef1

9 files changed

Lines changed: 536 additions & 405 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ typings/
5858
.env
5959

6060
package-lock.json
61-
assets
61+
stuff

README.md

Lines changed: 87 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# miflora
22

3-
Node.js package for the Xiaomi Mi Flora Plant Sensor built on top of [noble](https://github.com/noble/noble) and [ES6 promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises).
3+
Node.js package for the Xiaomi Mi Flora Plant Sensor built on top of [noble](https://github.com/noble/noble).
44

5-
![product image](http://img.site.huahuacaocao.net/production/production_05_01.png)
6-
7-
![npm](https://img.shields.io/npm/v/miflora.svg)
5+
[![npm](https://img.shields.io/npm/v/miflora.svg)](https://www.npmjs.com/package/miflora)
86
![language](https://img.shields.io/github/languages/top/ChrisScheffler/miflora.svg)
7+
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
98
![commit](https://img.shields.io/github/last-commit/ChrisScheffler/miflora.svg)
109
![firmware](https://img.shields.io/badge/firmware-3.1.8-brightgreen.svg)
11-
![licence](https://img.shields.io/npm/l/miflora.svg)
12-
13-
Have a look in the [Wiki](https://github.com/ChrisScheffler/miflora/wiki).
10+
[![licence](https://img.shields.io/npm/l/miflora.svg)](LICENSE)
1411

15-
**Code and readme are work in progress!**
12+
Have a look in the [Wiki](https://github.com/ChrisScheffler/miflora/wiki) for more information on the sensor.
1613

1714
---
1815

16+
## Prerequisites
17+
18+
Please see [the Prerequisites section for noble](https://github.com/noble/noble#prerequisites).
19+
1920
## Install
2021

2122
```sh
@@ -24,64 +25,92 @@ npm install miflora
2425

2526
## Usage
2627

28+
The library uses [async/await](https://javascript.info/async-await) code syntax instead of [Promises chaining](https://javascript.info/promise-chaining) in order to execute asynchronous code sequentially. Since all internal code is based on [Promises](https://javascript.info/promise-basics) you still can use the `method().then().catch()`-pattern regardless.
29+
30+
*All examples use async/await syntax.*
31+
32+
### Discover devices
33+
34+
```javascript
35+
const devices = await miflora.discover();
36+
console.log('devices discovered: ', devices.length);
37+
```
38+
39+
In this example we listen for 10000 (*default value*) milliseconds (10 seconds) and print out the number of detected devices.
40+
41+
### Discover devices (advanced)
42+
43+
```javascript
44+
const opts = {
45+
duration: 60000,
46+
addresses: ['c4:7c:8d:65:d6:1d', 'c4:7c:8d:65:d5:26', 'c4:7c:8d:65:e6:20']
47+
};
48+
const devices = await miflora.discover(opts);
49+
console.log('devices discovered: ', devices.length);
50+
```
51+
52+
This time we listen for 60000 milliseconds (60 seconds) **or** until all devices from given `opts.addresses` have been discovered and print out the number of detected devices.
53+
54+
### Query device information
55+
56+
All query methods implicitly initiate a connection if none exists. You can call `device.connect()` explicitly if you like nevertheless.
57+
58+
The library however **doesn't** perform a disconnect implicitly. You can call `device.disconnect()` when you have finished you queries or let you disconnect automatically from the device after 10 seconds.
59+
60+
#### Firmaware & Battery
61+
62+
```javascript
63+
const data = await device.queryFirmwareInfo();
64+
console.log(data);
65+
```
66+
67+
Example output:
68+
2769
```javascript
28-
const miflora = require('miflora');
29-
30-
miflora.discover().then(devices => {
31-
devices.forEach(device => {
32-
miflora.queryDevice(device).then(data => {
33-
console.log(JSON.stringify(data, null, 2));
34-
}).catch(err => {
35-
console.error('error while querying device', device, ':', err);
36-
});
37-
});
38-
}).catch(err => {
39-
console.error('well, something went wrong:', err);
40-
});
70+
{ address: 'c4:7c:8d:65:e6:20',
71+
type: 'MiFloraMonitor',
72+
firmwareInfo: { battery: 100, firmware: '3.1.8' } }
4173
```
4274

43-
### Example output
75+
#### Sensor values
4476

4577
```javascript
46-
{
47-
"address": "c4:7c:8d:65:e5:20",
48-
"rssi": -61,
49-
"battery": 100,
50-
"firmware": "3.1.8",
51-
"data": {
52-
"temperature": 22.1,
53-
"lux": 324,
54-
"moisture": 22,
55-
"fertility": 290
56-
}
57-
}
58-
{
59-
"address": "c4:7c:8d:65:d5:26",
60-
"rssi": -80,
61-
"battery": 98,
62-
"firmware": "3.1.8",
63-
"data": {
64-
"temperature": 20.5,
65-
"lux": 164,
66-
"moisture": 31,
67-
"fertility": 300
68-
}
69-
}
70-
{
71-
"address": "c4:7c:8d:65:d5:1d",
72-
"rssi": -77,
73-
"battery": 99,
74-
"firmware": "3.1.8",
75-
"data": {
76-
"temperature": 22,
77-
"lux": 311,
78-
"moisture": 26,
79-
"fertility": 450
80-
}
81-
}
78+
const data = await device.querySensorValues();
79+
console.log(data);
80+
```
81+
82+
Example output:
83+
84+
```javascript
85+
{ address: 'c4:7c:8d:65:e6:20',
86+
type: 'MiFloraMonitor',
87+
sensorValues: { temperature: 21.1, lux: 104, moisture: 36, fertility: 1049 } }
88+
```
89+
90+
#### Serial Number
91+
92+
```javascript
93+
const data = await device.querySerial();
94+
console.log(data);
95+
```
96+
97+
#### Combined query
98+
99+
```javascript
100+
const opts = {
101+
firmwareInfo: true,
102+
sensorValues: true,
103+
serial: false;
104+
};
105+
const data = await device.query(opts);
106+
console.log(data);
82107
```
83108

84109
## References
85110

86111
- https://github.com/demirhanaydin/node-mi-flora
87112
- https://wiki.hackerspace.pl/projects:xiaomi-flora
113+
114+
## Licence
115+
116+
[MIT](LICENSE)

_config.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)