Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.8 kB
3
Indexable
Never
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('COMx', { baudRate: 9600 }); // Replace 'COMx' with the actual COM port assigned to your phone
const parser = port.pipe(new Readline({ delimiter: '\r\n' }));

// Function to send SMS
function sendSMS(number, message) {
    port.write('AT\r\n', (err) => {
        if (err) {
            return console.log('Error writing: ', err.message);
        }
        console.log('AT command sent');
    });

    setTimeout(() => {
        port.write('AT+CMGF=1\r\n', (err) => {
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Set SMS mode to text');
        });
    }, 1000);

    setTimeout(() => {
        port.write(`AT+CMGS="${number}"\r\n`, (err) => {
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Recipient number set');
        });
    }, 2000);

    setTimeout(() => {
        port.write(`${message}\x1A`, (err) => { // \x1A is ASCII code for CTRL+Z
            if (err) {
                return console.log('Error writing: ', err.message);
            }
            console.log('Message sent');
        });
    }, 3000);
}

// Open errors will be emitted as an error event
port.on('error', function(err) {
  console.log('Error: ', err.message);
});

// Switches the port into "flowing mode"
port.on('data', function (data) {
  console.log('Data:', data);
});

// Wait for the port to open, then send SMS
port.on('open', function() {
    console.log('Port opened');
    setTimeout(() => {
        sendSMS('+1234567890', 'Hello from Node.js!');
    }, 5000);
});
Leave a Comment