Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.11 KB | None | 0 0
  1. void send_sms(char* text){
  2.   Serial.println("AT+CMGF=0"); // Set PDU mode
  3.   delay(3000);
  4.  
  5.   String phone_output = "0011000B91";
  6.  
  7.   String phone_number = calculate_phone_number();
  8.   phone_output.concat(phone_number);
  9.  
  10.   phone_output.concat("0000AA");  // TP-PID, TP-DCS, TP-validity
  11.  
  12.   int msg_len = strlen(text);
  13.   String hex_len = pad_hex(String(msg_len, HEX));
  14.  
  15.   Serial.println(hex_len);
  16.   phone_output.concat(hex_len);
  17.   String converted_text = convert_text_to_pdu(text);
  18.  
  19.   phone_output.concat(converted_text);
  20.  
  21.   int number_of_octets = phone_output.length() / 2;
  22.   Serial1.print("AT+CMGS=");
  23.   Serial1.println(number_of_octets);
  24.   delay(3000);
  25.  
  26.   Serial1.print(phone_output);
  27.   delay(3000);
  28.  
  29.   Serial1.write(26);
  30.   delay(3000);
  31.  
  32.   Serial.println("sent!");
  33. }
  34.  
  35. String calculate_phone_number(){
  36.   return "8346355555F5";
  37. }
  38.  
  39. String convert_text_to_pdu(char* text){
  40.   String converted = "";
  41.  
  42.   int data_length = strlen(text);
  43.   int to_append_index = 1;
  44.   for (int x = 0; x < data_length; x++){
  45.     String curr_char = String(text[x], BIN);
  46.    
  47.     if(to_append_index == 8){
  48.       to_append_index = 1;
  49.       continue;
  50.     }
  51.    
  52.     curr_char = String(curr_char.substring(0, curr_char.length() - (to_append_index - 1)));
  53.  
  54.     String to_append = "";
  55.     if(x != data_length - 1){
  56.       // not last character
  57.       String next_char = String(text[x+1], BIN);
  58.       to_append = String(next_char.substring(next_char.length() - to_append_index));
  59.     }
  60.     curr_char = to_append + curr_char;
  61.  
  62.     int converted_len = curr_char.length();
  63.     int convert_char = 0;
  64.     for(int bi = 0;bi<converted_len;bi++){
  65.       char c = curr_char.charAt(bi);
  66.       int curr_bit = c-'0';  // convert to int
  67.      
  68.       int partial = curr_bit * 1 << (converted_len-1-bi);
  69.       convert_char += partial;
  70.     }
  71.     String hex_char = pad_hex(String(convert_char, HEX));
  72.  
  73.     converted.concat(hex_char);  // concatenate to return string
  74.  
  75.     to_append_index++;
  76.   }
  77.  
  78.   return converted;
  79. }
  80.  
  81. String pad_hex(String hex){
  82.   if(hex.length() % 2 != 0){
  83.     return 0 + hex;
  84.   }
  85.   return hex;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement