温湿度采集bc20模块stm32底板阿里云物联网上传实例

 

 

 第一步将板子的BT0和3V3针脚串联,按下reset复位键

点击next

 选择源码目录user的hex文件即可

注意这个板子是115200波特率,返回ok证明已经通讯成功; 

修改代码连接自己的mqtt

#include "BC20.h"
#include "string.h"
#include "usart.h"
#include "wdg.h"
#include "led.h"
#include "cjson.h"
char *strx,*extstrx;
extern unsigned char  RxBuffer[255],RxCounter;
BC20 BC20_Status;
extern u8  Usart2IdleFlag;

阿里云的三元素,在设备里面都可以查询到//
#define ProductKey     "a1ZMkwhevG9"             //产品KEY
#define DeviceName          "mzh001"      //
#define DeviceSecret    "cc96840980ac49570acc47e2c0195b5c"  //
#define PubTopic         "/sys/webdu/pub"
#define SubTopic        "/sys/webdu/sub"
void Uart1_SendStr(char*SendBuf)//串口1打印数据
{
	while(*SendBuf)
	{
        while((USART1->SR&0X40)==0){}//等待发送完成 
        USART1->DR = (u8) *SendBuf; 
		SendBuf++;
	}
}
void Clear_Buffer(void)//清空缓存
{
		u8 i;
		Uart1_SendStr((char*)RxBuffer);
		for(i=0;i<100;i++)
		RxBuffer[i]=0;//缓存
		RxCounter=0;
		IWDG_Feed();//喂狗
	
}
void BC20_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+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+CESQ\r\n");//查看获取CSQ值
    delay_ms(300);
    strx=strstr((const char*)RxBuffer,(const char*)"+CESQ");//返回CSQ
    Clear_Buffer();	
		
}

void  MQTT_Init(void)
{
  
    //printf("AT+QMTCFG=\"aliauth\",0,\"%s\",\"%s\",\"%s\"\r\n",ProductKey,DeviceName,DeviceSecret);
    delay_ms(300);
    printf("AT+QMTOPEN=0,\"47.94.13.62\",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,\"client2023\",\"admin\",\"admin888\"\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(); 
    /*
    printf("AT+QMTSUB=0,1,\"mzh_m26\",0\r\n");//订阅个主题
        delay_ms(500);
    strx=strstr((const char*)RxBuffer,(const char*)"+QMTSUB: 0,1,0,0");//订阅成功
  while(strx==NULL)
    {
        strx=strstr((const char*)RxBuffer,(const char*)"+QMTSUB: 0,1,0,0");//订阅成功
    }
    Clear_Buffer(); 
    */
}

void aliyunMQTT_PUBdata(u8 temp,u8 humi,float adcvalue)
{
     u8 t_payload[200],len;
	    len=Mqttaliyun_Savedata(t_payload,temp,humi,adcvalue);
     t_payload[len]=0;
     printf("AT+QMTPUB=0,0,0,0,%s,%s\r\n",PubTopic,t_payload);//发布主题
		 //printf("AT+QMTPUB=0,0,0,0,%s,%s\r\n",PubTopic,"acsfsdf");
     delay_ms(300);
     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");//看下返回状态
    }
    delay_ms(500);
}

//访问阿里云需要提交JSON数据
u8 Mqttaliyun_Savedata(u8 *t_payload,u8 temp,u8 humi,float adcvalue)
{
	char json[]="{\"id\":\"26\",\"version\":\"1.0\",\"sys\":{\"ack\":0},\"params\":{\"CurrentTemperature\":{\"value\":%d},\"CurrentHumidity\":{\"value\":%d},\"LightLux\":{\"value\":%d.%01d}},\"method\":\"thing.event.property.post\"}";	 //     char json[]="{\"datastreams\":[{\"id\":\"location\",\"datapoints\":[{\"value\":{\"lon\":%2.6f,\"lat\":%2.6f}}]}]}";
    char t_json[200];
    unsigned short json_len;
	  u8 data;
	  data=(adcvalue-(u8)adcvalue)*10;
    sprintf(t_json, json, temp, humi,(u8)adcvalue,data);
    json_len = strlen(t_json)/sizeof(char);
  	memcpy(t_payload, t_json, json_len);
    return json_len;
}



