-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (39 loc) · 1.44 KB
/
index.js
File metadata and controls
46 lines (39 loc) · 1.44 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
const axios = require('axios');
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('/dev/ttyS0', { baudRate: 9600 });
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));
const serverUrl = 'https://externalserver.com/api/sms-commands';
setInterval(() => {
axios.get(serverUrl)
.then(response => {
if (response.data && response.data.number && response.data.message) {
sendSMS(response.data.number, response.data.message);
}
})
.catch(error => console.error('Failed to fetch commands:', error));
}, 300000); // 5 minutes
function sendSMS(number, message) {
port.write('AT+CMGF=1\r', (err) => {
if (err) {
return console.error('Error on write: ', err.message);
}
console.log('Text mode enabled for SMS');
port.write(`AT+CMGS="${number}"\r`, (err) => {
if (err) {
return console.error('Error on write: ', err.message);
}
console.log('Recipient number set');
port.write(`${message}\x1A`, (err) => {
if (err) {
return console.error('Error on write: ', err.message);
}
console.log('Message sent');
});
});
});
}
// Handle incoming data from SIM800L
parser.on('data', data => {
console.log('Read data:', data);
});