【问题标题】:How to read an SMS from Arduino and get LED to switch on or off如何从 Arduino 读取 SMS 并让 LED 打开或关闭
【发布时间】:2013-04-09 07:36:58
【问题描述】:
#include <SoftwareSerial.h>
char inchar; //Will hold the incoming character from the serial port.
SoftwareSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.

 int led1 = A2;

 void setup()
 {
     // Prepare the digital output pins
     pinMode(led1, OUTPUT);
     digitalWrite(led1, HIGH);

     //Initialize GSM module serial port for communication.
     cell.begin(19200);
     delay(30000); // Give time for GSM module to register on network, etc.
     cell.println("AT+CMGF=1"); // Set SMS mode to text
     delay(200);
     cell.println("AT+CNMI=3,3,0,0"); // Set module to send SMS data to serial out upon receipt 
     delay(200);
 }

 void loop() 
 {
     //If a character comes in from the cellular module...
     if(cell.available() >0)
     {
         delay(10);
         inchar=cell.read(); 
         if (inchar=='a')
         {
             delay(10);
             inchar=cell.read();
             if (inchar=='0')
             {
                 digitalWrite(led1, LOW);
             } 
             else if (inchar=='1')
             {
                 digitalWrite(led1, HIGH);
             }
             delay(10);
             delay(10);
         }
         cell.println("AT+CMGD=1,4"); // Delete all SMS
     }
 }

这是用于从蜂窝网络接收 SMS 的代码。我正在使用带有 SIM900 的 Arduino Gboard。代码没有错误,但是板上的 LED 没有响应 SMS 的开关。

为什么?

【问题讨论】:

  • 你能添加一个问题,带问号吗?目前尚不清楚您的问题是什么。 “代码中没有错误”到底是什么意思?您是否正确接收短信?
  • 不,先生,我的代码中没有 eroor。
  • 那么是板上的LED灯不亮吗?
  • 不,先生,我的代码中没有 eroor。问题:,我想从蜂窝网络(移动)打开或关闭 jst ......就像 #a0 或 #a1 一样将 jst 短信发送到 arduino,根据我的程序,led 是打开或关闭的,也没有问题上传但 led 没有给出响应....我的程序运行到初始级别意味着当我定义最初 led 低或 led 高但随后逻辑不遵循........所以你能告诉我有没有波特率问题??我用 19200 开始细胞...所以请尝试帮助我...thanxx
  • 是的,没有 LED 闪烁....仅在开始时将 LED 定义为低或高,它可以显示,但随后逻辑 nt 应用..所以如果你有任何想法......thn请告诉我

标签: arduino gsm


【解决方案1】:

这是一个功能齐全的代码,用于使用 Arduino 和 GSM 通过 SMS 发送命令,它还会回复灯的状态。

#include <SoftwareSerial.h>
SoftwareSerial GPRS(10, 11);
String textMessage;
String lampState;
const int relay = 12; //If you're using a relay to switch, if not reverse all HIGH and LOW on the code

void setup() {  
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH); // The current state of the light is ON

  Serial.begin(9600); 
  GPRS.begin(9600);
  delay(5000);
  Serial.print("GPRS ready...\r\n");
  GPRS.print("AT+CMGF=1\r\n"); 
  delay(1000);
  GPRS.print("AT+CNMI=2,2,0,0,0\r\n");
  delay(1000);
}

