STM32+NB模块对接阿里云

该代码展示了如何使用STM32与BC260Y NB-IoT模块进行初始化,包括网络激活、TCP连接创建、数据发送及接收。主要涉及串口通信、物联网连接、MQTT协议配置,以及数据的JSON格式封装。代码中定义了各种功能函数,如连接TCP、创建socket、发送数据等,用于实现与云端平台的数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码分为NB模组,串口调试,串口接NB

NB.c

#include "bc260y.h"
#include "string.h"
#include "usart.h"
#include "wdg.h"
#include "led.h"
char *strx,*extstrx;
extern unsigned char  RxBuffer[255],RxCounter;
BC260Y BC260Y_Status;

#define ProductKey     "a1SZ2mtWIur"             //产品KEY
#define DeviceName          "m001"      //
#define DeviceSecret    "9dc54467d48a50867dd3a089ef078224"  //
#define PubTopic         "/sys/a1SZ2mtWIur/m001/thing/event/property/post"//发布主题
#define SubTopic        "/sys/a1NXoxeFw4c/mzh002/thing/service/property/set"//订阅主题
void Clear_Buffer(void)//清空缓存
{
		u8 i;
		Uart1_SendStr((char*)RxBuffer);
		for(i=0;i<255;i++)
		RxBuffer[i]=0;//缓存
		RxCounter=0;
		IWDG_Feed();//喂狗
	
}
void BC260Y_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+QMTDISC=0\r\n");
		delay_ms(300);
    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
        Clear_Buffer();	
		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+CSQ\r\n");//查看获取CSQ值
    delay_ms(300);
    Clear_Buffer();	
}




void BC260Y_ConTCP(void)
{
	printf("AT+QICLOSE=0\r\n");//关闭socket连接
	delay_ms(300);
    Clear_Buffer();
    IWDG_Feed();//喂狗
}
void BC260Y_CreateTCPSokcet(void)//创建socket
{

    printf("AT+QIOPEN=1,0,\"TCP\",\"47.99.80.89\",14269,1234,1\r\n");//创建连接TCP,输入IP以及服务器端口号码,采用直接吐出的方式
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"+QIOPEN: 0,0");//检查四否登录成功
 	while(strx==NULL)
		{
            strx=strstr((const char*)RxBuffer,(const char*)"+QIOPEN: 0,0&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值