Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS : Send SMS via GSM device connected at serial port

Tags:

node.js

sms

I need to send SMS via a GSM device connected to the PC at serial COM1 port.

I am able to receive SMS but when I try to send an SMS, it's never delivered. When I try connecting the GSM device via hyper terminals and give the command to send SMS it works fine. Below is my code:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("COM1", {
    baudrate: 9600,  dataBits: 8,  parity: 'none',  stopBits: 1, flowControl: false, xon : false, rtscts:false, xoff:false, xany:false, buffersize:0
});

serialPort.on("open", function () {
    console.log('Serial communication open');
    serialPort.write("AT+CMGF=1");
    serialPort.write('\r');
    delay(10);
    serialPort.write("AT+CNMI=2,2,0,0,0");
    serialPort.write('\r');
    delay(10);
    serialPort.on('data', function(data) {
        gsm_message_sending(serialPort, "S", "1111111111"); //valid number only
    });
});

function gsm_message_sending(serial,message,phone_no)
{
    serial.write("AT+CMGF=1");
    delay(100); // a simple for loop
    serial.write("AT+CNMI=2,2,0,0,0"); 
    serial.write('\r');
    delay(200);
    serial.write("AT+CMGS=\"+91");
    serial.write(phone_no);
    serial.write('"')
    serial.write('\r');
    serial.write(message); 
    //Now I have to send two time consecutively Ctrl+Z or char - 26
    //I am assuming that maybe cause of below code SMS are not being send
    // Method 1 :
    // serial.write(String.fromCharCode(26)) 
    //serial.write(String.fromCharCode(26)) 
    //Method 2
    //serial.write(Buffer([0x1A]));
    //serial.write(Buffer([0x1A]));
    //serial.write('^z');
    //serial.write('^z');
}

I am not sure what wrong I am doing or why the SMS is not going out?

like image 567
Programmer Avatar asked Apr 19 '14 15:04

Programmer


1 Answers

Your script

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyUSB0", {
     baudrate: 9600,  dataBits: 8,  parity: 'none',  stopBits: 1, flowControl: false, xon : false, rtscts:false, xoff:false, xany:false, buffersize:0
});

serialPort.on("open", function () {
    console.log('Serial communication open');
    serialPort.write("AT^SYSCFG=13,1,3FFFFFFF,2,4");
    serialPort.write('\r');
    serialPort.on('data', function(data) {
        console.log("Received data: " + data);
    });
    gsm_message_sending(serialPort, "test2", "<you phone number>");
});

function gsm_message_sending(serial, message, phone_no) {
    serial.write("AT+CMGF=1");
    serial.write('\r');
    serial.write("AT+CMGS=\"");
    serial.write(phone_no);
    serial.write('"')
    serial.write('\r');
    serial.write(message); 
    serial.write(Buffer([0x1A]));
    serial.write('^z');
}

works perfectly for me with E3131 modem. As you can see, I changed a bit the script so that it only emits one sms and prints the messages:

Received data: AT^SYSCFG=13,1,3FFFFFFF,2,4
Received data:
OK

Received data: AT+CMGF=1
Received data:
OK

Received data: AT+CMGS="
Received data: 0671358943
Received data: "
Received data:
>
Received data: test2
Received data:
Received data: ^z
Received data:
+CMGS: 28

OK
like image 191
Mermoz Avatar answered Oct 21 '22 21:10

Mermoz