-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (44 loc) · 1.54 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import StreamrClient from "streamr-client";
const StreamrClient = require("streamr-client");
const SDS011Wrapper = require("sds011-wrapper");
const fs = require("fs");
require("dotenv").config();
const client = new StreamrClient({
auth: {
privateKey: process.env.ETHEREUM_PRIVATE_KEY,
},
});
const sensor = new SDS011Wrapper("/dev/ttyUSB0"); // Replace with the correct port for the SDS011 sensor on your Raspberry Pi
sensor.setReportingMode("query"); // Set the SDS011 sensor to query mode
sensor.setWorkingPeriod(1); // Set the working period to 1 minute (optional)
const publishAirQualityData = async (pm25, pm10) => {
const data = {
pm25: pm25,
pm10: pm10,
};
console.log("Air Quality Data:", data);
await client.publish("STREAM-ID", data); // Replace STREAM-ID with your Stream ID
};
sensor.on("measure", (pm25, pm10) => {
publishAirQualityData(pm25, pm10);
});
import StreamrClient from "streamr-client";
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const streamr = new StreamrClient({
auth: {
privateKey: PRIVATE_KEY,
},
});
const stream = await streamr.getOrCreateStream({
id: process.env.STREAMR_PRIVATE_KEY / sensor / firehose,
});
await stream.grantPermissions({
user: BrokerNodeAddress,
permissions: [StreamPermission.PUBLISH, StreamPermission.SUBSCRIBE],
});
const mqttClient = mqtt.connect("mqtt://localhost:1883", {
username: "x",
password: process.env.MQTT_CLIENT_PASSWORD,
});
const StreamId = process.env.STREAM_ID / sensor / firehose;
await mqttClient.publish(StreamId, JSON.stringify({ data }));