AGARDO
Posts: 2
Joined: Sun May 19, 2019 3:18 pm

Sending SMS with SIM900

Sun May 19, 2019 4:05 pm

Hello all
I have a Raspberry 3 and a sim900 shield and is sending SMS from my mobile to them. The setup answers ok. The problem is that the reply has to be "hardcoded" and I want the reply to be sent to where the SMS came from. For some reason I can't get the AT-command to work here, obviusly I get something wrong. Anyone?

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SIMSMS.py

import RPi.GPIO as GPIO
import serial
import time, sys
import datetime

P_BUTTON = 24 # Button, adapt to your wiring
WAKE_SIM = 22  #Start SIM900

def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(P_BUTTON, GPIO.IN, GPIO.PUD_UP)
    GPIO.setup(WAKE_SIM,GPIO.OUT)
    
def smsin(instring):
	i=15  #skip first part
	answer=" "
	while i<len(instring):
		s = instring[i]
		i += 1
		if s == "+" :
			i -= 1
			while (s != '"'):
				s = instring[i]
				answer += s
				i += 1
			break	
	answer = answer[1:len(answer)-1]
	return answer;
    
#SERIAL_PORT = "/dev/ttyAMA0"  # Raspberry Pi 2
SERIAL_PORT = "/dev/ttyS0"    # Raspberry Pi 3

ser = serial.Serial(SERIAL_PORT, baudrate = 9600, timeout = 5)
setup()

'''
# Soft start SIM900
GPIO.output(WAKE_SIM, GPIO.LOW)
time.sleep(1)
GPIO.output(WAKE_SIM, GPIO.HIGH)
time.sleep(3)
GPIO.output(WAKE_SIM, GPIO.LOW)
time.sleep(5)
'''

ser.write("AT+CMGF=1\r") # set to text mode
time.sleep(3)
ser.write('AT+CMGDA="DEL ALL"\r') # delete all SMS
time.sleep(3)
reply = ser.read(ser.inWaiting()) # Clean buf
print ("Listening for incomming SMS...")
while True:
    incomingcall = ""
    i = 0
    reply = ser.read(ser.inWaiting())
    if reply != "":
        ser.write("AT+CMGR=1\r") 
        time.sleep(3)
        reply = ser.read(ser.inWaiting())
        print ("SMS received. Content:")
        print (reply)
        incomingcall = smsin(reply)
        print (incomingcall)
        time.sleep(3)
        if "getStatus" in reply:
            t = str(datetime.datetime.now())
            if GPIO.input(P_BUTTON) == GPIO.HIGH:
                state = "Button released"
            else:
                state = "Button pressed"
   
            ser.write('AT+CMGS=str(incomingcall)\r') ####THIS IS WHERE IT FAILS!!
            time.sleep(3)
            msg = "Sending status at " + t + ":--" + state
            print ("Sending SMS with status info:" + msg)
            ser.write(msg + chr(26))
        time.sleep(3)
        ser.write('AT+CMGDA="DEL ALL"\r') # delete all
        time.sleep(3)
        ser.read(ser.inWaiting()) # Clear buf
    time.sleep(5)    


User avatar
topguy
Posts: 5772
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Sending SMS with SIM900

Sun May 19, 2019 11:54 pm

Code: Select all

ser.write('AT+CMGS=str(incomingcall)\r') ####THIS IS WHERE IT FAILS!!
I think this actually sends "AT+CMGS=str(incomingcall)" to the modem..

I'm no python expert ( and it too late to google ) but maybe something similar to this..

Code: Select all

ser.write('AT+CMGS=' + str(incomingcall) + '\r') 

AGARDO
Posts: 2
Joined: Sun May 19, 2019 3:18 pm

Re: Sending SMS with SIM900

Thu May 23, 2019 9:35 am

I have found the problem with sending SMS to sender, with the number in heading that is.
The sending row should be

Code: Select all

ser.write('AT+GMS=' + '"' + str(incomingcall) + '"' + '\')
Single quotes around double quotes.

Return to “Python”