-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (43 loc) · 1.24 KB
/
app.js
File metadata and controls
57 lines (43 loc) · 1.24 KB
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
import mqtt from 'mqtt';
import rpi433 from 'rpi-433';
console.log('Starting...');
const rfSniffer = rpi433.sniffer({
pin: 1,
debounceDelay: 100
});
const rfEmitter = rpi433.emitter({
pin: 0,
pulseLength: 500
});
// Connection to MQTT
console.log('Connecting to MQTT');
const mqttClient = mqtt.connect('mqtt://localhost');
mqttClient.on('connect', () => {
console.log('Connected to MQTT');
// Subscribe to topic 433
mqttClient.subscribe('433');
});
// When receiving a code from RF
rfSniffer.on('data', (data) => {
console.log(`Receive code ${data.code} from RF`);
// Send the code to MQTT
mqttClient.publish('433', 'receive:' + data.code.toString());
});
// Receive a message from MQTT
mqttClient.on('message', (topic, message) => {
message = message.toString();
console.log(`Receive message on topic ${topic}: ${message}`);
if (topic !== '433' || !/^send:/.test(message)) {
return false;
}
const code = message.replace(/^send:/, '');
console.log(`Receive code from MQTT: ${code}`);
// Send the message to RF
rfEmitter.sendCode(code, (err, message) => {
if (err) {
console.error('Error when sending code to RF');
return false;
}
console.log('Code has been succesfully sent to RF');
});
});