【问题标题】:How can I let Arduino GSM shield respond only to authorized numbers?如何让 Arduino GSM shield 仅响应授权号码?
【发布时间】:2017-06-05 04:03:46
【问题描述】:

我正在做一个项目,我向我的 Arduino GSM Shield 发送“1”以打开连接到引脚 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);


}

【问题讨论】:

  • 为什么要将 1 和 0 的检查放在一个循环中?在我看来,它永远不会退出那个循环。
  • 您在循环()中似乎也有太多右花括号
  • 我使用了 while 循环,这样它只会在 senderNumber 等于 adminNumber 时打开 LED...
  • 你能给我一个示例代码吗?
  • 宁可使用 IF。 loop() 方法无论如何都会循环。您可以将 if (sms.available()) 更改为 while (sms.available())

标签: arduino gsm


【解决方案1】:

可以像 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);

}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多