Code:
#include <SoftwareSerial.h>
SoftwareSerial GPRS(7, 8);
String theMsgStuff= "I am woody the cowboy";
void setup(){
GPRS.begin(19200); // Start the SoftwareSerial wich is using the GPRS UART
Serial.begin(19200); // Start the HardwareSerial wich is using the Arduino UART ( DEBUG OR WHEN CONNECTED TO A COMPUTER .. )
delay(1000);
Serial.println();
Serial.println("[ DEBUG ] mini gprs responder v0.1a");
}
void loop(){
handleIncomingMessages();
}
// main Fcns
void handleIncomingMessages(){
if( GPRS.available() ){ // if new data is coming from the GPRS UART
String content= "";
char character;
while( GPRS.available() ){ // while there's still data in serial ..
character = GPRS.read(); // read a char from the serial to the char
content.concat( character ); // use concat to append the new character to the content
}
// after that, display content if not empty or NULL
if( content != "" ){
//Serial.println( content );
handleMessage( content ); // parse message coming the GPRS UART
content = ""; // empty content
}
}
}
void handleMessage(String aMessage){
if( aMessage.startsWith("\r\n+CMTI: "SM"") ){ // message received from GPRS UART
Serial.println("[ DEBUG ] The SIM900 received a new message");
initMessageRead();
} else {
// I don't fuck*** care (..)
}
}
void initMessageRead(){
GPRS.print("AT+CMGR=1\r"); // write the "read last message" request to the GPRS UART
delay(1000); // let the GPRS print back the response to our request
String messageReceived = getResponseFromGPRS();
Serial.println("[ DEBUG ] Message content from 'initMessageRead()' : ");
Serial.println("[ raw message start ] " + messageReceived + " [ raw message end ] " );
GPRS.print("AT+CMGD=1,4\r"); // write the "delete all messages" request to the GPRS UART
delay(1000); // give it a little bit of time to execute ( and normally return '+CMGR:' )
String messageDeleteSuccFail = getResponseFromGPRS();
Serial.println("[ DEBUG ] Delete all messages received request from 'initMessageRead()' : ");
Serial.println("[ code start ] " + messageDeleteSuccFail + " [ code end ] " );
Serial.println("[ DEBUG ] Infos parsed from message : ");
// get infos from message:
// sender number
String messageOrigin = getMessageOrigin( messageReceived );
Serial.println("[ DEBUG ] Message origin : ");
Serial.println("[ origin start ] " + messageOrigin + " [ origin end ] " );
// actual message ( parsed )
String actualMessage = getActualMessage( messageReceived );
Serial.println("[ DEBUG ] Actual message : ");
Serial.println("[ message start ] " + actualMessage + " [ message end ] " );
// user auth code
String userAuthCode = getAuthCode( actualMessage );
Serial.println("[ DEBUG ] User authentication code : ");
Serial.println("[ auth code start ]" + userAuthCode + "[ auth code end ] " );
userAuthCode = userAuthCode.substring( 1, 5);
Serial.println("[ DEBUG ] User authentication code value is now : " + userAuthCode );
// if user code is admin, set a var of the sketch
Serial.println();
if( userAuthCode.equals("y33b") ){
Serial.println("[ DEBUG ] USER AUTHENTICATION ");
delay(500);
String theMessageToSend = getMessageTocompStuff();
sendMessage( messageOrigin, theMsgStuff );
} else if( userAuthCode.equals("z34a") ){
Serial.println("[ DEBUG ] ADMIN AUTHENTICATION ");
delay(500);
// get the new data from the admin
String theNewNum = getNewNumber( actualMessage );
Serial.println("[ DEBUG ] New number from Admin is : ");
Serial.println("[ new number start ]" + theNewNum + "[ new number end ] " );
//setMessageTocompStuff("ton num est: " + messageOrigin);
setMessageTocompStuff( theNewNum );
delay(500);
String theMessageToSend = getMessageTocompStuff();
sendMessage( messageOrigin, theMsgStuff );
} else if( userAuthCode.equals("unauthorized") ){
Serial.println("[ DEBUG ] UNKOWN AUTHENTICATION ");
} else {
// should not happen (..)
}
}
// Fcn to send text messages
void sendMessage(String number, String dataToSend){
//Serial.println("[ DEBUG ] Composing message for :" + number + " with: " + dataToSend + " as message content");
//delay(500);
String composeMsgCmd = "AT+CMGS="" + number + """; // build the 'compose new text message' AT command
GPRS.print( composeMsgCmd + "\r"); // write the "compose new text message" request to the GPRS UART
delay(1000); // give it a little bit of time to execute ( and normally return '> ' )
// add content to the message
GPRS.print( dataToSend );
delay(1000);
GPRS.print( (char)26 ); //the ASCII code of the ctrl+z is 26
delay(1000); // give it a little bit of time to execute ( and normally return '+CMGS: ' )
// THE MESSAGE SHOULD HAVE BEEN SENT TO WHOEVER REQUESTED THE NEW NUMBER BY NOW
Serial.println("[ DEBUG ] Message sent!");
}
// Test Fcn
String getResponseFromGPRS(){
if( GPRS.available() ){ // if new data is coming from the GPRS UART
String responseContent= "";
char responseChar;
while( GPRS.available() ){ // while there's still data in serial ..
responseChar = GPRS.read(); // read a char from the serial to the char
responseContent.concat( responseChar ); // use concat to append the new character to the content
}
return responseContent; // return the responseContent
}
}
// Fcn to set the message stuff
void setMessageTocompStuff(String theNewMessageStuff){
theMsgStuff = theNewMessageStuff;
}
// Fcn to get the message stuff
String getMessageTocompStuff(){
return theMsgStuff;
}
// HELPER FCNS FOR STRING PARSING //
// to get the number of the sender of the message
String getMessageOrigin(String theMessageFromGPRS){
//String theMsgOrigin = theMessageFromGPRS.substring(21,33); // extract what is between index 21 and index 33, wich hould be the number the message originated from
String theMsgOrigin = theMessageFromGPRS.substring(33,45);
return theMsgOrigin; // return it
}
// Fcn to trimm Strings
String trimmThat(String stuffToTrimm){
stuffToTrimm.trim();
return stuffToTrimm;
}
// to get the actual message out of a message received from the GPRS UART
String getActualMessage(String theMessageFromGPRS){
//String theActualMsg = theMessageFromGPRS.substring(61,78); // extract what is between index 61 and index 78, wich hould be the actual message
//String theActualMsg = theMessageFromGPRS.substring(73,90);
//String theActualMsg = theMessageFromGPRS.substring(81,98);
//String theActualMsg = theMessageFromGPRS.substring(81,140); // 30 more than necessary ;p but seems to leave the \r\n before the real content
String theActualMsg = theMessageFromGPRS.substring(83,110); // fixed 110 cause i am afraid it could cause some leaks ..
//theActualMsg.trim(); // the actual message has a \r\n in front of it (..)
return theActualMsg; // return it
}
// to get the authentication code from the actual message
String getAuthCode(String theActualMessage){
String theAuthCode = theActualMessage.substring(0,7); // extract what is between index 0 and index 6, wich should be the authentication code
return theAuthCode; // return it
}
// to get the new number from the actual message
String getNewNumber(String theActualMessage){
//String theNewNumber = theActualMessage.substring(7,17); // extract what is between index 8 and index 17, wich should be the new number
String theNewNumber = theActualMessage.substring(7,18);
return theNewNumber; // return it
}
// to get the number out of the phonebook entry
String getNumberFromPhonebookEntry(String theMessageFromGPRS){
String theCurrentNumber = theMessageFromGPRS.substring(10,20); // extract what is between index 10 and index 20, wich should be the current number held by the phonebook entry
return theCurrentNumber; // return it
}