概述
STM32L BC26初体验-上传多个实例到ONENET
对官方Example(UDP传输与接收数据液晶屏显示)进行的修改,使用LWM2M向平台发送4个实例,temperature instance(5700),Light instance (5850), digit input (5500), Battery Percentage (5700).
main.c
int main(void)
{
BOOL Read_75_Flag = TRUE;
if (SysTick_Config(SystemCoreClock / 1000))//ÉèÖÃ24bit¶¨Ê±Æ÷ 1msÖжÏÒ»´Î
{
/* Capture error */
while (1);
}
GPIO_CONFIGURATION();
uart1_init(115200);
uart2_init(115200);
uart3_init(115200);
delay_init();
while(DHT11_Init());//³õʼ»¯DHT11
while(BC26_Init());
BC26_PDPACT();
BC26_ConLWM2M();
// Éϱ¨100%ʱºòµÄµçÁ¿
memcpy(sendata,"100",strlen("100"));
memset(lenstr,0,BUFLEN);
sprintf(lenstr,"%d",strlen((char *)sendata));
BC26_Senddata(3,(u8 *)lenstr,(u8 *)sendata);//·¢ËÍÊý¾Ý
while(1)
{
// DHT11_Read_Data(&temp,&humi);//¶ÁÈ¡ÎÂʪ¶ÈÊý¾Ý
// printf(“ζȣº%d C rn”,temp);
// printf(“ʪ¶È£º%d RHrn”,humi);
// memset(sendata,0,100);
// memset(tempstr,0,100);
// sprintf(tempstr,"%d",temp);
// strcat(sendata,tempstr);
// strcat(sendata,".00");
// memset(lenstr,0,BUFLEN);
// sprintf(lenstr,"%d",strlen((char *)sendata)); //»ñÈ¡·¢Ë͵ij¤¶È
// printf(“sendata = %s len = %s rn”, sendata, lenstr);
// BC26_Senddata(0,(u8 *)lenstr,(u8 *)sendata);//·¢ËÍÊý¾Ý
// delay_ms(1000);
if(Read_75_Flag){
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_SET)
{
printf(“75 battery remainingrn”);
memcpy(sendata,“75”,strlen(“75”));
memset(lenstr,0,BUFLEN);
sprintf(lenstr,"%d",strlen((char *)sendata));
BC26_Senddata(3,(u8 *)lenstr,(u8 *)sendata);//·¢ËÍÊý¾Ý
Read_75_Flag = FALSE;
}
}
if(LED_FLAG == SET)
{
if(led_status == RESET)
{
printf("Open the doorrn");
memcpy(sendata,"1",strlen("1"));
}
else
{
printf("Close the doorrn");
memcpy(sendata,"0",strlen("0"));
}
memset(lenstr,0,BUFLEN);
sprintf(lenstr,"%d",strlen((char *)sendata));
BC26_Senddata(1,(u8 *)lenstr,(u8 *)sendata);//·¢ËÍÊý¾Ý
LED_FLAG = RESET;
}
if(AED_FLAG == SET)
{
if(sd_status == SD_PRESENT)
{
printf("insert AED devicern");
memcpy(sendata,"1",strlen("1"));
}
else
{
printf("remove AED devicern");
memcpy(sendata,"0",strlen("0"));
}
memset(lenstr,0,BUFLEN);
sprintf(lenstr,"%d",strlen((char *)sendata));
BC26_Senddata(2,(u8 *)lenstr,(u8 *)sendata);//·¢ËÍÊý¾Ý
AED_FLAG = RESET;
}
}
}
BC26.C
void BC26_ConLWM2M(void)
{
int errcount = 0;
int i=0,j=0;
Uart2_SendStr(“AT+MIPLCREATErn”); //+MIPLCREATE: 0
Delay(500);
Delay(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)“OK”);
if(strx==NULL)
{
printf(“Æô¶¯LWM2MÐÒéʧ°Ürn”);
Clear_Buffer();
Uart2_SendStr(“AT+MIPLCREATErn”); //+MIPLCREATE: 0
Delay(500);
Delay(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)“OK”);
if(strx)
{
printf(“Æô¶¯LWM2MÐÒé³É¹¦rn”);
}
else
{
strx=strstr((const char*)buf_uart2.buf,(const char*)“ERROR: 653”);
if(strx)
{
GPIO_SetBits(GPIOC,GPIO_Pin_7); //Ä£¿éÖØÆô
Delay(500);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
Delay(300);
NVIC_SystemReset(); //ûÓд´½¨TCP SOCKET¾ÍÖØÆôϵͳµÈµ½·þÎñÆ÷¾ÍÐ÷
}
}
Clear_Buffer();
}
Clear_Buffer();
Uart2_SendStr("AT+MIPLADDOBJ=0,3303,1,1,1,0rn"); // Temputure instance 5700
Delay(300);
Clear_Buffer();
Delay(300);
Uart2_SendStr("AT+MIPLADDOBJ=0,3311,1,1,1,0rn"); // Light instance 5850
Delay(300);
Clear_Buffer();
Delay(300);
Uart2_SendStr("AT+MIPLADDOBJ=0,3200,1,1,1,0rn"); // Aed device presence 5500
Delay(300);
Clear_Buffer();
Delay(300);
Uart2_SendStr("AT+MIPLADDOBJ=0,3320,1,1,1,0rn"); // Battery Percentage 5700
Delay(300);
Clear_Buffer();
Delay(300);
Uart2_SendStr("AT+MIPLOPEN=0,86400rn");//´ò¿ª
Delay(300);
while(j<INSTANCE_CNT)
{
strx=strstr((const char*)buf_uart2.buf,(const char*)"+MIPLOBSERVE");//·´À¡¹Û²ìºÅ
memset(atbuf,0,BUFLEN);
memset(objtnum,0,BUFLEN);
while(1)
{
Delay(30);
if(strlen((const char *)buf_uart2.buf) > 0)
{
strx=strstr((const char*)buf_uart2.buf,(const char*)"+MIPLOBSERVE");//¼ì²âµ½¹Û²ìºÅ½áÊø
if(strx)
{
sprintf(atbuf,"%s",buf_uart2.buf);
strx=strstr((const char*)strx+1,(const char*)",");
for(i=0;;i++) //²éѯ¹Û²ìºÅ
{
if(strx[i+1]==',')
break;
objtnum[i]=strx[i+1];
}
printf("µÃµ½ATÃüÁî:atbuf = %s rn",atbuf);
printf("µÃµ½¹Û²ìºÅ:objtnum = %s rn",objtnum);
break;
}
else //Èç¹ûûÓÐÕÒµ½Ä¿±ê¾ÍÇå¿Õ·´À¡µÄÊý×飬ÕÒµ½Á˾ÍÌø³öÀ´
{
printf("Á¬½Ó·´À¡:%srn",buf_uart2.buf);
Clear_Buffer();
}
}
}
Clear_Buffer();
printf("object number = %srn",objtnum);
memset(atstr,0,BUFLEN);
sprintf(atstr,"AT+MIPLOBSERVERSP=0,%s,1rn",objtnum);
Uart2_SendStr(atstr);//·¢Ë͹۲ìÃüÁî
Delay(300);
Clear_Buffer();
if(strstr((const char*)atbuf,(const char*)"3303"))memcpy(objtnum1,objtnum,BUFLEN); // Temputure
else if(strstr((const char*)atbuf,(const char*)"3311"))memcpy(objtnum2,objtnum,BUFLEN); // Light Status
else if(strstr((const char*)atbuf,(const char*)"3200"))memcpy(objtnum3,objtnum,BUFLEN); // AED device presence
else if(strstr((const char*)atbuf,(const char*)"3320"))memcpy(objtnum4,objtnum,BUFLEN); // Battery
j++;
}
j=0;
while(j<INSTANCE_CNT)
{
strx=strstr((const char*)buf_uart2.buf,(const char*)"+MIPLDISCOVER");//·µ»ØOK
memset(atbuf,0,BUFLEN);
memset(distnum,0,BUFLEN);
while(1)
{
Delay(30);
if(strlen((const char *)buf_uart2.buf) > 0)
{
strx=strstr((const char*)buf_uart2.buf,(const char*)"+MIPLDISCOVER");//¼ì²âµ½¹Û²ìºÅ½áÊø
if(strx)
{
sprintf(atbuf,"%s",buf_uart2.buf);
strx=strstr((const char*)strx+1,(const char*)",");
for(i=0;;i++) //²éѯ¹Û²ìºÅ
{
if(strx[i+1]==',')
break;
distnum[i]=strx[i+1];
}
printf("µÃµ½ATÃüÁî:atbuf = %s rn",atbuf);
printf("µÃµ½×ÊÔ´ºÅ:distnum = %s rn",distnum);
break;
}
else //Èç¹ûûÓÐÕÒµ½Ä¿±ê¾ÍÇå¿Õ·´À¡µÄÊý×飬ÕÒµ½Á˾ÍÌø³öÀ´
{
Clear_Buffer();
}
}
}
Clear_Buffer();
printf("discover number = %srn",distnum);
memset(atstr,0,BUFLEN);
if(strstr((const char*)atbuf,(const char*)"3303"))
{
sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,"5700"rn",distnum); // Temputure
memcpy(distnum1,distnum,BUFLEN);
}
else if(strstr((const char*)atbuf,(const char*)"3311"))
{
sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,"5850"rn",distnum); // Light Status
memcpy(distnum2,distnum,BUFLEN);
}
else if(strstr((const char*)atbuf,(const char*)"3200"))
{
sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,"5500"rn",distnum); // AED device presence
memcpy(distnum3,objtnum,BUFLEN);
}
else if(strstr((const char*)atbuf,(const char*)"3320"))
{
sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,"5700"rn",distnum); // Battery
memcpy(distnum4,objtnum,BUFLEN);
}
Uart2_SendStr(atstr);//·¢ËÍ×ÊÔ´ÃüÁî
Delay(300);
strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//·µ»ØOK
while(strx==NULL)
{
errcount++;
strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//·µ»ØOK
if(errcount>100) //·ÀÖ¹ËÀÑ»·
{
errcount = 0;
break;
}
}
Clear_Buffer();
j++;
}
}
完整工程:https://download.csdn.net/download/sinat_16873757/14950707
最后
以上就是飞快服饰为你收集整理的NB-IOT通过LWM2M协议发送4个实例到ONENET的全部内容,希望文章能够帮你解决NB-IOT通过LWM2M协议发送4个实例到ONENET所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复