1
2
3 import RPi.GPIO as GPIO
4 import time
5
6 VERBOSE = False
7 P_POWER = 11
8 P_RESET = 12
9
13
15 debug("Resetting modem...")
16 GPIO.setwarnings(False)
17 GPIO.setmode(GPIO.BOARD)
18 GPIO.setup(P_RESET, GPIO.OUT)
19 GPIO.output(P_RESET, GPIO.LOW)
20 time.sleep(0.5)
21 GPIO.output(P_RESET, GPIO.HIGH)
22 time.sleep(0.5)
23 GPIO.output(P_RESET, GPIO.LOW)
24 time.sleep(3)
25
27 debug("Toggling power...")
28 GPIO.setmode(GPIO.BOARD)
29 GPIO.setup(P_POWER, GPIO.OUT)
30 GPIO.output(P_POWER, GPIO.LOW)
31 time.sleep(0.5)
32 GPIO.output(P_POWER, GPIO.HIGH)
33 time.sleep(3)
34 GPIO.output(P_POWER, GPIO.LOW)
35
37
38 cmd = 'ATZ\r'
39 debug("Cmd: " + cmd)
40 ser.write(cmd)
41 time.sleep(2)
42 reply = ser.read(ser.inWaiting())
43 time.sleep(8)
44 debug("Reply: " + reply)
45 return ("OK" in reply)
46
48
49 cmd = 'AT+CSTT="' + apn + '"\r'
50 debug("Cmd: " + cmd)
51 ser.write(cmd)
52 time.sleep(3)
53
54
55 cmd = "AT+CIICR\r"
56 debug("Cmd: " + cmd)
57 ser.write(cmd)
58 time.sleep(5)
59
60
61 cmd = "AT+CIFSR\r"
62 debug("Cmd: " + cmd)
63 ser.write(cmd)
64 time.sleep(3)
65
66
67 reply = ser.read(ser.inWaiting())
68 debug("connectGSM() retured:\n" + reply)
69 return reply
70
72 cmd = 'AT+CIPSTART="TCP","' + host + '","' + str(port) + '"\r'
73 ser.write(cmd)
74 time.sleep(5)
75 reply = ser.read(ser.inWaiting())
76 debug("connctTCP() retured:\n" + reply)
77 return reply
78
80 ser.write("AT+CIPSEND\r")
81 time.sleep(2)
82 request = "GET " + request + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n"
83 ser.write(request + chr(26))
84 time.sleep(2)
85
87 ser.write("AT+CIPCLOSE=1\r")
88 reply = ser.read(ser.inWaiting())
89 debug("closeTCP() retured:\n" + reply)
90 if showResponse:
91 print "Server reponse:\n" + reply[(reply.index("SEND OK") + 9):]
92 time.sleep(2)
93
95 cmd = "AT+CIPSTATUS\n"
96 ser.write(cmd)
97 time.sleep(1)
98 reply = ser.read(ser.inWaiting())
99 return reply
100