当前位置:网站首页>SMS sending + serial port printing based on stm32f103-sim900a
SMS sending + serial port printing based on stm32f103-sim900a
2022-06-13 11:51:00 【Pica Churchill】
STM32F103-SIM900A texting + Serial printing
Recently, with STM32F103 Make a smart door lock gadget , If the password is entered incorrectly more times, unlock the verification code , Here I use SIM900A To unlock by sending a text message and entering a verification code . After a brief understanding, I also write this separately here SIM900A A note of .
Basic introduction
...... Let's go straight to the topic
One : Individual test
SIM900A Yes. AT Command operated , So I usually get this module to test whether it is normal . Through one USB-TTL and SIM900A Connect debug . Use our computer serial port assistant to test . There is no need to use a single-chip computer , Now we have tested it separately . No problem. It's not too late to turn it into code .
1. preparation
USB-TTL One
And DuPont line, needless to say
2. connection
USB-TTL | SIM900A |
---|---|
5V | VCC5 |
GND | GND |
TX | 5VR |
RX | 5VT |
3. Command operation
Open your serial port assistant
Be careful : It is recommended to use the serial port assistant provided by the seller
Be careful : It is recommended to use the serial port assistant provided by the seller
Be careful : It is recommended to use the serial port assistant provided by the seller
First step : send out AT Check whether it is normal ( Remember to enter and send again ) Successfully returns OK
AT
The second step : Select the SMS format (0:PDU Pattern 1: Text mode ) ( Remember to enter and send again ) Successfully returns OK
AT+CMGF=1
The third step : choice TE Font set ( Remember to enter and send again ) Successfully returns OK
AT+CSCS="UCS2"
Step four : preservation SMS Set up ( Remember to enter and send again ) Successfully returns OK
AT+CSCA?
Step five : Set SMS text mode parameters ( Remember to enter and send again ) Successfully returns OK
AT+CSMP=17,167,0,25
** Step six : Set the phone number of the receiving party ( To turn UNICON code ) ( Remember to enter and send again ) Successfully returns >** There is a code generated .
AT+CMGS=" The phone number of the party you receive the text message UNICON code "
Step seven : Send what you want to send ( To turn UNICON code )( Remember to enter and send again ) Successfully returns >
" Of the content sent UNICON code "
Step eight : Send end flag 16 Base number :0X1A Switch to 16 Send in hexadecimal mode ( You don't need to return to a new line ) Successfully returns OK
1A
Tips :
1. Not surprisingly, if you set the mobile phone number to receive the SMS, you can receive the content of the SMS you send
2. Why is it best to use the serial port assistant provided by the seller Because I didn't use the seller's at the beginning , I use other , Changed 2 All of them send the last 1A Back to error instead of OK. Later, it was successful to use what they provided .
3. About UNICON Code conversion software Generally, the seller will provide information Here's the picture
It doesn't matter if you don't Or don't bother to ask me to write one by myself I also write here Please forget to look But here I am ASCII turn Unicode If it's Chinese, you can write it yourself Or you'd better turn to the software Ha ha ha ha
4. test result
Two : About Unicode code
Unicode Basic introduction
Unicode Generally refers to unified code , It's also called the universal code 、 Single code , It's an industry standard in the field of computer science , Include character set 、 Coding scheme, etc .Unicode It is to solve the limitation of traditional character coding scheme , It sets a uniform and unique binary encoding for each character in each language , To meet cross language needs 、 Cross platform text conversion 、 Handling requirements .
1、 Encoding mode
unicode It is a character coding scheme developed by international organizations that can accommodate all characters and symbols .Unicode Use numbers 0-0x10FFFF To map these characters , Maximum capacity 1114112 Characters , Or there is 1114112 A code bit . A code point is a number that can be assigned to a character .UTF-8、UTF-16、UTF-32 They are all coding schemes that convert numbers to program data .
We look at the picture I'm just here ASCII turn … Patchy look
We put ASCII The string is converted into a double byte Chinese character unicode Code you find 1 This character becomes 0031
this 31 Did you think of something ,1 Of 16 No, it's just 0X31 Do you ? Front complement 2 individual 0 The back is its 16 It's over ?
/********************************************************************** describe : ASCII turn unicode such as '1' Turn into "0031" ***********************************************************************/
void ASCII_TO_Unicode(char *ASCII,char *Unicode)
{
int length;
int i = 0;
int j = 0;
memset(Unicode,'\0',sizeof(Unicode));
length = strlen(ASCII);
for(i=0;i<length;i++)
{
Unicode[j++] = '0';
Unicode[j++] = '0';
Unicode[j++] = (ASCII[i] / 16) + 0x30;
Unicode[j++] = (ASCII[i] % 16) + 0x30;
}
}
For example, I would like to transfer the phone number of the receiving party
3、 ... and : Code writing
With the test just now , We can turn it into code use STM32F103 To control SIM900A I sent a text message .
I don't want to add a screen here . Directly use the serial port to print information to view .
Use resources : Two serial ports
A serial port 1 For printing information ( Act as a display )
A serial port 2 Used for and SIM900A signal communication .
sim900a.c
#include "sim900a.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "usart.h"
#include "delay.h"
uint8_t verification_code[7]; // Store verification code
uint8_t sim900a_receive_data[128] = {
0};
uint16_t sim900a_receive_count;
/********************************************************************** describe : SIM900A The module sends instructions function Parameters : *str: Instruction content ack: The data that normally returns the result time: Allowable time (1 Secondary representative 10ms) return : 0: The instruction was correctly answered 1: The instruction was not answered ***********************************************************************/
uint8_t sim900a_send_cmd(uint8_t *str,uint8_t *ack,uint16_t time)
{
memset(sim900a_receive_data,'\0',sizeof(sim900a_receive_data));
sim900a_receive_count = 0;
usart_sendString(USART2,(char *)str); // Send instructions
if(ack == NULL) return 0;
// Enable interrupt reception
OPEN_USART2_RECEIVE;
while(time--)
{
delay_ms(10);
// Look for the key words you are looking for Find the switch and disconnect the receiver And back to 0
if(strstr((char *)sim900a_receive_data,(char *)ack) != NULL)
{
CLOSE_USART2_RECEIVE; // Turn off interrupt reception
return 0;
}
}
CLOSE_USART2_RECEIVE; // Turn off interrupt reception
return 1; // Timeout has not received the keyword you want Turn off the serial port interrupt And back to 1
}
/********************************************************************** describe : SIM900A The module sends instructions function Parameters : *str: Instruction content ack: The data that normally returns the result time: Allowable time (1 Secondary representative 10ms) return : 0: The instruction was correctly answered 1: The instruction was not answered ***********************************************************************/
uint8_t sim900a_send_end(uint8_t data,uint8_t *ack,uint16_t time)
{
sim900a_receive_count = 0;
memset(sim900a_receive_data,'\0',sizeof(sim900a_receive_data));
usart_sendByte(USART2,data); // Send end instruction
if(ack == NULL) return 0;
// Enable interrupt reception
OPEN_USART2_RECEIVE;
while(time--)
{
delay_ms(10);
// Look for the key words you are looking for Find the switch and disconnect the receiver And back to 0
if(strstr((char *)sim900a_receive_data,(char *)ack) != NULL)
{
CLOSE_USART2_RECEIVE; // Turn off interrupt reception
return 0;
}
}
CLOSE_USART2_RECEIVE; // Turn off interrupt reception
return 1; // Timeout has not received the keyword you want Turn off the serial port interrupt And back to 1
}
/********************************************************************** describe : Generate verification code randomly ('0'~'9') Generate characters '0' ~ '9' The decimal system is 48 ~ 57 ***********************************************************************/
void generate_verification_code(char *data,uint8_t number)
{
uint8_t i;
// srand(time(NULL));
for(i=0;i<number;i++)
{
data[i] = 48 + (rand() % 10);
}
}
/********************************************************************** describe : ASCII turn unicode such as '1' Turn into "0031" ***********************************************************************/
void ASCII_TO_Unicode(char *ASCII,char *Unicode)
{
int length;
int i = 0;
int j = 0;
memset(Unicode,'\0',sizeof(Unicode));
length = strlen(ASCII);
for(i=0;i<length;i++)
{
Unicode[j++] = '0';
Unicode[j++] = '0';
Unicode[j++] = (ASCII[i] / 16) + 0x30;
Unicode[j++] = (ASCII[i] % 16) + 0x30;
}
}
/********************************************************************** describe : SIM900A Send verification code function return : 0: success 1: Failure ***********************************************************************/
uint8_t sim900a_send_verification_code(void)
{
uint8_t ack; // Storage status
uint8_t err = 3; // Set the number of possible errors
uint8_t send_count = 3; // Number of times sent If the sending times reach or fail, the sending will be abandoned
uint8_t sim900a_send_buffer[96] = {
0};
SEND_AT:
strcpy((char *)sim900a_send_buffer,"AT+CMGS=\"");
// Send instructions AT Check whether it returns OK to 300ms Time detection
ack = sim900a_send_cmd(SIM900A_SEND_AT,"OK",30);
if(ack)
{
if(err-- == 0)
{
printf("AT FAIL\r\n");
return 1;
}
goto SEND_AT;
}
printf("AT OK\r\n");
err = 3;
// send out SMS format Instructions Check whether it returns OK to 300ms Time detection
SELECT_SMS_FORMAT:
ack = sim900a_send_cmd(SIM900A_SELECT_SMS_FORMAT,"OK",30);
if(ack)
{
if(err-- == 0)
{
printf("SMS FORMAT FAIL\r\n");
return 1;
}
goto SELECT_SMS_FORMAT;
}
printf("SMS FORMAT SUCCESS\r\n");
err = 3;
// send out choice TE Font set Instructions Check whether it returns OK to 300ms Time detection
SELECT_TE_FONT:
ack = sim900a_send_cmd(SIM900A_SELECT_TE_FONT,"OK",30);
if(ack)
{
if(err-- == 0)
{
printf("SELECT_TE_FONT FAIL\r\n");
return 1;
}
goto SELECT_TE_FONT;
}
printf("SELECT_TE_FONT SUCCESS\r\n");
err = 3;
// send out choice TE Font set Instructions Do not detect the return value
SAVE_SMS_SET:
ack = sim900a_send_cmd(SIM900A_SAVE_SMS_SET,NULL,0);
if(ack)
{
if(err-- == 0)
{
printf("SAVE_SMS_SET FAIL\r\n");
return 1;
}
goto SAVE_SMS_SET;
}
printf("SAVE_SMS_SET SUCCESS\r\n");
err = 3;
// send out Set SMS text mode parameters Instructions Check whether it returns OK to 300ms Time detection
SET_SMS_TEST_MODE:
ack = sim900a_send_cmd(SIM900A_SET_SMS_TEST_MODE,"OK",30);
if(ack)
{
if(err-- == 0)
{
printf("SMS TEST MODE FAIL\r\n");
return 1;
}
goto SET_SMS_TEST_MODE;
}
printf("SMS TEST MODE SUCCESS\r\n");
err = 3;
// Let's set the phone number of the receiving SMS party to generate unicode code
ASCII_TO_Unicode(PHONE_NUMBER,&sim900a_send_buffer[strlen(sim900a_send_buffer)]);
strcat((char *)sim900a_send_buffer,"\"\r\n");
printf("%s\r\n",sim900a_send_buffer);
// send out Phone number of the receiving party Instructions Check whether it returns OK to 1000ms Time detection
SELECT_PHONE_NUMBER:
ack = sim900a_send_cmd(sim900a_send_buffer,">",100);
if(ack)
{
if(err-- == 0)
{
printf("SET PHONE NUMBER FAIL\r\n");
return 1;
}
goto SELECT_PHONE_NUMBER;
}
printf("SET PHONE NUMBER SUCCESS\r\n");
err = 3;
// Empty buffer Prepare to send verification code
memset(sim900a_send_buffer,'\0',sizeof(sim900a_send_buffer));
// From here, you can start sending content
//1- Generate verification code
strcpy((char *)sim900a_send_buffer,"9A8C8BC17801003A"); // add to unicode code " Verification Code :"
generate_verification_code((char *)verification_code,6);// Generate verification code
printf(" Verification code generation results :%s\r\n",verification_code);
//2- Will be randomly generated 6 Bit verification code to Unicode code And spliced together
ASCII_TO_Unicode((char *)verification_code,&sim900a_send_buffer[strlen(sim900a_send_buffer)]);
strcat((char *)sim900a_send_buffer,"\r\n"); // Add carriage return for line change
printf("%s\r\n",sim900a_send_buffer);
//3- send data
SEND_DATA:
ack = sim900a_send_cmd(sim900a_send_buffer,">",300);
if(ack)
{
if(err-- == 0)
{
printf("SEND DATA FAIL\r\n");
return 1;
}
goto SEND_DATA;
}
printf("SEND DATA SUCCESS\r\n");
//4- send out 0X1A sign
ack = sim900a_send_end(0x1a,"OK",800);
if(ack)
{
if(send_count-- == 0) return 1;
memset(sim900a_send_buffer,'\0',sizeof(sim900a_send_buffer));
err = 3;
goto SEND_AT; // Resend SMS
}
printf("SEND MESSAGE SUCCESS\r\n");
return 0;
}
/********************************************************************** describe : SIM900A initialization function return : 0: success 1: Failure ***********************************************************************/
uint8_t sim900a_init(void)
{
uint8_t ack; // Storage status
uint8_t err; // Set the number of possible errors
err = 3;
// Send instructions AT Check whether it returns OK to 300ms Time detection
SEND_AT:
ack = sim900a_send_cmd(SIM900A_SEND_AT,"OK",30);
if(ack)
{
if(err-- == 0)
{
printf("AT FAIL\r\n"); //ESP8266 Module does not exist
return 1;
}
goto SEND_AT;
}
printf("AT SUCCESS\r\n");
return 0;
}
sim900a.h
#ifndef __SIM900A_H
#define __SIM900A_H
#include "stm32f10x.h"
/* Users must change xxxxxxxxxxx Change to the phone number for receiving SMS */
#define PHONE_NUMBER "xxxxxxxxxxx"
#define SIM900A_SEND_AT "AT\r\n"
//1. Select the SMS format (0:PDU Pattern 1: Text mode )
#define SIM900A_SELECT_SMS_FORMAT "AT+CMGF=1\r\n"
//2. choice TE Font set
#define SIM900A_SELECT_TE_FONT "AT+CSCS=\"UCS2\"\r\n"
//3. preservation SMS Set up
#define SIM900A_SAVE_SMS_SET "AT+CSCA?\r\n"
//4. Set SMS text mode parameters
#define SIM900A_SET_SMS_TEST_MODE "AT+CSMP=17,167,0,25\r\n"
extern uint8_t sim900a_receive_data[128];
extern uint16_t sim900a_receive_count;
extern uint8_t verification_code[7]; // Store verification code
uint8_t sim900a_send_cmd(uint8_t *str,uint8_t *ack,uint16_t time); //SIM900A The module sends a string instruction function
uint8_t sim900a_send_end(uint8_t data,uint8_t *ack,uint16_t time); //SIM900A The module sends a single byte instruction function
void generate_verification_code(char *data,uint8_t number); // Generate verification code randomly ('0'~'9')
void ASCII_TO_Unicode(char *ASCII,char *Unicode); // ASCII turn unicode
uint8_t sim900a_init(void);
uint8_t sim900a_send_verification_code(void);
#endif
main.c
#include "stm32f10x.h"
#include "delay.h"
#include "usart.h"
#include "sim900a.h"
/* Be careful : Come first sim900a.h Fill in the phone number for receiving SMS . connection : STM32F103 SIM900A PA2 5VR PA3 5VT 5V 5V GND GND */
int main()
{
usart1_init(115200); // A serial port 1 initialization For printing information
usart2_init(115200); // A serial port 2 initialization Used for and SIM900A modular
do
{
printf("SIM900A Init...\r\n");
}
while(sim900a_init());
printf("SIM900A Init Success\r\n");
if(sim900a_send_verification_code())
{
printf(" Failed to send SMS \r\n");
}
else
{
printf(" Send SMS successfully \r\n");
}
while(1);
}
Project presentations
summary
problem 1: At first, when I used the computer serial port assistant to test alone , Two serial port assistants are used, and the last step is reached send out 0x1A When to return error, I think there is something wrong with this module , Then use the one given by the merchant and it will be successful ..... Therefore, it is recommended to use the serial port assistant provided by the merchant ....
problem 2: If sending fails , Or maybe your cell phone is in arrears Ha ha ha ha ha ha ha . ah , I am . wipe . It took me half a day .
If I have a problem with what I wrote , Or not , Please give me more advice
# The whole project code .
printf("+meQ:844797079\n")
If you think this article is useful to you . Welcome to like 、 Comment ha ha
Continue refueling !!!
边栏推荐
- Model building process 1==miidock
- It's the first time that the programmer interview pays so much attention to the concept of investigation
- Performance monster on arm64: installation and performance test of API gateway Apache APIs IX on AWS graviton3
- web開發項目,web單頁開發
- Web 3.0? High cost version of P2P
- Text error correction -- crisp model
- Interview questions MySQL database
- [tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
- 智能客服系统框架rasa
- Idea usage
猜你喜欢
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction
LVGL库入门教程01-移植到STM32(触摸屏)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (II)
The most complete network, including interview questions + Answers
(small information for children to children-03) batch template production of basic information collection folder for children (including PDF, word and certificate folder)
Performance monster on arm64: installation and performance test of API gateway Apache APIs IX on AWS graviton3
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
2022年二建《建筑》参考答案汇总
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
随机推荐
Mac 安装 MySQL 教程
Interview questions MySQL database
致力超表面光子芯片产品研发与制造,山河光电完成数千万元Pre-A轮融资
break algorithm---multi-interface
(small information for children to children-03) batch template production of basic information collection folder for children (including PDF, word and certificate folder)
The most complete network, including interview questions + Answers
Model building process 1==miidock
TS進階之條件類型
Miidock file distribution
二建资格后审没通过怎么办?这篇文章告诉你
VSCode 如何将已编辑好的文件中的 tab 键转换成空格键
[tcapulusdb knowledge base] tcapulusdb Model Management Introduction
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
To avoid letting the transformation enterprises go astray, it is time to re understand the integration of xiahu warehouse| Q recommendation
Socket programming (Part 1)
(幼升小信息-03)批量模板制作 幼儿基本信息收集文件夹(包含PDF、Word、证件文件夹)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)
面试题 Mysql 数据库
It's the first time that the programmer interview pays so much attention to the concept of investigation
TS advanced condition type