NBIOT column of BC28 and STM32F103 MCU serial communication connection to Alibaba Cloud

Series Article Directory

Chuangsi Communication BC28 core board + STM32F series microcontroller development record blog
1. Test BC28 module 2. Serial assistant MQTT connects to Alibaba
Cloud IoT platform to receive and send data


Preface

I recorded the process of BC28 using the serial debugging assistant to connect to Alibaba Cloud. The most troublesome thing is to use the MCU to connect to Alibaba Cloud, because the code is really a headache! Fortunately, after constantly looking for source code, testing the code, and modifying the code, it was finally done! Let me share with you!

text

1. Serial port initialization

The BC28 module and the microcontroller send AT commands through the serial port! So you need to initialize the serial port! I use the board of the wildfire guide, here I use serial port 1 for serial port printing, and serial port 3 for communication between MCU and BC28!
Here I mainly talk about the two pins of my serial port 3 are B10 and B11. B10 is connected to the transmitter (Tx) of BC28 as the receiving end of the microcontroller, and B11 is connected to the receiving end (Rx) of BC28 as the transmitter of the microcontroller. The following is My serial port 3 receives the data returned by the BC28 module and stores it in the RxBuffer[] array.

void USART3_IRQHandler(void)         //串口3中断服务程序
{
		char Res;
	//接收模块返回来的数据
    if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  
		{   
		  /接收模块返回来的数据
            Res=USART_ReceiveData(USART3);
            RxBuffer[RxCounter++] =USART_ReceiveData(USART3);
        } 
} 	

2. Open the BC28 module
Send AT command to BC28, if there is no response, then open BC28.

void OPEN_BC28(void)
{
   char *strx;
    printf("AT\r\n"); 
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
    printf("AT\r\n"); 
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
   if(strx==NULL)
	{
        PWRKEY=1;//拉低
        delay_ms(300);
        delay_ms(300);
        delay_ms(300);
        delay_ms(300);	
        PWRKEY=0;//拉高正常开机
	}
    printf("AT\r\n"); 
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
     if(strx==NULL)//如果设备休眠了,就复位模块
     {
        RESET=1;//拉低
        delay_ms(300);
        delay_ms(300);	
        RESET=0;//复位模块
     }
    printf("ATE0&W\r\n"); //关闭回显
    delay_ms(300); 
    printf("AT+QMTDISC=0\r\n");//关闭连接
    delay_ms(300);
    printf("AT+QMTCLOSE=0\r\n");
    delay_ms(300); 
}

3.BC28 initialization
The initialization of BC28 is also the one-chip computer sending initialization AT command to BC28 through serial port 3.
The printf() function is used to send the command, and the strstr function is used to compare the received response data with the normal data that should be returned. If it is inconsistent, null is returned, and the program enters the loop to send the command until it is consistent with the specified data.

//RxBuffer[255]存放单片机接收BC28发来的数据,缓冲区
extern unsigned char  RxBuffer[255],RxCounter;

//清空缓存区
void Clear_Buffer(void)//清空缓存
{
		u8 i;
		Uart1_SendStr(RxBuffer);
		for(i=0;i<100;i++)
		RxBuffer[i]=0;//缓存
		RxCounter=0;
}

