52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
import serial
|
|
from gsmmodem import GsmModem
|
|
from gsmmodem.modem import SentSms
|
|
|
|
PORT = "/dev/tty.usbmodem11101"
|
|
BAUDRATE = 115200
|
|
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
|
|
|
|
|
|
def has_sim() -> bool:
|
|
# check pin
|
|
cmd_check_pin = "AT+cpin?\r"
|
|
msg = send_command(cmd_check_pin)
|
|
if b'OK' in msg:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
def send_command(cmd: str) -> bytes:
|
|
print("send command {}".format(cmd))
|
|
ser.write(cmd.encode())
|
|
msg = ser.read(100)
|
|
print(msg)
|
|
return msg
|
|
|
|
|
|
def send_sms(sms_destination: str, msg: str):
|
|
print('Initializing modem...')
|
|
modem = GsmModem(PORT, BAUDRATE)
|
|
modem.connect('0000')
|
|
# modem.waitForNetworkCoverage(10)
|
|
print('Sending SMS to: {0}'.format(sms_destination))
|
|
|
|
response = modem.sendSms(sms_destination, msg, True)
|
|
if type(response) == SentSms:
|
|
print('SMS Delivered.')
|
|
else:
|
|
print('SMS Could not be sent')
|
|
|
|
modem.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# enable verbose logs
|
|
cmd = "AT+CMEE=2\r"
|
|
send_command(cmd)
|
|
if has_sim():
|
|
send_sms("+33649614591","Modem pool test")
|
|
else:
|
|
print("no cart sim")
|