我的项目有一个奇怪的问题。我能够发送和接收 SMS 消息,但由于某种原因不是来自同一个项目。
这个小家伙发送了一条消息,并且工作正常:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+38762701893";
String message = "this is some message";
void setup() {
SIM900.begin(19200);
delay(20000);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop() {
sendSMS(no, message);
do {} while (1);
}
但是,如果我想让这个相同的功能在一个接收 SMS 消息并响应它们的更大的程序下工作,它就不起作用。
这是不工作示例的完整代码:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+122333444"; //phone number
String message = "";
char inchar;
int b = 13;
bool state = 0;
void setup()
{
Serial.begin(19200);
SIM900.begin(19200);
delay(5000);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
Serial.println("Ready...");
SIM900.begin(19200);
delay(500);
pinMode(b, OUTPUT);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(200);
SIM900.println("AT+CMGS=\"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(8000);
}
void loop()
{
if (state) {
sendSMS(no, message);
state = 0;
}
if (SIM900.available() > 0) {
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '#') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == 'a') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '0') {
digitalWrite(b, LOW);
message = "something is off";
} else if (inchar == '1') {
digitalWrite(b, HIGH);
message = "something is on";
}
}
state = 1;
}
}
}
该程序根据短信内容接收短信,打开或关闭LED,但由于某种原因它不会回复。任何帮助是极大的赞赏。