Skip to content

Instantly share code, notes, and snippets.

@biskandar
Created October 18, 2013 14:04
Show Gist options
  • Save biskandar/7042010 to your computer and use it in GitHub Desktop.
Save biskandar/7042010 to your computer and use it in GitHub Desktop.
Arduino Uno + GSM Shield + StarHub Sim Card : Send / Recv SMS
#include <GSM.h>
// the gsm library instances
GSM gsmAccess ;
GSM_SMS sms ;
void setup() {
// initialize serial communications
Serial.begin( 9600 ) ;
// start gsm shield
Serial.println( "GSM is initializing" ) ;
boolean notConnected = true ;
while ( notConnected ) {
if ( gsmAccess.begin() == GSM_READY ) {
notConnected = false ;
} else {
Serial.println( "GSM is not connected" ) ;
delay( 1000 ) ;
}
}
Serial.println( "GSM is initialized" ) ;
}
void loop() {
// when there is new sms arrived it will raised sms.available() as true
if ( sms.available() ) {
// read the mobile number
char mobileNumber[20] ;
sms.remoteNumber( mobileNumber , 20 ) ;
// read the message text
char ch ;
String st = String( "" ) ;
while ( ch = sms.read() ) {
st += ch ;
}
sms.flush() ;
int lenText = st.length() + 1 ;
char messageText[ lenText ] ;
st.toCharArray( messageText , lenText ) ;
// display it
Serial.print( mobileNumber ) ;
Serial.print( " > " ) ;
Serial.println( messageText ) ;
// send the reply sms back with prefix RE:
sms.beginSMS( &mobileNumber[1] ) ;
sms.print( "RE: " ) ;
sms.print( messageText ) ;
sms.endSMS() ;
delay( 1000 ) ;
} // if ( sms.available() )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment