[Iot smart gateway-03] send and receive Chinese text messages in the GPRS module

Source: Internet
Author: User

At the beginning of last year, the GPRS library V1.0.0 was launched. However, in this version, only the GPRS data communication between text message sending and receiving and string transmission is realized, for more information, see my previous blog titled GPRS Communication implementation. In the latest version, the above functions have been greatly upgraded. For example, you can send and receive text messages in PDU mode to support Chinese characters), MMS sending, binary GPRS data sending and receiving, SMS extraction by type, SMS deletion, and SMS event notification.

Considering that there are many functions, we will introduce the functions of the latest GPRS database in three articles. In this article, we will first introduce the content related to text message sending and receiving.

In the library function, the methods related to sending and receiving text messages are as follows:

1) Set the short message center address

Declaration: int SetCenterNum (sring CenterNum)

Parameter: CenterNum-Short Message Center address

Return: Operation Return Value

0-sent successfully

-1-setting failed

Description: Short Message Center address. China Unicom and China Mobile have different numbers in different regions. Generally, no special settings are required.

2) Send SMS PDUMethod)

Declaration: int SendMessage (sring phone, string msg)

Parameter: phone-phone number

Msg-string to be sent

Return: Operation Return Value

0-sent successfully

-1-The character length is 0 or greater than 70 bytes

-2-failed to set the Short Message format

-3-failed to send SMS

Description: send an SMS. Supports sending Chinese characters.

3) Read SMS

Declaration: int ReceiveMessage (out GsmMessage [] msgs, MessageType msgType)

Parameter: msgs-information set. Each item includes the phone number, sending time, and content sent by the SMS.

MsgType-Message Type: unread message, read message, unsent message, sent message, all messages

Return: return the set of SMS strings of the specified type.

Description: receives text messages. Currently, SMS messages can be received in both Chinese and English.

4) Delete SMS

Declaration: int DeleteMessage ()

Parameter: None

Return: Operation Return Value

0-delete 0 messages

-1-failed to set PDU Mode

> 0-Number of deleted Short Messages

Delete all short messages.

 

In addition to the method, there is also a text message to receive the event, the specific statement is as follows:

1) SMS notification

Declaration: SMSMessageReceivedEventHandler MessageReceived

Parameter: object sender-GPRS instance

Notes: SMS notification reception

 

To facilitate the usage of the library, we set a scenario to demonstrate the functions of the relevant code.

650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I4192R6-0.jpg "/>

The scenario is set as follows:

1) The program continuously collects temperature and humidity data. Once the data exceeds the preset range, an SMS notification will be sent to the police.

2) the mobile phone sends text messages, controls the relays, and can remotely turn on and off the humidifier.

The specific code is as follows: the temperature and humidity collection part. For details, see acquisition of temperature and humidity through AD ):

1) GPRS module initialization code

Int ret = 0;

Gprs = new GPRS ("COM3", 115200, GPRS. gprs stype. SIM300_V6_x );

// Open the GPRS module

If (ret = gprs. Open ())! = 0)

{

Debug. Print ("Open Failed:" + ret. ToString ());

Gprs. Close ();

Screen. DrawText ("failed to open GPRS module", font, Colors. Red, 20, 60 );

Screen. Flush ();

Return;

}

Debug. Print ("Open OK ");

Screen. DrawText ("successfully opened the GPRS module", font, Colors. Green, 20, 60 );

Screen. Flush ();

 

// Determine whether the service is online. The timeout value is 10 s.

If (ret = gprs. IsOnline (10000 ))! = 0)

{

Debug. Print ("AT Failed:" + ret. ToString ());

Gprs. Close ();

Screen. DrawText ("GPRS module not online", font, Colors. Red, 20,100 );

Screen. Flush ();

Return;

}

Debug. Print ("at OK ");

Screen. DrawText ("GPRS module online", font, Colors. Green, 20,100 );

Screen. Flush ();

 

// Initialize the GPRS module

If (ret = gprs. Initialize ())! = 0)

{

Debug. Print ("Initialize Failed:" + ret. ToString ());

Gprs. Close ();

Return;

}

Debug. Print ("Initialize OK ");

Screen. DrawText ("GPRS initialization successful", font, Colors. Green, 20,140 );

Screen. Flush ();

 

// Display signal strength

// Debug. Print ("Signal:" + gprs. Signal. ToString ());

Gprs. MessageReceived + = new SMSMessageReceivedEventHandler (gprs s_messagereceived );

2) receive and control SMS messages in the GPRS module

Static void maid (object sender)

{

Int ret = 0;

GsmMessage [] msgs = null;

 

// Read unread Short Messages

If (ret = gprs. ReceiveMessage (out msgs, GPRS. MessageType. REC_UnRead) <0)

{

Debug. Print ("ReceiveMessage Failed:" + ret. ToString ());

}

Else

{

If (ret> 0)

{

Foreach (GsmMessage msg in msgs)

{

Debug. Print (msg. ToString ());

}

// Gprs. SendMessage (msgs [0]. phone, msgs [0]. ToString ());

 

Switch (msgs [0]. message)

{

Case "open ":

OpSwitch. Write (true );

State = true;

Break;

Case "off ":

OpSwitch. Write (false );

State = false;

Break;

}

}

}

// Delete Short Message

// Gprs. DeleteMessage ();

}

3) humidity alarm code

// Humidity alarm

If (H> 60 &&! AlarmFlag)

{

// Fill in the actual phone number when sending an alert message)

Gprs. SendMessage ("158 XXXXXXX", "current humidity:" + H. ToString ("F2 "));

AlarmFlag = true;

}

If (AlarmFlag & H <50)

{

AlarmFlag = false;

}

Actual Operation:

650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I4194625-1.jpg "/>

Note: The hardware platform uses the latest Wisteria 207 system; the GPRS module adopts the Sim300 V6.02 version of the module; the relay module adopts a 5 V Drive and can control 220 V 10A of electrical appliances, generally, small-power household appliances can be used directly. The temperature and humidity module uses a relatively low-cost AD interface module.

 

The next blog post will introduce how the GPRS module sends MMs.

 

Source: http://www.sky-walker.com.cn/MFRelease/Sample/YF_GPSR01.rar

Class Library download: http://www.sky-walker.com.cn/MFRelease/library/v42/YFSoft.Hardware.GPRS.rar

MF Introduction: http://blog.csdn.net/yefanqiu/article/details/5711770

MF data: http://www.sky-walker.com.cn/News.asp? Id = 25

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1I4194625-2.png "/>

This article is from the "ye fan Studio" blog, please be sure to keep this source http://yfsoft.blog.51cto.com/1635641/882120

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.