Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void send_sms(char* text){
- Serial.println("AT+CMGF=0"); // Set PDU mode
- delay(3000);
- String phone_output = "0011000B91";
- String phone_number = calculate_phone_number();
- phone_output.concat(phone_number);
- phone_output.concat("0000AA"); // TP-PID, TP-DCS, TP-validity
- int msg_len = strlen(text);
- String hex_len = pad_hex(String(msg_len, HEX));
- Serial.println(hex_len);
- phone_output.concat(hex_len);
- String converted_text = convert_text_to_pdu(text);
- phone_output.concat(converted_text);
- int number_of_octets = phone_output.length() / 2;
- Serial1.print("AT+CMGS=");
- Serial1.println(number_of_octets);
- delay(3000);
- Serial1.print(phone_output);
- delay(3000);
- Serial1.write(26);
- delay(3000);
- Serial.println("sent!");
- }
- String calculate_phone_number(){
- return "8346355555F5";
- }
- String convert_text_to_pdu(char* text){
- String converted = "";
- int data_length = strlen(text);
- int to_append_index = 1;
- for (int x = 0; x < data_length; x++){
- String curr_char = String(text[x], BIN);
- if(to_append_index == 8){
- to_append_index = 1;
- continue;
- }
- curr_char = String(curr_char.substring(0, curr_char.length() - (to_append_index - 1)));
- String to_append = "";
- if(x != data_length - 1){
- // not last character
- String next_char = String(text[x+1], BIN);
- to_append = String(next_char.substring(next_char.length() - to_append_index));
- }
- curr_char = to_append + curr_char;
- int converted_len = curr_char.length();
- int convert_char = 0;
- for(int bi = 0;bi<converted_len;bi++){
- char c = curr_char.charAt(bi);
- int curr_bit = c-'0'; // convert to int
- int partial = curr_bit * 1 << (converted_len-1-bi);
- convert_char += partial;
- }
- String hex_char = pad_hex(String(convert_char, HEX));
- converted.concat(hex_char); // concatenate to return string
- to_append_index++;
- }
- return converted;
- }
- String pad_hex(String hex){
- if(hex.length() % 2 != 0){
- return 0 + hex;
- }
- return hex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement