zabsalahid / serialport-gsm

SerialPort-GSM is a simplified plugin for communicating with gsm modems. (Primarily for sms) (Focused in PDU mode)
MIT License
90 stars 47 forks source link

Need help to handle situation when gsm modem is turned off. #2

Closed 7aman closed 5 years ago

7aman commented 5 years ago

Hi.
Thanks for sharing your module. It is very helpful.
I set a RPi 3 B to monitoring some sensors. I use your module with a sim800L to provide a 24/7 SMS device to alert abnormal events. So my code must run 24/7.
This is a basic example that runs perfectly.

/* server.js */
let serialportgsm = require('serialport-gsm')
let modem = serialportgsm.Modem()
let options = {
    baudRate: 115200,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false,
    xon: false,
    rtscts: false,
    xoff: false,
    xany: false,
    buffersize: 0,
    autoDeleteOnReceive: true,
    enableConcatenation: true
}

modem.open('/dev/ttyS0', options, (data)=>{console.log(data)});
modem.on('open', () => {
    modem.initializeModem(msg => console.log('initialize msg:', msg));
    modem.setModemMode(msg => console.log('set pdu msg:', msg), 'PDU');
});
modem.on('close', msg => console.log('on close msg:' , msg));

modem.on('error',  msg => console.log('on error msg:' , msg));

But when sim800L is turned off (it might happen sometimes), Rpi CPU usage and temperature will raise.
This is screenshot of s-tui on RPi.

screenshot from 2019-02-08 12-30-42

Console logs:

pi@monitor:~/gsm-v2 $ node server.js 
open msg:  null
initialize msg:  { status: 'success',
  request: 'modemInitialized',
  data: 'Modem Successfully Initialized' }
set pdu msg:  { status: 'success', request: 'modemMode', data: 'PDU_Mode' }

When sim800L is turned on back again, code will run perfectly again and CPU usage will drop to normal.
My question:
How can I handle this turned off situations?

7aman commented 5 years ago

It's worth mentioning that only a turned off modem will cause this CPU usage. Disconnecting Rx and Tx have no effects on CPU. It's probably an issue of serialport itself and not your module. This happens to data received by ttyS0 when I turn off modem. It goes on and on.

pi@monitor:~/gsm-v2 $ python3 -m serial.tools.miniterm /dev/ttyS0 115200
--- Miniterm on /dev/ttyS0  115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
at
OK
␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀
zabsalahid commented 5 years ago

Hello there @7aman! Thanks for trying out serialport-gsm!

Since the close event is fired, it means the serialport's close method was fired successfully. I too think there is probably an issue with serialport. I'll look into it and notify you if ever I happen to stumble with some workaround.

Nice thing your working out there with RPi 3 B and sim800L!

7aman commented 5 years ago

Thanks. I appreciate your consideration, and look forward to hearing from you.

I'm not sure if close event is fired.
Text below, shows console's logs of my code (server.js I shared above). When I was executing node server.js Sim800L was running. After printing these messages I turned it off for 30 seconds. Then I turned it back on.
Since my simple console.log callbacks on close and error events printed out nothing, it seems close and error events were not fired.
Also during these power-off and power-on actions, my code never exited and It kept running and functioning as nothing unusual happened.

pi@monitor:~/gsm-v2 $ node server.js 
open msg:  null
initialize msg:  { status: 'success',
  request: 'modemInitialized',
  data: 'Modem Successfully Initialized' }
set pdu msg:  { status: 'success', request: 'modemMode', data: 'PDU_Mode' }
zabsalahid commented 5 years ago

@7aman any device disconnected while open or calling the close method should fire the close event. So turning off the Sim800L should fire close event.

May I ask how you toggle power of your Sim800L?

7aman commented 5 years ago

@zabsalahid I do it by disconnecting VCC and GND.

zabsalahid commented 5 years ago

Hi @7aman !

I have updated the module to 2.1.3. Can you please update your module. And see if it is still the same.

By disconnecting VCC and GND, you mean the Sim800L will have no power?

Can you also test if manually calling modem.close would still have the same CPU usage and temp.

7aman commented 5 years ago

I tested v2.1.5. CPU usage is still going up.
I found a hardware solution but later I will try to fix this issue in my code.

Hardware Solution: When sim800L is turned off, RPi3 is still listening and translating receiving bits. I have not any equipment (e.g. an oscilloscope) right now to check receiving bit. But if you open a serial console like I shared its results before, you will see some weird characters appear on serial console that shows RPi3 thinks it is receiving data.
After "OK" response, I turned off SIM800L.

pi@monitor:~/gsm-v2 $ python3 -m serial.tools.miniterm /dev/ttyS0 115200
--- Miniterm on /dev/ttyS0  115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
at
OK
␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀

After powering off, "␀" character will appear and it will fill rows and rows of terminal very fast. It is interesting that if you disconnect Rx from RPi3, terminal will stop showing "␀" character.
I temporarily fixed this issue by adding a middle hardware, a Bi-Directional Logic Level Converter. Its main application is to convert logic level between different devices such as Arduino that uses 5V and RPi3 which uses 3.3V. In this case because both SIM800L and RPi3 are using 3.3V it is not required. But by using this middle-man and applying 3.3V to both LV and HV sides, it will stop RPi3 to receive such wired bits at its serial port.

I will try later to remove logic converter and fix this issue in the code and let you know the result.
Thanks for your time.

7aman commented 5 years ago

By disconnecting VCC and GND, you mean the Sim800L will have no power?

Yes.
Story behind: I launched my little project over night to see if it is stable in long term. I launched a s-tui in separate window and went to sleep. In the morning I found CPU usage was very high and temperature was up to 70 C degree. And SIM800L was not responding to abnormal sensors. I found power supply of SIM800L was disconnected at some point. I repeated disconnecting SIM800L power a few more times and found this issue.

Can you also test if manually calling modem.close would still have the same CPU usage and temp.

No. If I call modem.close() manually in my code, it fires modem.on('close') callback function and then closes and exits the process. So it will not use CPU anymore.
It is not helpful for me. I need an .on('disconnect') event and a modem.reconnect() to serve my purpose 24/7. I will try to implement such functionality later.

zabsalahid commented 5 years ago

@7aman , I was using your sample code above and ran it. It works fine. image On close was called when I pulled out the wavecom fastrack.

Also looked into @serialport/stream.js, disconnecting a port will cause it to close the connection.

And this one using python. image Immediately thrown an error upon disconnecting the device from the usb.

7aman commented 5 years ago

So it is an issue with RPi3 serial port (gpio 14, 15) or SIM800L.
Because you have not my set of hardware I will try to fix it and I will share the result with you.
Thanks for your time.

zabsalahid commented 5 years ago

No problem @7aman.

iamqinglong commented 5 years ago

Hello @7aman , How did you manage to implement .on('disconnect') and also modem.reconnect() ? I also need those functionality. Because when I try to plug out my usb modem from my pc it calls the function modem.close() and exits the process.