本文主要是介绍NB-IOT通过LWM2M协议发送4个实例到ONENET,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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());//³õʼ»¯DHT11while(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 \r\n”,temp);
// printf(“ʪ¶È£º%d RH\r\n”,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 \r\n”, 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 remaining\r\n”);
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 door\r\n");memcpy(sendata,"1",strlen("1"));}else{printf("Close the door\r\n");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 device\r\n");memcpy(sendata,"1",strlen("1"));}else{printf("remove AED device\r\n");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+MIPLCREATE\r\n”); //+MIPLCREATE: 0
Delay(500);
Delay(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)“OK”);
if(strx==NULL)
{
printf(“Æô¶¯LWM2MÐÒéʧ°Ü\r\n”);
Clear_Buffer();
Uart2_SendStr(“AT+MIPLCREATE\r\n”); //+MIPLCREATE: 0
Delay(500);
Delay(500);
strx=strstr((const char*)buf_uart2.buf,(const char*)“OK”);
if(strx)
{
printf(“Æô¶¯LWM2MÐÒé³É¹¦\r\n”);
}
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,0\r\n"); // Temputure instance 5700
Delay(300);
Clear_Buffer();
Delay(300);Uart2_SendStr("AT+MIPLADDOBJ=0,3311,1,1,1,0\r\n"); // Light instance 5850
Delay(300);
Clear_Buffer();
Delay(300);Uart2_SendStr("AT+MIPLADDOBJ=0,3200,1,1,1,0\r\n"); // Aed device presence 5500
Delay(300);
Clear_Buffer();
Delay(300);Uart2_SendStr("AT+MIPLADDOBJ=0,3320,1,1,1,0\r\n"); // Battery Percentage 5700
Delay(300);
Clear_Buffer();
Delay(300);Uart2_SendStr("AT+MIPLOPEN=0,86400\r\n");//´ò¿ª
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 \r\n",atbuf);printf("µÃµ½¹Û²ìºÅ:objtnum = %s \r\n",objtnum);break;}else //Èç¹ûûÓÐÕÒµ½Ä¿±ê¾ÍÇå¿Õ·´À¡µÄÊý×飬ÕÒµ½Á˾ÍÌø³öÀ´{printf("Á¬½Ó·´À¡:%s\r\n",buf_uart2.buf);Clear_Buffer();}}}Clear_Buffer();printf("object number = %s\r\n",objtnum);memset(atstr,0,BUFLEN);sprintf(atstr,"AT+MIPLOBSERVERSP=0,%s,1\r\n",objtnum);Uart2_SendStr(atstr);//·¢Ë͹۲ìÃüÁîDelay(300);Clear_Buffer();if(strstr((const char*)atbuf,(const char*)"3303"))memcpy(objtnum1,objtnum,BUFLEN); // Temputureelse if(strstr((const char*)atbuf,(const char*)"3311"))memcpy(objtnum2,objtnum,BUFLEN); // Light Statuselse if(strstr((const char*)atbuf,(const char*)"3200"))memcpy(objtnum3,objtnum,BUFLEN); // AED device presenceelse 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");//·µ»ØOKmemset(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 \r\n",atbuf);printf("µÃµ½×ÊÔ´ºÅ:distnum = %s \r\n",distnum);break;}else //Èç¹ûûÓÐÕÒµ½Ä¿±ê¾ÍÇå¿Õ·´À¡µÄÊý×飬ÕÒµ½Á˾ÍÌø³öÀ´{Clear_Buffer();}}}Clear_Buffer(); printf("discover number = %s\r\n",distnum);memset(atstr,0,BUFLEN);if(strstr((const char*)atbuf,(const char*)"3303")) {sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,\"5700\"\r\n",distnum); // Temputurememcpy(distnum1,distnum,BUFLEN);}else if(strstr((const char*)atbuf,(const char*)"3311")){sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,\"5850\"\r\n",distnum); // Light Statusmemcpy(distnum2,distnum,BUFLEN);}else if(strstr((const char*)atbuf,(const char*)"3200")){sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,\"5500\"\r\n",distnum); // AED device presencememcpy(distnum3,objtnum,BUFLEN);}else if(strstr((const char*)atbuf,(const char*)"3320")){sprintf(atstr,"AT+MIPLDISCOVERRSP=0,%s,1,4,\"5700\"\r\n",distnum); // Battery memcpy(distnum4,objtnum,BUFLEN);}Uart2_SendStr(atstr);//·¢ËÍ×ÊÔ´ÃüÁîDelay(300);strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//·µ»ØOKwhile(strx==NULL){errcount++;strx=strstr((const char*)buf_uart2.buf,(const char*)"OK");//·µ»ØOKif(errcount>100) //·ÀÖ¹ËÀÑ»·{errcount = 0;break;}} Clear_Buffer();j++; }
}
完整工程:https://download.csdn.net/download/sinat_16873757/14950707
这篇关于NB-IOT通过LWM2M协议发送4个实例到ONENET的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!