-
Notifications
You must be signed in to change notification settings - Fork 11
/
mms.py
330 lines (274 loc) · 9.5 KB
/
mms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# -*- coding: utf-8 -*-
#
# mms.py
# Script to send a MMS
#
import nadhat_pwr_on
import nadhat_pwr_off
import wiringpi
import serial
import time, sys
import datetime
import os, signal
import argparse
operator_sms_dict = {'ORANGE': '+33689004000', 'SFR': '+33609001390', 'FREE': '+33695000695', 'BOUYGUES': '+33660660001', 'ORANGE_CARAIBES': '+590690350012', 'LAPOSTE': '+33609001390'}
operator_mms_url_dict = {'ORANGE': 'http://mms.orange.fr', 'SFR': 'http://mms.sfr.fr', 'FREE': 'http://mms1', 'BOUYGUES': 'http://mms.bouyguestelecom.fr/mms/wapenc', 'ORANGE_CARAIBES': '', 'LAPOSTE': 'http://mmsdebitel'}
operator_mms_proxy_dict = {'ORANGE': '192.168.10.200', 'SFR': '10.151.0.1', 'FREE': '212.27.40.225', 'BOUYGUES': '62.201.129.226', 'ORANGE_CARAIBES': '', 'LAPOSTE': '10.143.156.3'}
operator_mms_port_dict = {'ORANGE': '8080', 'SFR': '8080', 'FREE': '80', 'BOUYGUES': '8080', 'ORANGE_CARAIBES': '', 'LAPOSTE': '8080'}
operator_mms_apn_dict = {'ORANGE': 'orange.acte', 'SFR': 'mmssfr', 'FREE': 'mmsfree', 'BOUYGUES': 'mmsbouygtel.com', 'ORANGE_CARAIBES': '', 'LAPOSTE': 'sl2sfr'}
# Contenu MMS
PIC = 1
TITLE = 0
TEXT = 0
#MAILFROM = "ihoratius@garatronic.fr"
##############Begin process command line ###########
parser = argparse.ArgumentParser(description='Configure and send your mms. command line could be: python mms.py "+336XXXXXXXX" "pic.jpg" -o ORANGE -cp 0000 -p "/dev/ttyAMA0"')
parser.add_argument('RN', type=str, help='Receiver number. ex: "+336XXXXXXXX"')
parser.add_argument('pic', type=str, help='picture path. ex: "pic.jpg"')
parser.add_argument('--title', '-t',
help='title file text. ex "title.txt"',
type=str)
parser.add_argument('--text', '-txt',
help='text file text. ex "text.txt"',
type=str)
parser.add_argument('--operator', '-o',
help='Service provider',
choices=['ORANGE', 'SFR', 'FREE', 'BOUYGUES', 'ORANGE_CARAIBES', 'LAPOSTE'],
default='ORANGE')
parser.add_argument('--cpin', '-cp',
help='Pin code',
type=str,
default='0000')
parser.add_argument('--port', '-p',
help='Serial port',
type=str,
default='/dev/ttyAMA0')
title = ""
text = ""
args = parser.parse_args()
operator_sms = operator_sms_dict[args.operator]
receiver_nb = args.RN
serial_port = args.port
code_pin = args.cpin
pic = args.pic
title = args.title
text = args.text
operator_mms_url = operator_mms_url_dict[args.operator]
operator_mms_proxy = operator_mms_proxy_dict[args.operator]
operator_mms_port = operator_mms_port_dict[args.operator]
operator_mms_apn = operator_mms_apn_dict[args.operator]
if title:
TITLE = 1
if text:
TEXT = 1
print "center_sms="+args.operator+", receiver_nb="+receiver_nb+", serial_port="+serial_port+", code_pin="+code_pin+", pic="+pic+", operator_mms_url="+operator_mms_url+", operator_mms_proxy="+operator_mms_proxy+", operator_mms_port="+operator_mms_port+", operator_mms_apn="+operator_mms_apn
#print center_sms
##############End process command line ###########
# Wait for the nadhat answer
def wait_Answer(code):
time.sleep(2)
rep = ser.read(ser.inWaiting()) # Check if the nadhat answers
if rep != "":
if code in rep:
print "Answers : "+code
else :
print code+" not received : No communication with the nadhat board"
sys.exit(0)
else :
print "No response from the board"
sys.exit(1)
# Serial port init
ser = serial.Serial(
port = serial_port,
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS
)
# Read file size
def taille_fichier(nom_fichier):
taille = os.stat(nom_fichier)
return taille.st_size
# Start the SIM800C
nadhat_pwr_on.pwr_on()
time.sleep(5)
# Check the communication with the nadhat board
ser.write("AT\r") # Send AT command
wait_Answer("OK")
# Send the pin code
ser.write("AT+CPIN="+code_pin+"\r")
print "Code PIN sent... wait 10 secondes."
time.sleep(5)
print "Wait for 5 more seconds..."
time.sleep(5)
print "Check the slow blinking LED rate..."
wait_Answer("OK")
# Set MMS mode
ser.write('AT+CMMSINIT\r')
print "AT+CMMSINIT\r"
wait_Answer("OK")
# The URL to be sending the MMS
ser.write('AT+CMMSCURL="'+operator_mms_url+'"\r')
print 'AT+CMMSCURL="'+operator_mms_url+'"\r'
wait_Answer("OK")
# Define the bearer ID
ser.write('AT+CMMSCID=1\r')
print "AT+CMMSCID=1\r"
wait_Answer("OK")
# MMS Proxy
ser.write('AT+CMMSPROTO="'+operator_mms_proxy+'",'+operator_mms_port+'\r')
print 'AT+CMMSPROTO="'+operator_mms_proxy+'",'+operator_mms_port+'\r'
wait_Answer("OK")
# MMS Send configration
ser.write('AT+CMMSSENDCFG=1,1,0,0,1,4,2,0\r')
print "AT+CMMSSENDCFG=1,1,0,0,1,4,2,0\r"
wait_Answer("OK")
# MMS Send configration -> save
ser.write('AT+CMMSSCONT\r')
print "AT+CMMSSCONT\r"
wait_Answer("OK")
# Define the bearer settings
ser.write('AT+SAPBR=3,1,"Contype","GPRS"\r')
print 'AT+SAPBR=3,1,"Contype","GPRS"\r'
wait_Answer("OK")
# Define bearer context
ser.write('AT+SAPBR=3,1,"APN","'+operator_mms_apn+'"\r')
print 'AT+SAPBR=3,1,"APN","'+operator_mms_apn+'"\r'
wait_Answer("OK")
# Active bearer context
ser.write('AT+SAPBR=1,1\r')
print "AT+SAPBR=1,1\r"
wait_Answer("OK")
# Display the context for checking
ser.write('AT+SAPBR=2,1\r')
print "AT+SAPBR=2,1\r"
time.sleep(2)
rep = ser.read(ser.inWaiting()) # Regarde si la carte a répondu
if rep != "":
print "Response : "+ rep + "\r"
else :
print "No response for the bearer settings AT+SAPBR=2,1\r"
sys.exit(0)
# On vérifie également que la carte a répondu OK
if not ("OK" in rep):
print "Communication problem with the NadHAT board: OK not received"
sys.exit(0)
# Start the mms editing mode
ser.write('AT+CMMSEDIT=1\r')
print "AT+CMMSEDIT=1\r"
wait_Answer("OK")
# ============= ENVOI DU MMS : IMAGE ================
if (PIC):
# Temps d'envoi du fichier dans le MMS
timeout = 60000
# Trouver la taille de l'image
taille = taille_fichier(pic)
print "Taille de l'image à envoyer : " + str(taille) + "\r"
# Envoyer le fichier
ser.write('AT+CMMSDOWN="PIC",' + str(taille) + ',' + str(timeout) + '\r')
print 'AT+CMMSDOWN="PIC",' + str(taille) +',' +str(timeout) + '\r'
wait_Answer("CONNECT")
# Envoi du fichier sur le port série
with open(pic, "rb") as f:
octet = f.read(1)
while octet != "":
ser.write(octet)
# Afficher l'octet sauf si c'est une commande (< 0x10)
# puis l'envoyer sur le port série
if (octet < 0x10) :
print "-- ",
else :
print hex(ord(octet)) + " ",
octet = f.read(1)
# Attendre le retour du SIM800C
wait_Answer("OK")
# ============= ENVOI DU MMS : TITRE ================
if (TITLE):
# Temps d'envoi du fichier dans le MMS
timeout = 60000
# Trouver la taille de l'image
taille = taille_fichier("title.txt")
print "Size of the title file to send : " + str(taille) + "\r"
# Envoyer le fichier
ser.write('AT+CMMSDOWN="TITLE",' + str(taille) + ',' + str(timeout) + '\r')
print 'AT+CMMSDOWN="TITLE",' + str(taille) +',' +str(timeout) + '\r'
wait_Answer("CONNECT")
# Envoi du fichier sur le port série
with open("title.txt", "rb") as f:
octet = f.read(1)
while octet != "":
ser.write(octet)
# Afficher l'octet sauf si c'est une commande (< 0x10)
# puis l'envoyer sur le port série
if (octet < 0x10) :
print "-- ",
else :
print hex(ord(octet)) + " ",
octet = f.read(1)
# Attendre le retour du SIM800C
wait_Answer("OK")
# ============= ENVOI DU MMS : TEXTE ================
if (TEXT):
# Temps d'envoi du fichier dans le MMS
timeout = 60000
# Trouver la taille du texte
taille = taille_fichier("text.txt")
print "Size of the text file to send : " + str(taille) + "\r"
# Envoyer le fichier
ser.write('AT+CMMSDOWN="TEXT",' + str(taille) + ',' + str(timeout) + '\r')
print 'AT+CMMSDOWN="TEXT",' + str(taille) +',' +str(timeout) + '\r'
wait_Answer("CONNECT")
# Envoi du fichier sur le port série
with open("text.txt", "rb") as f:
octet = f.read(1)
while octet != "":
ser.write(octet)
# Afficher l'octet sauf si c'est une commande (< 0x10)
# puis l'envoyer sur le port série
if (octet < 0x10) :
print "-- ",
else :
print hex(ord(octet)) + " ",
octet = f.read(1)
# Attendre le retour du SIM800C
wait_Answer("OK")
# Recipient number
ser.write('AT+CMMSRECP="' + receiver_nb + '"\r')
print 'AT+CMMSRECP="' + receiver_nb + '"\r'
wait_Answer("OK")
# Sender mail adress
#ser.write('AT+CMMSBCC=' + MAILFROM + '\r')
#print 'AT+CMMSBCC=' + MAILFROM + '\r'
#wait_Answer("OK")
# Checking the MMS before sending
ser.write('AT+CMMSVIEW\r')
print "AT+CMMSVIEW\r"
time.sleep(2)
rep = ser.read(ser.inWaiting()) # Regarde si la carte a répondu
if rep != "":
print "Response : "+ rep + "\r"
else :
print "No response for the MMS checking\r"
sys.exit(0)
# On vérifie également que la carte a répondu OK
if not ("OK" in rep):
print "Communication problem with the NadHAT board: OK not received"
sys.exit(0)
# Send the MMS
ser.write("AT+CMMSSEND\r")
print "AT+CMMSSEND\r"
# Wait for the reponded code (can take a long time)
time.sleep(1)
rep = ser.read(ser.inWaiting()) # vide le buffer
print "Wait for the SIM800C feedback(can take a long time)\r"
rep = ""
while rep == "":
rep = ser.read(ser.inWaiting()) # Regarde si la carte a répondu
time.sleep(1)
print "Response : "+ rep + "\r"
# Exit the MMS mode and remove the MMS from the buffer
ser.write('AT+CMMSEDIT=0\r')
print "AT+CMMSEDIT=0\r"
wait_Answer("OK")
# Stop the SIM800C
nadhat_pwr_off.pwr_off()