如何从嵌入式 C 中的 GSM 调制解调器读取消息? [英] How to read messages from GSM modem in Embedded C?
问题描述
我目前正在物联网"域下开展我的迷你项目.我选择使用 GSM 模块设计一个无线布告板.
I am currently working on my mini-project under the domain "Internet of Things". I chose to design a wireless Notice board using a GSM module.
我将项目分为两个模块.一、完美完成的Arduino-LED板卡接口.
I divided the project into two modules. First, the Arduino-LED board interface which perfectly completed.
二、GSM-Arduino接口.基本上,一条消息/短信将从手机发送到 GSM 模块,然后我们必须使用 Arduino 从 GSM 模块读取该消息.我在这里面临一个问题.消息正在发送到 GSM 调制解调器,但我无法读取它.我尝试编写不同的代码,但它不起作用.消息未显示.
Second, GSM-Arduino interface. Basically, a message/SMS will be sent from the mobile phone to the GSM module and then we have to read that message from GSM module using Arduino. I am facing a problem here. The message is being sent to the GSM modem but I am not able to read it. I tried writing different codes, but its not working. The message is not being displayed.
这是我试过的代码片段.
Here is the code snippet I tried.
`#include SoftwareSerial.h
SoftwareSerial SIM900A(2,3);// RX | TX
// Connect the SIM900A TX to Arduino pin 2 RX
// Connect the SIM900A RX to Arduino pin 3 TX.
void setup()
{
SIM900A.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor(Arduino)
Serial.println ("SIM900A Ready");
delay(100);
Serial.println (" Press s to send and r to recieve ");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's': SendMessage();
break;
case 'r': RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial.println ("Set SMS Number");
SIM900A.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); //Replace with your mobileno.
delay(1000);
Serial.println ("Set SMS Content");
SIM900A.println("Hello, I am SIM900A GSM Module");// The SMS text you want to send
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println (" ->SMS Sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Receiving SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
Serial.write (" ->Unread SMS Recieved");
}`
推荐答案
您可能需要使用以下命令将首选短信存储设置为 SIM 卡:
You might have to set the preferred SMS storage to SIM card using the command:
SIM900A.print("AT+CPMS=\"SM\"\r");
此外,将此命令移至 setup():
Also, move this command to setup():
SIM900A.print("AT+CMGF=1\r");
最后,请注意我是如何使用 SIM900A.print() 而不是 SIM900A.println() 并在每个命令后发送 '\r' 或 0x0d 的.println() 发送\n\r",这会导致某些调制解调器出现问题.
Lastly, note how I have used SIM900A.print() instead of SIM900A.println() and send a '\r' or 0x0d after each command. println() sends a "\n\r" and that causes problems in some modems.
这篇关于如何从嵌入式 C 中的 GSM 调制解调器读取消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!