Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
7aman opened this issue Feb 8, 2019 · 12 comments
Closed

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

7aman opened this issue Feb 8, 2019 · 12 comments

Comments

@7aman
Copy link

7aman commented Feb 8, 2019

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
Copy link
Author

7aman commented Feb 8, 2019

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
Copy link
Owner

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
Copy link
Author

7aman commented Feb 9, 2019

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
Copy link
Owner

zabsalahid commented Feb 9, 2019

@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
Copy link
Author

7aman commented Feb 9, 2019

@zabsalahid I do it by disconnecting VCC and GND.

@zabsalahid
Copy link
Owner

zabsalahid commented Feb 9, 2019

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
Copy link
Author

7aman commented Feb 9, 2019

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
Copy link
Author

7aman commented Feb 9, 2019

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
Copy link
Owner

@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
Copy link
Author

7aman commented Feb 9, 2019

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
Copy link
Owner

No problem @7aman.

@iamqinglong
Copy link

iamqinglong commented Apr 21, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants