Design of wireless communication module based on STM32 and SIM900A

Design a module that can realize the sending and receiving of short messages and wireless data transmission. This article uses the mainstream product STM32 of the ARM Cortex-M3 core as the main control chip and SIM900A from SIMCom as the communication chip. After consulting a large number of related documents and data manuals of related chips, this article designs a remote wireless communication module. After the module was tested in the laboratory for a week, there was no disconnection, and the speed of sending and receiving data was also very fast. The module has the advantages of stable performance, small size and high cost performance. After the factory was put into use, the response was good.

Design a module that can realize the sending and receiving of short messages and wireless data transmission. This article uses the mainstream product STM32 of the ARM Cortex-M3 core as the main control chip and SIM900A from SIMCom as the communication chip. After consulting a large number of related documents and data manuals of related chips, this article designs a remote wireless communication module. After the module was tested in the laboratory for a week, there was no disconnection, and the speed of sending and receiving data was also very fast. The module has the advantages of stable performance, small size and high cost performance. After the factory was put into use, the response was good.

With the continuous development of networks and modern communication technologies, long-distance wireless communication technology has now taken a very important position in the field of industrial control after years of research and practical application, and it is playing an increasingly important role. According to the requirements of the manufacturer, the wireless communication module designed in this article mainly realizes the functions of sending and receiving text messages and data, and achieves the stability of the module, which can automatically reconnect after the disconnection.

1 Overall design scheme of wireless communication module

The module is mainly composed of 4 parts: power supply part, main control part, communication part and data transmission part. The communication module uses SIM900A for wireless communication. The main control module uses STM32 as the main control chip to control the sending and receiving of short messages and the smooth progress of data transmission. The remote terminal can be the data center software of a mobile phone or an upper computer, which is processed and stored to facilitate future queries. This module has low power consumption, convenient and flexible, simple and stable operation, can be automatically connected after disconnection, the operation process is very stable, and the cost is low.

2 wireless communication module hardware design

2.1 Functional requirements

The wireless communication module designed in this article requires a single 12V power input. After the module is powered on, there is a corresponding indicator light to indicate the running status of the module. In addition, the module is required to be able to realize the two main functions of short message and data transparent transmission. After the mobile phone sends the short message to the module, the module can act and give a response. Finally, it is required to still be able to send and receive short messages when establishing a TCP link.

Design of wireless communication module based on STM32 and SIM900A
Figure 1 Structure diagram of wireless communication module

2.2 Power section

In the main circuit, the operating voltage of the main control chip STM32 is 2.0~3.6 V, and the operating voltage of the communication chip SIM900A is 3.1~4.6 V. In order to make each part of the module work normally, the two must be powered separately. The power supply circuit is as follows :

In the picture, V12 is connected to an external 12 V power supply, filtered by a capacitor, and then input to LM2576 to realize the conversion from 12 V to 4 V. R1 and R2 play a role in dividing the voltage in the circuit. When the D2 light is on, it means that the module has been powered normally. LM2576 is a 3 A current output step-down switch integrated voltage regulator circuit produced by National semiconductor, with a complete protection circuit and relatively stable.

Design of wireless communication module based on STM32 and SIM900A
Figure 2 Power module

2.3 Main control chip

The main control module adopts STM32 single-chip microcomputer as the microcontroller. The chip can work in the temperature range of -40~105℃. The MAX3232 chip is used for the level conversion of the serial port to realize the communication between the controller and the communication interface. Serial port 1 is connected to the power level conversion chip Max3223, USART1_TX (output, so when configuring GPIO, define the port mode as push-pull output, USART1_RX is input, defined as floating input mode. Serial port 2 communicates with SN65LBC184D to realize data Transceiver, USART2_RTS, USART2_RX are input ports, the mode is defined as floating input mode, USART2_CTS, USART2_TX are output ports, and the mode is defined as push-pull output. Serial port 3 is used to control the SIM900A chip, USART3_RTS, USART3_RX are input ports, and the mode is defined as floating input. USART3_CTS and USART3_TX are output ports, and the mode is defined as push-pull output.

Design of wireless communication module based on STM32 and SIM900A
Figure 3 Main control chip STM32

2.4 Communication chip

The communication chip adopts SIMCom’s new compact product SIM900A, which is a dual-band GSM/GPRS module, fully adopts SMT packaging, stable performance, exquisite appearance, high cost performance, and can meet various needs of users. When realizing the automatic reconnection function after disconnection, it involves the use of DCD and RI two pins. The DCD pin is used to implement analog-to-digital conversion. When the module is disconnected, it will give the DCD pin a high level. When this level is detected by the DCD pin, the module will take corresponding actions to reconnect to the line. After the module goes online, the RI pin remains high. When a call or text message comes in, the RI pin will have a low level. When the RI pin detects this low level, the module Corresponding actions will be taken to enter SMS or phone mode.

Design of wireless communication module based on STM32 and SIM900A
Figure 4 SIM900A communication chip

3 Software design of wireless communication module

3.1 Low-level configuration of STM32

In order to realize the data communication between the STM32 single-chip microcomputer and the SIM900A module, the two major functions of SMS sending and receiving and data transmission are realized. First, we must build a development platform, add the library functions and configuration files that need to be used in the project, and then configure the system clock, interrupt controller, input and output GPIO, and the corresponding serial port. When configuring these parameters, you first need to compile with the schematic diagram, and then check which ports the chip uses and the functions of these ports, so as to ensure that they are correct. The next step is to configure each part to ensure the normal operation of the module.

3.1.1 Serial port configuration

After the development environment is set up, the port parameters can be configured. For this module, set the USART transmission bit rate to 9 600 b/s, the word length to be 8 bits, 1 bit stop bit, and no inspection mode. After initializing the serial ports 1, 2, and 3, open the interrupt response function of the serial port: USART_ITConfig (USART1, USART_IT_RXNE, ENABLE) (take the serial port 1 as an example), and enable the corresponding serial port: USART_Cmd (USART1, ENABLE). The configuration is basically complete.

3.1.2 Interrupt controller configuration

First configure the priority grouping, set the preemptive priority to 1 bit and the secondary priority to 3 bits. This module defines 5 global interrupts, which are: two RTC global interrupts, USART1 global interrupt, USART2 global interrupt, and USART3 global interrupt. Configure the priority for each interrupt, and then enable the serial port to initialize. It should be noted that the PC15 pin is used as the external interrupt input pin of EXTI15. It is triggered when the level of this pin is low, and the module enters the SMS mode, so this interrupt is given a higher priority, so it is defined This interrupt takes priority 1 bit first, followed by priority 0 bit. When setting the priority, you must select the optimal interrupt grouping and priority according to the running status of the module to ensure that the program can quickly respond to interrupts when the program is running.

3.2 The realization of the short message function of the wireless communication module

1) The specific process of sending and receiving SMS

The realization of the short message function mainly involves two key parts, one is the AT command, and the other is the serial port reading and writing. AT command is the communication protocol between the main control chip STM32 and SIM900A, which completes the control of SIM900A. There are two main modes for sending and receiving short messages, one is text mode, which only supports English content, and the other is PDU mode. After this module is initialized, it first sends an “AT+Enter” command to SIM900A via USART to check whether the AT command is working properly. If it returns OK, it means that the next SMS function can be performed.

The SMS function of SIM900A is mainly divided into two parts. First: read short messages. The setting command to read the short message is: AT+CMGR. After the command takes effect, there are two return values: index and mode. The index is the number of the received short message. We put the content of the received index in a length of 30. In buf, write buf to serial port 3, and then read the content in serial port 3. In this way, the number of the short message is obtained. After obtaining the number of the short message, you can get the specific content of the short message, and the content of the short message is stored in the alpha, which is in the third position of the response, and the short message can be obtained through get_fw (at_string, phnum, 19, 2) The content is taken out so that the content of the short message can be obtained. Second: Send short messages. The setting command for sending short messages is: AT+CMGS. First, store the phone number for sending the short message into a buf, and write this buf into the serial port 3 to obtain the mobile phone number. When the short message is sent, the content of the short message will be accompanied by a “>” sign. During the programming, as long as the “>” sign is detected, the following content is the content sent by the short message, and finally the content of the short message is written to the serial port 3 is fine.

2) Corresponding AT command

Design of wireless communication module based on STM32 and SIM900A

3.3 Definition of SMS command

This module has customized many short message commands to set or query the parameters of the module, specifically: SIP: IP address setting command, SPT: port number setting command, DID: ID number setting command, ACON: automatic connection command, PRT: print Command, LIVE: heartbeat operation command, CSQ: signal strength query command. These instructions realize the operation of the module by means of short messages.

Take SIP as an example. Its implementation process is: first take out the characters before the’=’ in the SMS and store them in sms_cmd, then compare sms_cmd with SIP, SIT, DID, ACON, PRT, LIVE, CSQ, if Equal to one of them, the program jumps to the corresponding part to perform the corresponding operation. Here, strcmp (sms_cmd, “SIP”) == 0, the program jumps to the SIP part to perform the operation of the IP part. Then, take out the characters after’=’ and store them in sms_para. Use the string comparison function to determine whether it is a’?’ or a number. If it is’?’, it means that the SMS command is a query command, and you only need to use the output to Display the IP number to the user. If it is a number, it means that the SMS command is a setting command, and some parameter values ​​need to be reset to change the running process of the module. For SIP, if there is a number after the’=’, it means that the short message is used by the user to set the IP address of the module. At this point, it is enough to assign the current IP value to the IP value of the module. The final output shows that the IP address is set successfully, and the SPT part of the operation is completed.

4 Realization of data transmission of wireless communication module

4.1 Definition of data format

The specific format of the data packet is as follows:

The data packet header has been defined as S_PACKET_HEADER structure type, link_id is DWORD type, for sending, fill in the target link_id, for the received data packet, it is the source link_id (automatically converted and filled by the server), the end flag is 0x01, and the data is based on the specific The commands vary.

Design of wireless communication module based on STM32 and SIM900A

4.2 Agreement process of login and logout

The background software initiates a TCP connection request according to the specified server address and port number, and starts command interaction after the connection is successful. The working process is described as follows:

1) The background software first uses the CMD_LOGIN command to log in to the communication server. Before the login is successful, the server will not process any other commands sent. Send CMD_LOGIN:FORWARD to the server, carrying data as S_TERMINAL. The target link_id of this command is set to 0, which means it is sent to the server, not to other terminals.

2) The server sends the information of other online modules in CMD_LOGIN:RESPONSE_OK to form an online module list in the client application. If the background receives a CMD_LO GIN:;RESPONSE_FAILED response, it will fail. After receiving the CMD_LOGIN:RESPONSE_OK response, it is successful, and the data carried is S_TERMINAL.

3) The background software sends the CMD_KEEP_LIVE command regularly to keep the link from being terminated by the network and the server. Each online device must send CMD_KEEP_LIVE information to the server within a certain time interval. The server will check this. If a device does not send CMD_KEEP_LIVE information within a certain time interval, it is considered that the device has been “dead” and the server will disconnect its connection.

4) The background software can control any module in the module list.

5) When the background software exits, the user equipment should first send the CMD_LOGOFF command to the server to tell the server “I want to log out”, and the target link_id of this command is set to 0. When sending CMD _LOGOFF:FORWARD to the server, there is no need to carry data.

Design of wireless communication module based on STM32 and SIM900A
Figure 5 Agreement process of login and offline

4.3 The specific process of data transmission

First, define the functions for establishing and closing TCP connections. The AT commands involved are AT+CIPSTART and AT+CIPCLOSE. When establishing a TCP connection, first write the AT+CIPSTART command to the serial port 3, and then after the command takes effect, A “CONNECT” will be returned, and then as long as the return value “CONNECT” is detected, it means that the TCP connection has been established successfully. Closing a TCP connection is similar to establishing a TCP connection. After the AT+CIPCLOSE command takes effect, a “CLOSE OK” will be returned. As long as the “CLOSE OK” is detected, it means that the TCP connection has been closed.

After the TCP connection is established, the transparent transmission mode has been turned on, and the data can be sent and read at this time.

When the user wants to send data, he must send the data according to the structure of the data packet. First, write the header to the serial port 3. If there is data, write the data content and the receiving object to the serial port 3 together, so that the data The sending is complete.

When reading data, as long as there is data coming, one byte at a time, the data is stored in user_string[i]In, then i++, receive data again.

As for the end of the data reading, this program defines three end conditions: 1) If the user defines the length of the data, the end will be read until the last byte. 2) If the user does not define the length of the data, it ends when the carriage return character is read. 3) If there is neither a defined data packet length nor a carriage return character, the length of the data storage exceeds user_string[i]It ends when three-quarters of the total length, or ends when no data is sent for more than 100 milliseconds.

4.4 Corresponding AT commands

Design of wireless communication module based on STM32 and SIM900A

5 Handling of disconnection and reconnection

5.1 Closing and establishing of TCP link

The AT command involved in the establishment of a TCP link is at+cipstart, which has two return values, which are the IP address and port number of the module. First, store the IP address and port number of the current module returned by the at+cipstart instruction in a buf, and then write the buf into the serial port 3. If the return value “CONNECT” can be read later, it means that the TCP connection has been completed. Established.

The AT command involved in closing the TCP link is at+cipclose. This command has no return value. You can write the command directly to the serial port 3. If “CLOSE OK” is detected, it means that the TCP link has been closed.

5.2 DCD detection

When the TCP is established, the voltage value of the DCD pin changes from high to low, so the level state of the DCD pin can be used to indicate the connection of the TCP. In the program, this article sets the clock to detect the status value of the DCD pin. When the level value of the pin changes from high to low, it means that the module has been disconnected, and then the module restarts to log in until Until landing.

5.3 SMS sending and receiving under TCP link

This article defines a global interrupt. When a text message or a call arrives at the module, the level of the RI pin will change from high to low. At this time, the global interrupt will be triggered, and the module will immediately switch to sending and receiving text messages or data transmission.

6 Test results

After the design of this module was completed, it ran stably in the laboratory for 2 weeks without any problems. Then a special case test was carried out. After the module was powered off, the power supply was restored, and the data center was reconnected after the power was off, and it can continue to operate stably. The module was then sent to the factory for use. After being used for 3 weeks, the factory responded that the module was in good condition and there was no disconnection.

7 Conclusion

The wireless communication module designed in this article uses STM32 to control the SIM900A chip to realize the sending and receiving of short messages and the wireless transmission of data. This design completes the design and implementation of the hardware and software parts of wireless communication. After running the test several times, the module did not have any problems such as disconnection and heating, and it was very stable. The cost of this design is low, the operation is stable and reliable, and the application range is very wide. The use of SIM900A reduces the cost of the module and makes it more commercially valuable.

The Links:   FF600R12ME4 CM150DY-34A

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *