为什么 Arduino mega 和 SIM900 不会发送消息如果我 input/read 到串行监视器,不同的 phone 数字?
Why does Arduino mega and SIM900 won't send message if I input/read to serial monitor, different phone numbers?
我是 arduino 和 gsm sim900 的新手,我目前正在处理这个项目,当学生扫描他们的条形码时,它会将考勤输入数据库并向他们的监护人发送短信。我的问题是,当我尝试输入 "serial.available" 和 "readString(phonenumber)" 等不同的数字时,它不会发送消息,我没有收到任何错误,而且 sim 卡有 e-load/。抱歉语法不好。这是代码:
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(6,7);
String number;
void setup()
{
SIM900A.begin(9600);
Serial.begin(9600);
Serial.println ("SIM900A Ready");
delay(100);
Serial.print("Enter PHONE NUMBER: ");
while(Serial.available()==0){} //INPUT PHONE NUMBER
number = Serial.readString();
delay(500);
Serial.println ("Type s to send message or r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1");
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.print("AT+CMGS=\""+number+"\"\r"); //send to input phone number
delay(1000);
Serial.print(number);
delay(500);
Serial.println ("Set SMS Content");
SIM900A.print("Message from ArduinoGSMSIM900");
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);
delay(1000);
Serial.println ("Message has been sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0");
delay(1000);
Serial.write ("Unread Message done");
}
请帮助我,在此先感谢!
如果你读过 SoftwareSerial Library documentation,你会注意到它说:
The library has the following known limitations:
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
因此,请将 SoftwareSerial SIM900A(6,7);
上的线路更改为引脚 6 和 7 以外的线路。
好的,经过这么多研究我解决了我的问题。这是为那些和我有同样问题的人准备的,他们为一行简单的代码浪费了很多时间。
当我 serial.readString()
输出将有一个白色 space 就像@hcheung 说的,所以为了摆脱那个白色 space 我使用 number.Trim()
,它将准确读取字符串长度。
arduino GSM SIM900 的最终草图:
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(10,11);
String number;
String message;
void setup()
{
SIM900A.begin(9600);
Serial.begin(9600);
Serial.println ("SIM900A Ready");
delay(100);
Serial.print("Enter PHONE NUMBER: ");
while(Serial.available()==0){}
number = Serial.readString();
delay(100);
number.trim();
delay(200);
Serial.println(number.length());
Serial.print("Enter MESSAGE: ");
while(Serial.available()==0){}
message = Serial.readString();
delay(100);
Serial.println ("Type s to send message or r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1");
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.print("AT+CMGS=\""+number+"\"\r");
delay(1000);
Serial.print(number);
delay(500);
Serial.println ("Set SMS Content");
SIM900A.print(message);
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);
delay(1000);
Serial.println ("Message has been sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0");
delay(1000);
Serial.write ("Unread Message done");
}
我是 arduino 和 gsm sim900 的新手,我目前正在处理这个项目,当学生扫描他们的条形码时,它会将考勤输入数据库并向他们的监护人发送短信。我的问题是,当我尝试输入 "serial.available" 和 "readString(phonenumber)" 等不同的数字时,它不会发送消息,我没有收到任何错误,而且 sim 卡有 e-load/。抱歉语法不好。这是代码:
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(6,7);
String number;
void setup()
{
SIM900A.begin(9600);
Serial.begin(9600);
Serial.println ("SIM900A Ready");
delay(100);
Serial.print("Enter PHONE NUMBER: ");
while(Serial.available()==0){} //INPUT PHONE NUMBER
number = Serial.readString();
delay(500);
Serial.println ("Type s to send message or r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1");
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.print("AT+CMGS=\""+number+"\"\r"); //send to input phone number
delay(1000);
Serial.print(number);
delay(500);
Serial.println ("Set SMS Content");
SIM900A.print("Message from ArduinoGSMSIM900");
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);
delay(1000);
Serial.println ("Message has been sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0");
delay(1000);
Serial.write ("Unread Message done");
}
请帮助我,在此先感谢!
如果你读过 SoftwareSerial Library documentation,你会注意到它说:
The library has the following known limitations:
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
因此,请将 SoftwareSerial SIM900A(6,7);
上的线路更改为引脚 6 和 7 以外的线路。
好的,经过这么多研究我解决了我的问题。这是为那些和我有同样问题的人准备的,他们为一行简单的代码浪费了很多时间。
当我 serial.readString()
输出将有一个白色 space 就像@hcheung 说的,所以为了摆脱那个白色 space 我使用 number.Trim()
,它将准确读取字符串长度。
arduino GSM SIM900 的最终草图:
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(10,11);
String number;
String message;
void setup()
{
SIM900A.begin(9600);
Serial.begin(9600);
Serial.println ("SIM900A Ready");
delay(100);
Serial.print("Enter PHONE NUMBER: ");
while(Serial.available()==0){}
number = Serial.readString();
delay(100);
number.trim();
delay(200);
Serial.println(number.length());
Serial.print("Enter MESSAGE: ");
while(Serial.available()==0){}
message = Serial.readString();
delay(100);
Serial.println ("Type s to send message or r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1");
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.print("AT+CMGS=\""+number+"\"\r");
delay(1000);
Serial.print(number);
delay(500);
Serial.println ("Set SMS Content");
SIM900A.print(message);
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);
delay(1000);
Serial.println ("Message has been sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0");
delay(1000);
Serial.write ("Unread Message done");
}