I had worked on GSM long back ago and sometimes it becomes very difficult to debug the problem. However u need to just check whether the AT commands are being sent properly to the GSM module. I had tried using Software Serial but it did not work for me but when I switched to the usual Serial port it worked like a charm. Few things that you need to check are:
- Check the baud rate of GSM module. Does it match with the baud rate of Software Serial.
- Do a if-else check to see whether the commands are actually being sent to the GSM module. The println() returns the number of bytes written so you can do like this in your sendSMS() function.
int a; //To store the number of bytes written
a=SIM900.println("AT command to be sent");
Serial.println("Command not sent");
Now upload the code and open the serial monitor to see whether the command is actually being sent to the GSM module or not.
- You also need to check whether the format of your AT command is correct. Because you need to send \n(newline) and\r(carriage return) at the end of every AT command to ensure proper operation. For this you need to read the response sent back by the GSM module after every command. This can be done by reading the Software serial port and the printing the response onto the Serial monitor.
- You also need to check whether your GSM module has detected the SIM card. Usually this is indicated by an onboard LED that blinks after every 3 seconds.
I think your code is pretty much correct but try to ensure that everything is setup properly as I said.
Here is my code but I used Serial port instead:
delay(10000); \\For initialization purpose
Serial.println("AT+CMGS=\"Phone number with international code\"");
This code will send the SMS once so you need to reset the board for sending another SMS or you can remove the do while loop for continuous operation.You can adjust the delay accordingly.