Last active
September 25, 2021 12:18
-
-
Save suzukiken/60b1a340788647852b5b125b4f5d191d to your computer and use it in GitHub Desktop.
Sending / Receiving SMS using Raspberry Pi, Soracom Air SIM and ABIT AK-020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
'''Sending / Receiving SMS using Raspberry Pi, Soracom Air SIM and ABIT AK-020 | |
''' | |
import time | |
import serial | |
import re | |
class Sms: | |
def __init__(self): | |
self.pattern = re.compile('\+CMGL: [0-9]+,"REC UNREAD"') | |
self.serial = serial.Serial( | |
'/dev/ttyUSB0', | |
460800, | |
timeout = 5, | |
xonxoff = False, | |
rtscts = False, | |
dsrdtr = False, | |
bytesize = serial.EIGHTBITS, | |
parity = serial.PARITY_NONE, | |
stopbits = serial.STOPBITS_ONE | |
) | |
self.serial.write('ATZ\r') | |
self.check_response_isok() | |
self.serial.write('AT+CFUN=1\r') | |
self.check_response_isok() | |
self.serial.write('AT+CGDCONT=1,"IP","soracom.io"\r') | |
self.check_response_isok() | |
self.serial.write('AT+CMGF=1\r') | |
self.check_response_isok() | |
def __del__(self): | |
self.serial.close() | |
def wait_response(self): | |
time.sleep(1) | |
while self.serial.inWaiting() == 0: | |
time.sleep(0.5) | |
def check_response_isok(self): | |
self.wait_response() | |
r = self.serial.read(self.serial.inWaiting()).split('\r\n') | |
if len(r) < 2 or r[-2] != 'OK': | |
raise Exception(r) | |
def check_response_isprompt(self): | |
self.wait_response() | |
r = self.serial.read(self.serial.inWaiting()).split('\r\n') | |
if len(r) < 1 or r[-1] != '> ': | |
raise Exception(r) | |
def dispose_response(self): | |
self.wait_response() | |
self.serial.read(self.serial.inWaiting()) | |
def send_message(self, message, to): | |
self.serial.write('AT+CMGS="%s"\r' % to) | |
self.check_response_isprompt() | |
self.serial.write(message + chr(26)) # CTRL-Z | |
self.dispose_response() | |
self.check_response_isok() | |
def receive_message(self): | |
self.serial.write('AT+CMGL="REC UNREAD"\r\n') | |
self.wait_response() | |
r = self.serial.read(self.serial.inWaiting()).split('\r\n') | |
if len(r) < 2 or r[-2] != 'OK': | |
raise Exception(r) | |
messages = [] | |
is_message = False | |
for line in r: | |
if is_message: | |
messages.append(line) | |
if line and self.pattern.match(line): | |
is_message = True | |
else: | |
is_message = False | |
return messages | |
if __name__ == "__main__": | |
sms = Sms() | |
sms.send_message('Hello world!', '00000000000') # replace 00000000000 by the phone number you want to send SMS to. | |
print sms.receive_message() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考になります。ありがとうございます。
ファイルを添付することを試したいのですが、そのようなことは可能でしょうか?