Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created November 6, 2016 12:40
Show Gist options
  • Save lsongdev/a33d65158fbd20ec7a495fbc0c1e53f9 to your computer and use it in GitHub Desktop.
Save lsongdev/a33d65158fbd20ec7a495fbc0c1e53f9 to your computer and use it in GitHub Desktop.
void send_sms(String message, String phone_nr )
{
byte original_byte;
byte newbyte;
int pdu_length;
int counter;
String newphone_nr;
int numlength;
delay(1000);
Serial.begin(9600);
delay(1000);
numlength=phone_nr.length()-1;
if (numlength% 2 != 0 ) {
phone_nr += "F";
}
for(int x = 0; x < numlength; x = x+2){
newphone_nr.concat(phone_nr.charAt(x+2));
newphone_nr.concat(phone_nr.charAt(x+1));
}
//start sending SMS
Serial.println("AT"); //optional: say hello to the modem
delay(3000);
Serial.println("AT+CMGF=0"); //set pdu mode
delay(3000);
int textlength= message.length()*7/8; // text message length: number of characters*7/8
if (message.length()*7%8 != 0) {
textlength++;
}
if (numlength%2 != 0) { //if the length of phone number was uneven, it now increases by 1 because of the added "F"
pdu_length = (8 + (numlength+1)/2 + textlength);
}
else { //8 bytes of parameters + (length of phonenumber)/2 + number of bytes in the text
pdu_length = (8 + numlength/2 + textlength);
}
Serial.print("AT+CMGW="); //message will be stored in the mobile phone - AT+CMGS will send it off.
//delay(3000);
Serial.println(pdu_length);
//delay(100);
Serial.print("001100"); //these are some parameters which we don't need to bother about
//delay(100);
if (numlength<16){ //now we have to send the byte containing the length of the phone number in HEX format
Serial.print(0);
//delay(100);
} //the byte in HEX format needs to be 2 characters long; if needed we add a 0 in front, e.g. "C" will become "0C"
Serial.print(numlength,HEX); //length telnr - this time any additional F is not to be considered
// delay(100);
Serial.print("91"); //international format of phone number //81 for national format
//delay(100);
Serial.print(newphone_nr); //now we send the rearranged phone number
//delay(100);
Serial.print("0000AA"); //some more parameters
//delay(100);
if (message.length()<16){
Serial.print(0);
//delay(100);
} //a byte in HEX format indicating the length of the text. Again the byte needs to be made of 2 characters
Serial.print(message.length(),HEX);
//delay(100);
counter=0;
newbyte=0;
for (int j=0; j<message.length(); j++) {
original_byte = message.charAt(j); //one by one we take each byte out of the original text
for (int i=0; i<7; i++) {
newbyte=newbyte|bitRead(original_byte, i)<<counter; //take the bits 0 to 6 of the old byte, shift as required for the new byte and add to the new byte
counter++;
if (counter==8) {
counter=0; //if a new byte is complete, set the counter to 0
if (newbyte<16){
Serial.print(0);
// delay(100);
} //each byte in HEX format should consist of 2 characters, if necessary add 0 in front
Serial.print (newbyte, HEX); //when the new byte is finished, send it to the mobile phone
//delay(100);
newbyte=0; // the next new byte will start again from 0
}
}
}
if (message.length()*7%8!=0) { //if there were remaining bits (not enough to form a new byte), these are sent out as the last byte
if (newbyte<16){
Serial.print(0);
//delay(100);
} //each byte in HEX format should consist of 2 characters, if necessary add 0 in front
Serial.println (newbyte, HEX);
//delay(100);
}
delay (3000);
Serial.write(26); //this is a command telling the mobile phone that the end of the transmission has been reached
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment