当前位置:网站首页>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


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

 Insert picture description here
...... 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
 Insert picture description here
And DuPont line, needless to say

2. connection

USB-TTLSIM900A
5VVCC5
GNDGND
TX5VR
RX5VT

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
 Insert picture description here

4. test result

 Insert picture description here

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 ?
 Insert picture description here

/**********************************************************************  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
 Insert picture description here

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

 Insert picture description here

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

原网站

版权声明
本文为[Pica Churchill]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131142502748.html