void BC28_Init(void)
{
    printf("AT\r\n"); 
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
    Clear_Buffer();	
    while(strx==NULL)
    {
        Clear_Buffer();	
        printf("AT\r\n"); 
        delay_ms(300);
        strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
    }
    printf("AT+CFUN=1\r\n");//获取卡号,类似是否存在卡的意思,比较重要。
		
    delay_ms(300);
    printf("AT+CIMI\r\n");//获取卡号,类似是否存在卡的意思,比较重要。
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"460");//返460,表明识别到卡了
    Clear_Buffer();	
    while(strx==NULL)
    {
        Clear_Buffer();	
        printf("AT+CIMI\r\n");//获取卡号,类似是否存在卡的意思,比较重要。
        delay_ms(300);
        strx=strstr((const char*)RxBuffer,(const char*)"460");//返回OK,说明卡是存在的
    }
        printf("AT+CGATT=1\r\n");//激活网络,PDP
        delay_ms(300);
        strx=strstr((const char*)RxBuffer,(const char*)"OK");//返OK
        Clear_Buffer();	
        printf("AT+CGATT?\r\n");//查询激活状态
        delay_ms(300);
        strx=strstr((const char*)RxBuffer,(const char*)"+CGATT:1");//返1
		    Uart1_SendStr((char*)RxBuffer);
        Clear_Buffer();	
		    Uart1_SendStr("CGATT。。。。。。");
		while(strx==NULL)
		{
            Clear_Buffer();	
            printf("AT+CGATT?\r\n");//获取激活状态
            delay_ms(300);
            strx=strstr((const char*)RxBuffer,(const char*)"+CGATT:1");//返回1,表明注网成功
		}
		printf("AT+CESQ\r\n");//查看获取CSQ值
        delay_ms(300);
        strx=strstr((const char*)RxBuffer,(const char*)"+CESQ");//返回CSQ
		if(strx)
			{
				BC26_Status.CSQ=(strx[7]-0x30)*10+(strx[8]-0x30);//获取CSQ
				if((BC26_Status.CSQ==99)||((strx[7]-0x30)==0))//说明扫网失败
				{
					while(1)
					{
                        BC26_Status.netstatus=0;
						Uart1_SendStr("信号搜索失败,请查看原因!\r\n");
                        RESET=1;//拉低
                        delay_ms(300);
                        delay_ms(300);	
                        RESET=0;//复位模块
						delay_ms(300);//没有信号就复位
					}
				}
             else
             {
                 BC26_Status.netstatus=1;
              }        
            }
              Clear_Buffer();	
}

4. Connect to the Alibaba Cloud IoT platform

(1) First, we must macro-define the triplet information of Alibaba Cloud platform.

#define ProductKey     "a1tW6nsMu2w"             //²úÆ·KEY
#define DeviceName          "BC28"      //
#define DeviceSecret    "9ef288679f79069558bbf3d82da90931"  //
#define PubTopic         "/sys/a1tW6nsMu2w/BC28/thing/event/property/post"
#define SubTopic        "/sys/a1tW6nsMu2w/BC28/thing/service/property/set"

(2) Connect to the Alibaba Cloud IoT platform

void  MQTT_Init(void)
{
    printf("AT+QMTCFG=\"aliauth\",0,\"%s\",\"%s\",\"%s\"\r\n",ProductKey,DeviceName,DeviceSecret);
    delay_ms(300);
    printf("AT+QMTOPEN=0,\"139.196.135.135\",1883\r\n");//通过TCP方式去连接MQTT阿里云服务器 
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"+QMTOPEN: 0,0");//看下返回状态
  while(strx==NULL)
    {
      strx=strstr((const char*)RxBuffer,(const char*)"+QMTOPEN: 0,0");//确认返回值正确
    }
    Clear_Buffer(); 
   printf("AT+QMTCONN=0,\"client\"\r\n");//去登录MQTT服务器,名称随意
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"+QMTCONN: 0,0,0");//看下返回状态
  while(strx==NULL)
    {
        strx=strstr((const char*)RxBuffer,(const char*)"+QMTCONN: 0,0,0");//看下返回状态
    }
    Clear_Buffer(); 
}
 

(3) Send data to Alibaba Cloud

void aliyunMQTT_PUBdata(u8 temp,u8 humi)
{
     u8 t_payload[200],len;
     printf("AT+QMTPUB=0,0,0,0,\"%s\"\r\n",PubTopic);//发布主题
     delay_ms(300);
     len=Mqttaliyun_Savedata(t_payload);
     t_payload[len]=0;
     printf("%s",t_payload);
  while((USART3->SR&0X40)==0);//循环发送,直到发送完?
		 USART3->DR = (u8) 0x1A;    
     delay_ms(100);
     strx=strstr((const char*)RxBuffer,(const char*)"+QMTPUB: 0,0,0");//看下返回状态
  while(strx==NULL)
    {
        strx=strstr((const char*)RxBuffer,(const char*)"+QMTPUB: 0,0,0");//看下返回状态
    }
    Clear_Buffer(); 
}
//生成JSON格式数据
u8 Mqttaliyun_Savedata(u8 *t_payload,)
{
	  int err;
	   uint16_t pkt_id = 1;
    char led1status1,led1status2; 

     char json[]="{params:{RoomHumidity:5.8}}";	 
   	char t_json[200];
    int payload_len;
    unsigned short json_len;
    sprintf(t_json, json, temp, humi);
    payload_len =  strlen(t_json)/sizeof(char);
    json_len = strlen(t_json)/sizeof(char);
  	memcpy(t_payload, t_json, json_len);
    return json_len;
}

Guess you like

Origin blog.csdn.net/JIANGYINGH/article/details/112580210