Hi everyone
I got a Tinysine GSM/GPRS Shield with SIM900 (total newb on this one) trying to get it to work with AT commands. I have gone through all examples given by the manufacturer at their website, with an Arduino UNO and a Mega 2560 all under great success.
Unfortunately there some things I want to do in the future regarding the GPRS FTP file transfers which I cannot locate how these can be implemented with the Tinysine GSM/GPRS shield. That is why I turned over to AT Commands where a plethora of code examples are at hand.
The problem I faced was that even when I tried to send a simple SMS totaly relying upon AT commands I failed big. I though that AT commands are solving the need of libraries, storage and compatibility around all SIM900 modules since they are "just serial" because these are common instructions across all manufacturers.
I used the code below but couldn't send anything
/*
* GPRS/GSM Quadband Module (SIM900)
*
* Copyright (C) Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* a
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Version: 2.0
* Design: David Gascón
* Implementation: Alejandro Gallego & Marcos Martinez
*/
int8_t answer;
int onModulePin= 2;
char aux_string[30];
char phone_number[]="+***********"; // ********* is the number to call
char pin[] = "****";
char sms_text[]="Test-Arduino-Hello World";
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(9600);
Serial.println("Starting...");
power_on();
delay(3000);
// sets the PIN code
sprintf(aux_string, "AT+CPIN=%s", pin);
sendATcommand(aux_string, "OK", 2000);
delay(3000);
Serial.println("Connecting to the network...");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
Serial.print("Setting SMS mode...");
sendATcommand("AT+CMGF=1", "OK", 1000); // sets the SMS mode to text
Serial.println("Sending SMS");
sprintf(aux_string,"AT+CMGS=\"%s\"", phone_number);
answer = sendATcommand(aux_string, ">", 2000); // send the SMS number
if (answer == 1)
{
Serial.println(sms_text);
Serial.write(0x1A);
answer = sendATcommand("", "OK", 20000);
if (answer == 1)
{
Serial.print("Sent ");
}
else
{
Serial.print("error ");
}
}
else
{
Serial.print("error ");
Serial.println(answer, DEC);
}
}
void loop(){
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){ // Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialice the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
I know that this is for a libelium GSM shield but still these are all plain AT Commands. Can anyone please inform me why I failed so big in both UNO and Mega 2560... apart from being noob!
Is it something that has to do with just RX,TX pins cause I have tried many different things but still... failure.
I know that it has nothing to do with baud rate cause they are synced at 9600 both Arduino and Tinysine GSM module. I have also crosschecked with the AT+IPR? command.
I have tried to write all required AT commands through Arduino's serial monitor but unfortunately ctrl-z cannot be written in either form hex or otherwise therefore I cannot send an SMS manually. I have succeeded through another serial terminal but not with Arduino IDE 1.0.6 (required for Tinysine GSM library) serial monitor. Why is that?