如何从Arduino读取短信并让LED打开或关闭

时间:2013-04-09 07:36:58

标签: arduino gsm

#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
     }
 }

这是从蜂窝网络接收短信的代码。我正在使用带有SIM900的Arduino Gboard。代码中没有错误,但是电路板上的LED不响应SMS而打开或关闭。

为什么?

5 个答案:

答案 0 :(得分:1)

更改

AT+CNMI=3,3,0,0

为:

AT+CNMI=2,2,0,0,0

答案 1 :(得分: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);
  }
}

答案 2 :(得分:0)

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

尝试下面的代码(注意:未经测试!),以了解您应该寻找的内容:

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

我的猜测是你会看到的不仅仅是'0'或'1':)

答案 3 :(得分:0)

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;

      } 
    }   
}

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

答案 4 :(得分:0)

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

// 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