Advertisement
HintSystem

Untitled

Dec 11th, 2023 (edited)
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import machine
  2. import utime
  3.  
  4. sim800l = machine.UART(1, baudrate=115200, tx=8, rx=9, timeout=10000)
  5. import re
  6.  
  7. resCodes = ['OK', 'ERROR']
  8. def resComplete(line):
  9.     if any(element in line for element in resCodes):
  10.         return True
  11.     return False
  12.  
  13. def getCommand(string):
  14.     resPattern = re.compile(r'+(\w+)')
  15.     cmd = resPattern.search(line).match
  16.     return cmd
  17.  
  18. def awaitResF(line, cmd):
  19.     if cmd in line:
  20.         return True
  21.     return False
  22.  
  23. def send_at_command(command, awaitRes=False):
  24.     sim800l.write(command + b'\r\n')
  25.     sim800l.flush() #wait until command is sent
  26.  
  27.     cmd = getCommand(command)
  28.  
  29.     response = b''
  30.     newLine = ''
  31.     while not resComplete(newLine) and (not awaitRes or awaitResF(newLine, cmd)):
  32.         newLine = sim800l.readline()
  33.         if newLine == None:
  34.             #The machine took too long to respond
  35.             #You can set the timeout time in the sim800 constructor - machine.uart(1, baudrate=..., tx=..., rx=..., timeout=...) in ms
  36.             print("RESPONSE TIMED OUT")
  37.             return
  38.         response += newLine
  39.  
  40.     print('cmd :', command, 'res :', response.decode( 'utf-8').strip(), '\n----')
  41.  
  42. send_at_command(b'AT+CMEE=2')
  43. send_at_command(b'AT+CFUN=1')
  44. send_at_command(b'AT+CPIN=7575', True)
  45. send_at_command(b'AT+CREG?')
  46. send_at_command(b'AT+CGATT?')
  47. send_at_command(b'AT+CGATT=1')
  48. send_at_command(b'AT+CSTT=?')
  49. send_at_command(b'AT+CSTT=internet.tele2.lv')
  50. send_at_command(b'AT+CIICR')
  51. send_at_command(b'AT+CSQ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement