1

This is my class for checking unread messages

 def CheckUnreadMsgs(ser)
   print "Check for new messages...\n"
   ser.write("AT\r")
   time.sleep(3)
   line =  ser.read(size = 64)
   print line
   sen.write('AT+CMGL="REC UNREAD"\r')
   time.sleep(3)
   response = ser.read(size =200)
   print response
   return response

This is the main program.

print "Opening communications serial...\n"
ser = serial.Serial('/dev/ttyUSB1',115200,timeout=1)
ser.open()
#check continous loop
print "Starting main loop\n"

while True:
  message = CheckUnreadMsgs(ser)
  a = message.find('\n') + 63
  b = len(message)
  msg = message[a:b]
  print msg
  if (msg == "on")
    print "Power ON Commanded!\n"

I have already extracted the body of the SMS but the variable doesn't seem to pass through the condition if (msg == "on"). Can you help me about this problem?

1

This my answer using regex.

print "Opening communications serial...\n"
ser = serial.Serial('/dev/ttyUSB1',115200,timeout=1)
ser.open()
#check continous loop
print "Starting main loop\n"

while True:
  message = CheckUnreadMsgs(ser)
  a = message.find('\n') + 63
  b = len(message)
  msg = message[a:b]
  m = re.search("(pow.*)",msg)
  if m:
      mm = (m.group(1))
      if "on" in mm:
         print "Power On"
      elif "off" in mm:
         print "Power Off"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.