-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
44 lines (37 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import dhtSensor from 'node-dht-sensor';
import dotenv from 'dotenv';
import { DeviceClient, DeviceConfig } from '@wiotp/sdk';
import { createLogger, format, transports } from 'winston';
dotenv.config();
const deviceConfig = DeviceConfig.parseEnvVars();
const deviceClient = new DeviceClient(deviceConfig);
const logger = createLogger({
format: format.combine(
format.timestamp(),
format.prettyPrint()
),
transports: [
new transports.Console()
]
});
const SENSOR_ID = 22;
const GPIO = 4;
const QOS = 2;
const INTERVAL = 60; // seconds
deviceClient.connect();
deviceClient.on('connect', () => {
logger.info('Connected to the IBM IoT platform');
setInterval(() => {
dhtSensor.promises.read(SENSOR_ID, GPIO).then(
res => {
const data = {
temperature: res.temperature.toFixed(1),
humidity: res.humidity.toFixed(1)
};
deviceClient.publishEvent('data', 'json', data, QOS);
logger.info(data);
},
err => logger.error('Error reading sensor data', err));
}, INTERVAL * 1000);
});
deviceClient.on('error', err => logger.error('Error connecting to the IBM IoT platform', err));