void BC20_RECData(void)
{
	  u8 recdata[250];
		Uart1_SendStr(RxBuffer);
    strx=strstr((const char*)RxBuffer,(const char*)"+QMTRECV:");//返回+QMTRECV:,表明接收到onenet平台
	      //+QMTRECV: 0,0,"$creq/a54ff306-8993-52e1-9cbf-5ef099f4c0c8","1"
      if(strx)
            {
                strx=strstr((const char*)RxBuffer,(const char*)"}\"");
                //  有}"出现代表数据接收完成
                if(strx)
                {
                  memcpy(recdata, RxBuffer, RxCounter);//接收到的数据保存 
                  CParsejson(recdata); 
                  Clear_Buffer();  
               
                 }
                  memset(recdata, 0, 250);
             
            } 
						if(Usart2IdleFlag)//空闲的时候,将数组归0
						{
							RxCounter=0;
						}
    
}




void CParsejson(void *data)
{
    
    u8 i=0,*jsonstrx;
		u8 jsondata[250];
		u8 status;

  // char* data = "{\"method\":\"thing.service.property.set\",\"id\":\"1686637400\",\"params\":{\"WorkSwitch\":1},\"version\":\"1.0.0\"}";
     cJSON * root, *itemName,*itemAge,*order;
     //从缓冲区中解析json结构
    jsonstrx=strstr((const char*)data,(const char*)"{"); //判断到有"{出现,代表json产生 
    if(jsonstrx)
    {
         while(1)
        {
            if(jsonstrx[i]==0x0D)//找到结束符,就退出  
            {   
                jsondata[i-1]=0;
                i=0;
                break;
            }
            jsondata[i]=jsonstrx[i];
            i++;
        }
        
    }
    Uart1_SendStr(jsondata);
    root = cJSON_Parse(jsondata);   
    if (jsondata == NULL)
    {
			   //将json结构占用的数据空间释放
        memset(jsondata, 0, 250);
        cJSON_Delete(root);
        return;
    }
  order = cJSON_GetObjectItem(root, "params");
	if (order == NULL) {
		 //将json结构占用的数据空间释放
    memset(jsondata, 0, 250);
    cJSON_Delete(root);
		return ;
	}
    itemName = cJSON_GetObjectItem(order, "NightLightSwitch");//控制LED开关
    if (itemName)
    {
      status= itemName->valueint;//
        if(status)//开灯
				{
           
            LED1=0;
					 
				}
       else//关灯
			 {
            LED1=1;
			 }
    }	
    itemName = cJSON_GetObjectItem(order, "Buzzer");//控制蜂鸣器
    if (itemName)
    {
      status= itemName->valueint;//
        if(status)//开
            BEEP=1;
       else//关
            BEEP=0;
    }	
    //将json结构占用的数据空间释放
    memset(jsondata, 0, 250);
    cJSON_Delete(root);
}


//访问阿里云需要提交JSON数据将状态值返回
void Mqttaliyun_SaveREADdata(void)
{
	  char json[]="{\"id\":\"26\",\"version\":\"1.0\",\"sys\":{\"ack\":0},\"params\":{\"NightLightSwitch\":%d,\"Buzzer\":%d},\"method\":\"thing.event.property.post\"}";	 //     char json[]="{\"datastreams\":[{\"id\":\"location\",\"datapoints\":[{\"value\":{\"lon\":%2.6f,\"lat\":%2.6f}}]}]}";
    char t_json[200];
		u8 LEDstatus, BEEPstatus;
    unsigned short json_len;
	    if(READLED1)//高电平,关灯
				LEDstatus=0;
			else
				LEDstatus=1;
			if(READBEEP)//高电平,开
				BEEPstatus=1;
			else
				BEEPstatus=0;
     sprintf(t_json, json, LEDstatus, BEEPstatus);
     json_len = strlen(t_json)/sizeof(char);
     t_json[json_len]=0;
     printf("AT+QMTPUB=0,0,0,0,%s,%s\r\n",PubTopic,t_json);//发布主题
     delay_ms(300);
     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");//看下返回状态
    }
    delay_ms(300);
}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值