请有人告诉我为什么这段代码不起作用。我想提取消息发送者的号码,然后使用AT命令将消息转发给它。它提取发件人的数量并将其存储在变量中,但为什么不向该号码发送消息呢?
#include <GSM.h>
GSM_SMS sms;
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "xxxxxxxxxx";
String number;
char inchar;
char outString[22];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
if(inchar == '#')
{
sendInfo();
}
}
}
}
}
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1000);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
delay(1000);
Serial1.print("HI");
delay(1000);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1000);
Serial.println("sms sent ");
} //end sendInfo()
答案 0 :(得分:1)
好的,问题似乎与这些问题有关:
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
但如果我们写下面写的行,程序就可以了!
Serial1.write("AT+CMGS=\"");
Serial1.print(number);
Serial1.write("\"\r");
答案 1 :(得分:-1)
“将消息发送回消息发送者”的代码(还原) GSM与Arduino UNO接口。
/* Code done by Vatsal Mehta <Vatsalmehta121297@gmail.com>
contact for any doubts and clarifications.
i'm very active at my mail so you may expect the reply soon enough
*/
#include <SoftwareSerial.h>
SoftwareSerial Serial1(9, 10);
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "8147XXXXXX"; // This variable is of no need in program, it is merely used to use your number for testing
String number;
char inchar;
char outString[22];
void sendInfo();
void recSms();
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++) // calibration is not done as it is not required
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
sendInfo();
}
}
}
}
void sendInfo()
{
Serial1.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1 second
Serial1.print("AT+CMGS=");
Serial1.print("\"");
Serial1.print(number);
Serial1.print("\"");
Serial1.println("\r"); //
delay(1000);//
//1Serial1.print("\n");
Serial1.println("Vatsal Mehta, BMSCE, Bangalore");// The SMS text you want to send
delay(100);
Serial1.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to the module
delay(1000);
// below code is just to show what is printing in Serial1 in Serial Monitor. (below code is not necceassay if you want to optimize the program)
Serial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1 second
Serial.print("AT+CMGS=");
Serial.print("\"");
Serial.print(number);
Serial.print("\"");
Serial.print("\r");
delay(1000);
Serial.print("\n");
Serial.println("Vatsal Mehta, BMSCE, Bangalore");// The SMS text you want to send
delay(100);
Serial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to the module
delay(1000);
number = "\0"; // inorder to clear the contents of number variable
while (Serial1.available() > 0) // Don't read unless
{
Serial.write(Serial1.read());// there you know there is data
}
Serial.println("sms sent ");
} //end sendInfo()