From ecfcb667afabdd27f86ea78ce78b74b8c367e9cf Mon Sep 17 00:00:00 2001 From: PAN Lei Date: Mon, 7 Feb 2022 10:06:08 +0100 Subject: [PATCH] can send sms --- .gitignore | 1 + main.py | 56 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/main.py b/main.py index 75660ab..6e98af5 100644 --- a/main.py +++ b/main.py @@ -1,33 +1,51 @@ import serial +from gsmmodem import GsmModem +from gsmmodem.modem import SentSms -serial_list=[] -ser = serial.Serial("COM74", 115200, timeout=1) +PORT = "/dev/tty.usbmodem11101" +BAUDRATE = 115200 +ser = serial.Serial(PORT, BAUDRATE, timeout=1) -def changeToSMSMode(): - cmd = "AT+CMGF=1\r" # text mode - sendCommand(cmd) - msg = ser.read(64) - print(msg) +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 sendCommand(cmd: str): +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 + # enable verbose logs cmd = "AT+CMEE=2\r" - sendCommand(cmd) - #check pin - cmd_check_pin = "AT+cpin?\r" - sendCommand(cmd_check_pin) - # cmd = "AT\r" - # sendCommand(cmd) - # msg = ser.read(64) - # print(msg) - # changeToSMSMode() - + send_command(cmd) + if has_sim(): + send_sms("+33649614591","Modem pool test") + else: + print("no cart sim")