void loop(){
  if(GPRS.available()>0){
    textMessage = GPRS.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("ON")>=0){ //If you sent "ON" the lights will turn on
    // Turn on relay and save current state
    digitalWrite(relay, HIGH);
    lampState = "ON";
    Serial.println("Lamp set to ON\r\n");  
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
    delay(500);
    GPRS.print("Lamp was finally switched ON.\r");
    GPRS.write( 0x1a );
    delay(1000);
  }
  if(textMessage.indexOf("OFF")>=0){
    // Turn off relay and save current state
    digitalWrite(relay, LOW);
    lampState = "OFF"; 
    Serial.println("Lamp set to OFF\r\n");
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\""); /// RECEIVER: change the phone number here with international code
    delay(500);
    GPRS.print("Lamp was finally switched OFF.\r");
    GPRS.write( 0x1a );
    delay(1000);
  }
  if(textMessage.indexOf("STATUS")>=0){
    String message = "Lamp is " + lampState;
    GPRS.print("AT+CMGF=1"); 
    delay(1000);
    Serial.println("Lamp state resquest");
    textMessage = "";
    GPRS.println("AT+CMGS=\"+631234567890\""); // RECEIVER: change the phone number here with international code
    delay(500);
    GPRS.print("Lamp is currently ");
    GPRS.println(lampState ? "ON" : "OFF"); // This is to show if the light is currently switched on or off
    GPRS.write( 0x1a );
    delay(1000);
  }
}

【讨论】:

  • 嗨,GPRS.print("AT+CMGF=1\r\n");GPRS.print("AT+CMGF=1\r"); 你为什么使用 \n,有必要吗。
【解决方案2】:

改变

AT+CNMI=3,3,0,0

到:

AT+CNMI=<b>2,2,0,0,0</b>

【讨论】:

    【解决方案3】:

    最简单的方法就是最好的方法。

    // if You use SoftwareSerial lib, declare object for GSM
    SoftwareSerial gsm(8,9); // TX, RX
    void setup(){
         // initialise UART and GSM communication between Arduino and modem
         Serial.begin(115200);
         gsm.begin(115200);
         // wait 5-10sec. for modem whitch must connect to the network
         delay(5000);
         // configure modem - remember! modem didn't remeber Your's configuration!
         gsm.print("at+cmgf=1\r"); // use full functionality (calls, sms, gprs) - see app note
         gsm.print("at+clip=1\r"); // enable presentation number
         gsm.print("at+cscs=\"GSM\"\r"); // configure sms as standard text messages
         gsm.print("at+cnmi=1,2,0,0,0\r"); // show received sms and store in sim (probobly, I don't compre this settings with app note but it's working :)
    }
    void loop(){
         String response = gsmAnswer();
        if(response.indexOf("+CMT:") > 0 ) { // SMS arrived
        // Now You can parse Your Message, if You wont controll only LED, just write
           if(response.indexOf("LED ON") > 0) {
              digitalWrite(LED_PIN, HIGH); // enable it
           }else if(response.indexOf("LED OFF") > 0) {
              digitalWrite(LED_PIN, LOW); // turn off
           }
           delay(1000);
        }
    }
    
    
    String gsmAnswer(){
       String answer;
       while(!gsm.available());
       while(gsm.available()){
         delay(5);
         if(Serial.available() > 0){
           char s = (char)gsm.read();
           answer += s;
         }
      }
      return answer;
    }
    

    再想一想,收到的短信格式如下:

    +CMT: "+48xxxxxxxxx","","17/07/07,21:57:04+08"
    Test of arrived messages
    

    【讨论】:

      【解决方案4】:

      在尝试解析响应之前,您应该首先确切地知道响应是什么。

      尝试以下代码之类的简单代码(注意:未经测试!)以了解您应该寻找什么:

      void loop() {
          if(cell.available() > 0) {
              char ch = cell.read();
              Serial.print(ch);
          }
      }
      

      我猜你会看到的不仅仅是“0”或“1”:)

      【讨论】:

        【解决方案5】:
        void loop() {
            while(cell.available() > 0) {inchar = cell.read(); readString+=c;delay(1);} ///can be a delay up to (10) so you can get a clear reading
            Serial.print(readString);  /// Declare a string    " String readString; "
            for (i=0; i<200; i++){  /// Serch for the txt you sent up to (200) times it depends how long your ""readString" is
              if(readString.substring(i,i+4)=="RING"){ //// I am looking for the word RING sent from my phone
                digitalWrite(13,HIGH);
                break;
        
              } 
            }   
        }
        

        这将帮助您找到您阅读的特定单词(短信)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多