Hello, I am using sim800l gsm with arduino nano. It should send a message to the number I specified in the software. I just need to change the number in each project. Can I send a message to the number I registered in the phone book of my sim card? Thank you to everyone who helped. Good forums.
Hi, a quick look in the SIM800 AT Command manual revealed an example exactly covering your question. Page 356, 20.2 SIM Commands.
thanks for your answer.
AT+CPBR=1
I can view the number in the directory with the command. I failed to message this number
can you help me?
So you basically requested the phone book entry at location 1. and the module response was something like this?
+CPBR: (1-250),40,14
OK
1, "PHONE_NUMBER",145,"ENTRY_NAME"
OK
You need to store this response in a char array (string), then extract and store the phone number into another array. After this you can use the phone number char array to construct the SMS command. As you didn't post any code, it's hard to give more detailed help.
You could check out this SIM900 Tutorial: Step 5. It uses a sendATcommand function which waits for an expected answer and stores it, so you will be able to extract the phone number after request.
thanks for your answer.
code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
char Received_SMS;
String PhoneNumber;
String Number;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("Initializing...");
delay(1000);
PhoneNumber = "";
mySerial.println("AT");
updateSerial();
delay(10);
mySerial.println("AT+CMGF=1");
updateSerial();
delay(10);
mySerial.println("AT+CNMI=1,2,0,0,0");
updateSerial();
delay(10);
mySerial.println("AT+CMGL=\"REC UNREAD\"");
updateSerial();
delay(10);
mySerial.println("AT+CPBR=1""\r");
updateSerial();
delay(100);
PhoneNumber = mySerial.readString();
delay(100);
Serial.println("PhoneNumber");
Serial.println(PhoneNumber);
/*mySerial.println("AT+CMGS=\"+905004003020\"");
updateSerial();
delay(10);
mySerial.print("Hello");
updateSerial();
mySerial.write(26);*/
}
void loop()
{
while(mySerial.available()>0) {
Received_SMS=mySerial.read();
Serial.print(Received_SMS);}
}
void updateSerial()
{
delay(500);
while (Serial.available()) { mySerial.write(Serial.read()); }
while(mySerial.available()) { Serial.write(mySerial.read()); }
}
SERİAL MONİTOR
Initializing...
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
AT+CMGL="REC UNREAD"
OK
AT+CPBR=1
+CPBR: 1,"05004003020",129,"Mrt"
OK
PhoneNumber
**I am using sim800l. I can view the number in the phone book, but I cannot save this number to a variable and send a message. Where am I doing wrong? Thank you for your help.
I could say, you're not working for it too hard.
Okay, you don't have to, the uC will do that for you. First of all, you need to forget about String class and use C strings instead.
I am using sim800l.
I know the tutorial was for SIM900, but I'm quite sure these basic commands are common for them. I would boldly use the sendATcommand function from the tutorial. You just need to change it a little bit, as you are using software serial for the gsm shield.
uint8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned long timeout)
{
uint8_t answer = 0;
uint8_t x = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialise the string
delay(100);
while( mySerial.available() > 0) mySerial.read(); // Clean the input buffer
mySerial.println(ATcommand); // Send the AT command
previous = millis();
do // this loop waits for the answer
{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(mySerial.available() != 0)
{
response[x] = mySerial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}
while((answer == 0) && ((millis() - previous) < timeout)); // Waits for the asnwer with time out
//Print response to Serial monitor
Serial.println(response);
return answer;
}
Then you just simply call it with the needed command. For example to request the first phonebook entry:
sendATcommand("AT+CPBR=1", "OK", 10000ul);
This separate function with the expected response check also has the advantage of managing errors. So if the expected answer is not received, you can e.g. restart the module and try again instead of just blindly send out the next command. But let's focus on your current task.
After the upper command, response array will hopefully contain this:
+CPBR: 1,"05004003020",129,"Mrt"
OK
For your defense, the tutorial used a fixed phone number, so this is how it could be done. Are you familiar with strtok and strcpy?
//phone number should be a global, it will be needed for AT command composing
char phone_number[16] = "";
//this could be done inside sendATcommand or in main (make response global), your call
//(inside sendATcommand another parameter could indicate to get phone number)
if (getPhoneNumber)
{
char* pch;
pch = strtok(response, ",\""); //start splitting response string, at commas and double quotation marks
pch = strtok(NULL, ",\""); //continue splitting, because phone number is the 2nd token
strcpy(phone_number, pch); //pch now points to the number, simply copy it to phone_number
}
Okay, now you have the phone number in a char array, just like in the tutorial!
This is the key for the next step:
char aux_string[30];
sprintf(aux_string, "AT+CMGS=\"%s\"", phone_number);
By the way, I'd rather use snprintf:
char aux_string[30];
snprintf(aux_string, 30, "AT+CMGS=\"%s\"", phone_number);
Thanks a lot for your answer. I am a beginner in Arduino software and trying to improve myself. I will look into your answer and try to do it. thanks again.
You're welcome.
Hello, I am using sim800l gsm with arduino nano. It should send a message to the number I specified in the software. I just need to change the number in each project. Can I send a message to the number I registered in the phone book of my sim card? Thank you to everyone who helped. Good forums.
Hi good forums.
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
#include "call.h"
CallGSM call ;
SMSGSM sms ;
void setup()
{
Serial.begin (9600) ;
Serial.println("GSM GPRS Shield");
delay(2000);
if (gsm.begin(4800)){
Serial.println("\nstatus=READY");
sms.SendSMS(1,"HELLO WORLD!");
Serial.write("AT+CPBR=1\r");
delay (3000);
Serial.println("SEND SMS");
}
else Serial.println("\nstatus=IDLE");
}
void loop ()
{
}
With this code, I can send an automatic message to the registered number in the directory. I want to save this number in a variable and use it in the code. Please help me.
TOPIC MERGED.
Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
Account on hold for 24 hours to allow you to catch up with forum rules.
Hi, okay you switched to a library. You are using this: GSMSHIELD, right?
SIM900.h include created an object called gsm. You can access it's inherited GetPhoneNumber() method.
You can simply use it like this:
char saved_number[16];
gsm.GetPhoneNumber(1, saved_number);
Hi, I was able to run it without any problems, albeit mixed. I'm attaching the code, if you have any suggestions, I'd like to know. I will add more: status control, timer and will notify when power is gone. Thank you very much.
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
boolean started=false;
char smsbuffer[160];
String msg=String("");
int RELAY=9;
char n[16];
String num;
char saved_num[16];
String saved_number;
void setup() {
Serial.begin(9600);
Serial.println("GSM shield testing.");
if(gsm.begin(9600)) {
Serial.println("\nstatus=READY");
gsm.GetPhoneNumber(1, saved_num);
saved_number=String(saved_num);
sms.SendSMS (1, "SYSTEM READY");
delay (1000);
Serial.println("\nSMS sent OK"); }
else Serial.println("\nstatus=IDLE");
pinMode( RELAY, OUTPUT );
digitalWrite( RELAY, LOW ); }
void loop() {
if(gsm.readSMS(smsbuffer,160,n,20)) {
Serial.println(n);
num=String(n).substring(2,13);
Serial.println(smsbuffer);
msg=String(smsbuffer);
msg.toUpperCase(); }
delay(1000);
if(msg.indexOf("ON")>=0) {
digitalWrite( RELAY, HIGH);
if (sms.SendSMS(1, "OPENED")) {
Serial.println("\nSMS sent ok"); }
if (num != saved_number){
if (sms.SendSMS(n,"OPENED")) {
Serial.println("\nSMS sent ok"); } }
msg=String(""); }
if(msg.indexOf("OFF")>=0) {
digitalWrite( RELAY, LOW);
if (sms.SendSMS(1, "CLOSED")) {
Serial.println("\nSMS sent ok"); }
if (num != saved_number){
if (sms.SendSMS(n, "CLOSED")) {
Serial.println("\nSMS sent ok"); } }
msg=String(""); } }