In our previous post we used UART signals to connect to Sim900. We also tried to make a call and send a text message.
So far, we didn’t consider too much responses to commands which are sent to the module. Today we’re going to explore them in detail.
As an example, we’ll study getting the module’s status. Also, we’ll try to get incoming calls and text messages.
Let’s start from getting messages from the module. Earlier we used sleep function, as we didn’t know for sure when exactly the data is ready to be read. Let’s use pool function, so that the thread wakes up once the data is available for reading.
func waitForRead() -> Bool
{
var devPoolFd : pollfd = pollfd(fd: sim900descriptor, events: Int16(POLLIN), revents: 0)
let poolResult : Int32 = poll(UnsafeMutablePointer([devPoolFd] as Array), 1, -1);
return poolResult > 0;
}
We don’t have to wait long as we get a response once the command is processed. By default, the module sends echo of the received command. So this should be taken into account when reading the response.
Echo is followed by newline and status of the command execution - ОК or ERROR. You can disable getting echo of your command by “ATE0” command. The response will be of “+:“ format.
Here are some examples:
Getting info about operator:“AT+COPS?”
We get the response:
+COPS: 0,0,”AT&T”
The response is 0,0,”AT&T”
The first parameter indicates that GSM network was chosen automatically. The third parameter stays for the operator name.
Here’s how this command can be sent in Swift:
let cmd = "AT+COPS?\r\n"
write(sim900descriptor, cmd, UInt(countElements(cmd)))
Sending all the subsequent commands in this article is similar to this one.
You can see what other commands return:
AT+CSPN? - SIM card’s operator
AT+CSQ - signal strength
AT+CBC - voltage, battery level
AT+COPS=? - available operators in the coverage area
In our previous post we learned how to make an outgoing call. When receiving an incoming call, we get “RING” string at certain intervals. We can response to the call by “ATA” command or reject the call by “ATH” command.
In order not to get incoming calls at all, reject them by AT+GSMBUSY=1 command.
AT+CLCC command is needed to get the call’s status. Here are the command parameters:
+CLCC: ,,,,
To get the telephone number of the caller, execute AT+CLIP=1. The answering machine will send
RING together with the telephone number:
+CLIP:,…
If you get +CPIN: SIM PIN, it means that working with a SIM card is possible after entering the PIN code via AT+CPIN= command.
OK, so now we know how to get and reject incoming calls and get the status of the call.
In our previous post we learned how to send a text message. Incoming messages are received in +CMTI: “SM”, format.
stays for the number assigned to the saved message
We’ll use this number to get the contents of the message via AT+CMGR command:
AT+CMGR=,
Now we get the response to CMGR command:
+CMGR: “REC UNREAD”,“+496967733496”,“”,“14/10/24,16:14:17+12”
Message from phone
Clean out your text messages periodically to free space, otherwise you won’t be able to send or get a new message. To delete text messages from all folders, execute at+cmgd=1,4
To view all messages, execute AT+CMGL.
So, these are the basic functions of GSM phone based on Sim900 radio module. Here’s a code sample in Swift we’ve prepared for you. Sim900.swift contains more methods to use in your work.
Stay tuned!
Subscribe here:
https://www.facebook.com/develtima
https://twitter.com/develtima