Module SIM800Modem
[hide private]
[frames] | no frames]

Source Code for Module SIM800Modem

  1  #SIM800Modem.py 
  2   
  3  import RPi.GPIO as GPIO 
  4  import time 
  5   
  6  VERBOSE = False 
  7  P_POWER = 11 # Power pin 
  8  P_RESET = 12 # Reset pin 
  9   
10 -def debug(text):
11 if VERBOSE: 12 print "Debug:---", text
13
14 -def resetModem():
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
26 -def togglePower():
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
36 -def isReady(ser):
37 # Resetting to defaults 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) # Wait until connected to net 44 debug("Reply: " + reply) 45 return ("OK" in reply)
46
47 -def connectGSM(ser, apn):
48 # Login to APN, no userid/password needed 49 cmd = 'AT+CSTT="' + apn + '"\r' 50 debug("Cmd: " + cmd) 51 ser.write(cmd) 52 time.sleep(3) 53 54 # Bringing up network 55 cmd = "AT+CIICR\r" 56 debug("Cmd: " + cmd) 57 ser.write(cmd) 58 time.sleep(5) 59 60 # Getting IP address 61 cmd = "AT+CIFSR\r" 62 debug("Cmd: " + cmd) 63 ser.write(cmd) 64 time.sleep(3) 65 66 # Returning all messages from modem 67 reply = ser.read(ser.inWaiting()) 68 debug("connectGSM() retured:\n" + reply) 69 return reply
70
71 -def connectTCP(ser, host, port):
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
79 -def sendHTTPRequest(ser, host, request):
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)) # data<^Z> 84 time.sleep(2)
85
86 -def closeTCP(ser, showResponse = False):
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
94 -def getIPStatus(ser):
95 cmd = "AT+CIPSTATUS\n" 96 ser.write(cmd) 97 time.sleep(1) 98 reply = ser.read(ser.inWaiting()) 99 return reply
100