我正在开发一个项目,我将'1'发送到我的Arduino GSM Shield以打开连接到引脚13的LED,我通过发送'0'将其关闭,但我想按顺序修改代码用于GSM Shield仅响应指定的数量。这是我的代码,在我的例子中,我使用了一个示例电话号码。目前,当我从adminNumber发送消息时,LED不会打开也不会关闭。
// include the GSM library
#include <GSM.h>
// PIN Number for the SIM
#define PINNUMBER ""
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
// Array to hold the number a SMS is retreived from
char senderNumber[20];
char adminNumber[20]="+123456789234";
void setup()
{
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("SMS Messages Receiver");
// connection state
boolean notConnected = true;
// Start GSM connection
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop()
{
char c;
sms.remoteNumber(senderNumber, 20);
sms.remoteNumber(adminNumber, 20);
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// Any number besides admin number will not change the state of the pin
while(adminNumber == senderNumber){
// Any messages starting with 1 will set pin 13 to HIGH
if (sms.peek() == '1'){
digitalWrite(13, HIGH);
sms.flush();
}
// Any messages starting with 0 will set pin 13 to LOW
else if(sms.peek() == '0'){
digitalWrite(13, LOW);
sms.flush()
}
}
// Read message bytes and print them
while (c = sms.read())
Serial.print(c);
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}
答案 0 :(得分:0)
阵列比较可以像Kurz7那样https://cboard.cprogramming.com/c-programming/41972-how-do-i-compare-two-arrays.html
// include the GSM library
#include <GSM.h>
#include <string.h>
// PIN Number for the SIM
#define PINNUMBER "xxxx"
// initialize the library instances
GSM gsmAccess;
GSM_SMS sms;
// Array to hold the number a SMS is retreived from
char senderNumber[20];
char adminNumber[20]="+xxxxxxxxxxx";
void setup()
{
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
// initialize serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("SMS Messages Receiver");
// connection state
boolean notConnected = true;
// Start GSM connection
while (notConnected)
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
}
void loop()
{
char c;
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
sms.remoteNumber(senderNumber,20); // Get remote number
Serial.println(senderNumber);
// Any number besides admin number will not change the state of the pin
if (strcmp( adminNumber, senderNumber ) == 0 )
{
// Any messages starting with 1 will set pin 13 to HIGH
if (sms.peek() == '1') {digitalWrite(13, HIGH);
}
// Any messages starting with 0 will set pin 13 to LOW
else if(sms.peek() == '0') {digitalWrite(13, LOW);
}
// Read message bytes and print them
while (c = sms.read())
Serial.print(c);
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}}