跋扈洋 · 2月14日

【Mini-F5265-OB开发板试用测评】基于NB-IOT的数据上云

引言

本文主要介绍如何通过Mini-F5265-OB开发板和nb-iot模块实现数据上云,将Mini-F5265-OB开发板的按键1的开关状态通过nb-iot传输到阿里云平台上。

前期准备

硬件

  1. Mini-F5265-OB开发板
  2. USB转TTL工具
  3. BC26(nb-iot模块)

    软件

  4. keil 5
  5. 串口调试助手
  6. MM32\_JLINK\_pack\_1.21
  7. MM32\_KEIL\_Pack\_2.25
  8. LibSamples\_MM32F5260\_V0.11.3

项目开发

硬件开发

  1. 首先是按键采集部分,我们根据电路图可以知道,按键1的对应引脚为PB0。
     title=
    并且,按键1按下的状态为低电平。
     title=
  2. 其次是串口部分,本项目使用的是串口1,根据产品手册可知,Mini-F5265-OB开发板的串口1的收发引脚分别为PA9和PA10。
     title=
    3.Mini-F5265-OB开发板和nb-iot模块连接使用串口,将Mini-F5265-OB开发板的PA9连接到nb-iot的RX引脚,将Mini-F5265-OB开发板的PA10连接到nb-iot的TX引脚。

软件开发

  1. 首先对MM32F5260的按键进行配置
    `void GPIO_Configure(void)
    {
    GPIO_InitTypeDef GPIO_InitStruct;
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    }`
  2. 对串口1进行配置,使用接收中断
    `void UART_Configure(uint32_t Baudrate)
    {
    GPIO_InitTypeDef GPIO_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    UART_InitTypeDef UART_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);

    UART_StructInit(&UART_InitStruct);
    UART_InitStruct.BaudRate = Baudrate;
    UART_InitStruct.WordLength = UART_WordLength_8b;
    UART_InitStruct.StopBits = UART_StopBits_1;
    UART_InitStruct.Parity = UART_Parity_No;
    UART_InitStruct.HWFlowControl = UART_HWFlowControl_None;
    UART_InitStruct.Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(UART1, &UART_InitStruct);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    NVIC_InitStruct.NVIC_IRQChannel = UART1_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    UART_Cmd(UART1, ENABLE);
    }`

  3. 重定向printf到UART1
    ` int fputc(int ch, FILE *f)
    {
    UART_SendData(UART1, (uint8_t)ch);
    while (RESET == UART_GetFlagStatus(UART1, UART_FLAG_TXC))
    {
    }

    return (ch);
    }`

  4. 创建阿里云物联网平台设备,并将参数放置到程序中
    在阿里云的物联网平台上,创建产品->创建设备,然后在产品中进行功能定义,这里仅定义一个switch。发布上线后,将mqtt三要素定义到程序中。
     title=
#define ProductKey "根据阿里云上的复制过来" 
#define DeviceName "根据阿里云上的复制过来"  
#define DeviceSecret "根据阿里云上的复制过来"  
#define webaliyun "根据阿里云上的复制过来" //格式为iot-xxxxxxxxx.mqtt.iothub.aliyuncs.com
  1. 编写bc26驱动程序
    准备一个清空接收buff的程序

`void Clear_Buffer(void)//清空缓存
{

    uint8_t i;
    Uart1_SendStr((char*)RxBuffer);
    for(i=0;i<255;i++)
    RxBuffer[i]=0;//缓存
    RxCounter=0;
PLATFORM_DelayMS(1);

}`
编写函数BC26_Init实现判断bc26模块是否进入到正常工作的状态。
`

printf("AT\r\n"); 
PLATFORM_DelayMS(300);
strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
Clear_Buffer();    
while(strx==NULL)
{
    Clear_Buffer();    
    printf("AT\r\n"); 
    PLATFORM_DelayMS(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返回OK
}
    printf("AT+QMTDISC=0\r\n");
    PLATFORM_DelayMS(300);
printf("AT+CFUN=1\r\n");
PLATFORM_DelayMS(300);
Clear_Buffer();    
    printf("AT+CGATT=1\r\n");//激活网络,PDP
    PLATFORM_DelayMS(300);
    strx=strstr((const char*)RxBuffer,(const char*)"OK");//返OK
    Clear_Buffer();    
    printf("AT+CGATT?\r\n");
    PLATFORM_DelayMS(300);
    strx=strstr((const char*)RxBuffer,(const char*)"+CGATT: 1");//返1
    Clear_Buffer();    
    while(strx==NULL)
    {
        Clear_Buffer();    
        printf("AT+CGATT?\r\n");
        PLATFORM_DelayMS(300);
        strx=strstr((const char*)RxBuffer,(const char*)"+CGATT: 1");//返回1,表明注网成功
    }
    printf("AT+CSQ\r\n");//查看获取CSQ值
PLATFORM_DelayMS(300);
Clear_Buffer();

`
编写MQTT\_Init实现mqtt的配置
` char atstr[BUFLEN];
int errcount;
printf("AT+QMTCLOSE=0\r\n");
PLATFORM_DelayMS(300);
Clear_Buffer();

printf("AT+QMTCFG=\"ALIAUTH\",0,\"%s\",\"%s\",\"%s\"\r\n",ProductKey,DeviceName,DeviceSecret);

strx=strstr((const char)RxBuffer,(const char)"OK");//返OK
PLATFORM_DelayMS(300);
Clear_Buffer();

printf("AT+QMTOPEN=0,\"%s\",1883\r\n",webaliyun);
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,\"%s\"\r\n",DeviceName);
PLATFORM_DelayMS(300);
strx=strstr((const char)RxBuffer,(const char)"+QMTCONN: 0,0,0");//返+QMTCONN: 0,0,0

while(strx==NULL)
{
strx=strstr((const char)RxBuffer,(const char)"+QMTCONN: 0,0,0");//看下返回状态
}
Clear_Buffer();`

现象

当按下Mini-F5265-OB开发板按键1时,物联网云平台显示效果如下:
image.png
当松开Mini-F5265-OB开发板按键1时,物联网云平台显示效果如下:
image.png

推荐阅读
关注数
1
内容数
1
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息