如何读取来自Arduino的短信,并让LED指示灯打开或关闭 #包括 焦英寸//将保存来自串行端口的传入字符。 软件串行单元(2,3)//创建一个“假”串行端口。引脚2是Rx引脚,引脚3是Tx引脚。 int-led1=A2; 无效设置() { //准备数字输出引脚 引脚模式(led1,输出); 数字写入(led1,高); //初始化GSM模块串行端口以进行通信。 贝京(19200); 延迟(30000);//给GSM模块在网络上注册的时间,等等。 cell.println(“AT+CMGF=1”);//将SMS模式设置为文本 延迟(200); cell.println(“AT+CNMI=3,3,0,0”);//将模块设置为在收到SMS数据后向串行输出发送SMS数据 延迟(200); } void循环() { //如果某个字符来自蜂窝模块。。。 if(cell.available()>0) { 延迟(10); inchar=cell.read(); 如果(英寸=='a') { 延迟(10); inchar=cell.read(); 如果(英寸=='0') { 数字写入(led1,低电平); } else if(inchar=='1') { 数字写入(led1,高); } 延迟(10); 延迟(10); } cell.println(“AT+CMGD=1,4”);//删除所有SMS } }

如何读取来自Arduino的短信,并让LED指示灯打开或关闭 #包括 焦英寸//将保存来自串行端口的传入字符。 软件串行单元(2,3)//创建一个“假”串行端口。引脚2是Rx引脚,引脚3是Tx引脚。 int-led1=A2; 无效设置() { //准备数字输出引脚 引脚模式(led1,输出); 数字写入(led1,高); //初始化GSM模块串行端口以进行通信。 贝京(19200); 延迟(30000);//给GSM模块在网络上注册的时间,等等。 cell.println(“AT+CMGF=1”);//将SMS模式设置为文本 延迟(200); cell.println(“AT+CNMI=3,3,0,0”);//将模块设置为在收到SMS数据后向串行输出发送SMS数据 延迟(200); } void循环() { //如果某个字符来自蜂窝模块。。。 if(cell.available()>0) { 延迟(10); inchar=cell.read(); 如果(英寸=='a') { 延迟(10); inchar=cell.read(); 如果(英寸=='0') { 数字写入(led1,低电平); } else if(inchar=='1') { 数字写入(led1,高); } 延迟(10); 延迟(10); } cell.println(“AT+CMGD=1,4”);//删除所有SMS } },arduino,gsm,Arduino,Gsm,这是从蜂窝网络接收SMSE的代码。我正在使用Arduino Gboard和SIM900。代码中没有错误,但电路板上的LED不会响应SMS打开或关闭 为什么?在试图解析响应之前,您应该首先确切地知道响应是什么 尝试一些简单的代码(注意:未测试!)来了解您应该寻找什么: void loop() { if(cell.available() > 0) { char ch = cell.read(); Serial.print(ch); } } 我的

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


为什么?

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

尝试一些简单的代码(注意:未测试!)来了解您应该寻找什么:

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

我的猜测是,您将看到的不仅仅是“0”或“1”:)

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

尝试一些简单的代码(注意:未测试!)来了解您应该寻找什么:

void loop() {
    if(cell.available() > 0) {
        char ch = cell.read();
        Serial.print(ch);
    }
}
我猜您将看到的不仅仅是“0”或“1”:)

void loop(){
而(cell.available()>0){inchar=cell.read();readString+=c;delay(1);}///可以是高达(10)的延迟,因此可以获得清晰的读数
Serial.print(readString);///声明字符串“string readString;”
对于(i=0;i
void循环(){
而(cell.available()>0){inchar=cell.read();readString+=c;delay(1);}///可以是高达(10)的延迟,因此可以获得清晰的读数
Serial.print(readString);///声明字符串“string readString;”
对于(i=0;i变化

致:

AT+CNMI=2,2,0,0,0
变化

AT+CNMI=3,3,0,0
致:


AT+CNMI=2,2,0,0,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

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

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

这是一个使用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(10,11);
字符串文本消息;
弦状态;
const int relay=12;//如果使用继电器切换,如果不使用,则反转代码上的所有高电平和低电平
无效设置(){
pinMode(继电器、输出);
digitalWrite(继电器,高);//灯的当前状态为打开
Serial.begin(9600);
GPRS.begin(9600);
延迟(5000);
串行打印(“GPRS就绪…\r\n”);
GPRS.print(“AT+CMGF=1\r\n”);
延迟(1000);
GPRS.print(“AT+CNMI=2,2,0,0,0\r\n”);
延迟(1000);
}
void循环(){
如果(GPRS.available()>0){
textMessage=GPRS.readString();
串行打印(文本消息);
延迟(10);
} 
如果(textMessage.indexOf(“ON”)>=0{//如果发送“ON”,灯将点亮
//打开继电器并保存当前状态
数字写入(继电器,高);
lampState=“ON”;
Serial.println(“灯设置为打开\r\n”);
textMessage=“”;
GPRS.println(“AT+CMGS=\”+631234567890\”;//接收器:在此处使用国际代码更改电话号码
延迟(500);
GPRS.print(“灯终于打开了。\r”);
GPRS.write(0x1a);
延迟(1000);
}
如果(textMessage.indexOf(“OFF”)>=0){
//关闭继电器并保存当前状态
数字写入(继电器,低电平);
lampState=“关”;
Serial.println(“灯设置为关闭\r\n”);
textMessage=“”;
GPRS.println(“AT+CMGS=\”+631234567890\”);///接收器:使用国际代码在此处更改电话号码
延迟(500);
GPRS.print(“灯最终关闭。\r”);
GPRS.write(0x1a);
延迟(1000);
}
如果(textMessage.indexOf(“状态”)>=0){
字符串消息=“灯为”+灯状态;
GPRS.print(“AT+CMGF=1”);
延迟(1000);
Serial.println(“灯状态请求”);
textMessage=“”;
GPRS.println(“AT+CMGS=\”+631234567890\”;//接收器:在此处使用国际代码更改电话号码
延迟(500);
GPRS.print(“灯当前为”);
GPRS.println(lampState?“开”:“关”);//这是用来显示灯当前是打开还是关闭的
GPRS.write(0x1a);
延迟(1000);
}
}

这是一个使用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(10,11);
字符串文本消息;
弦状态;
const int relay=12;//如果使用继电器切换,如果不使用,则反转代码上的所有高电平和低电平
无效设置(){
pinMode(继电器、输出);
digitalWrite(继电器,高);//灯的当前状态为打开
Serial.begin(9600);
GPRS.begin(9600);
延迟(5000);
串行打印(“GPRS就绪…\r\n”);
GPRS.print(“AT+CMGF=1\r\n”);
延迟(1000);
GPRS.print(“AT+CNMI=2,2,0,0,0\r\n”);
延迟(1000);
}
void循环(){
如果(GPRS.available()>0){
textMessage=GPRS.readString();
串行打印(文本消息);
延迟(10);
} 
如果(textMessage.indexOf(“ON”)>=0{//如果发送“ON”,灯将点亮
//打开继电器并保存当前状态
digitalWri