4950| 0
|
[2019参赛作品] 【脑洞大赛】基于NB-IoT的智慧路灯监控系统(硬件开发) |
本节将进行嵌入式硬件编程开发,通过电能检测模块实现实时采集路灯节点电压、电流、功率、总耗电量、功率因子、路灯开关状态、耗能产生二氧化碳、光照度、路灯亮度、环境光照度、GPS位置11种传感数据,通过NB-IoT模组实现对华为云物联网平台数据上传及下发获取,达到联动控制效果。 1、开发软件 嵌入式硬件开发使用STM32CubeMX和Keil5实现,STM32CubeMX搭建NUCLEO-L432KC开发板GPIO底层配置,Keil5实现NUCLEO-L432KC开发板编程开发。 STM32CubeMX 是 ST 意法半导体近几年来大力推荐的STM32 芯片图形化配置工具,也是配置和初始化 C 代码生成器(STM32 configuration and initialization C code generation),也就是自动生成开发初期关于芯片相关的一些初始化代码。STM32CubeMX 包含了 STM32 所有系列的芯片,包含示例和样本(Examples and demos)、中间组件(Middleware Components)、硬件抽象层(Hardwaree abstraction layer)。 STM32CubeMX简介、下载及安装参见:https://blog.csdn.net/m0_38106923/article/details/89458297 Keil C51是美国Keil Software公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上、结构性、可读性、可维护性上有明显的优势,因而易学易用。Keil提供了包括C编译器、宏汇编、链接器、库管理和一个功能强大的仿真调试器等在内的完整开发方案,通过一个集成开发环境(μVision)将这些部分组合在一起。运行Keil软件需要WIN98、NT、WIN2000、WINXP等操作系统。如果你使用C语言编程,那么Keil几乎就是你的不二之选,即使不使用C语言而仅用汇编语言编程,其方便易用的集成环境、强大的软件仿真调试工具也会令你事半功倍。 Keil5简介、下载、安装及破解参见:https://blog.csdn.net/m0_38106923/article/details/89492257 2、STM32CubeMX底层环境搭建 使用STM32CubeMX搭建NUCLEO-L432KC开发板GPIO底层配置,主要配置微控制器晶振、定时器、PWM接口、UART1、UART2、ADC接口和LED状态指示灯接口,具体实现如下: 3、Keil5编写控制模块实现代码 在Keil5开发环境中需要编写硬件基础底层配置功能、电能电耗检测功能、GPS定位功能、环境光照检测功能、NB-IoT模组通信功能和云端交互功能。 NB-IoT模组配置实现代码如下: [mw_shl_code=c,true]void BC20_LWM2M_Init() { HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QGNSSC=1\r\n",13,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QGNSSAGPS=1\r\n",16,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QLWSERV=\"49.4.85.232\",5683\r\n",31,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QLWCONF=\"862*************\"\r\n",30,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QLWADDOBJ=19,0,1,\"0\"\r\n",25,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QLWOPEN=0\r\n",14,0xff); HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QLWCFG=\"dataformat\",1,1\r\n",28,0xff); HAL_Delay(1000); memset(usart2_rec_buffer,0,usart2_rcv_len); usart2_rcv_len = 0; }[/mw_shl_code] 电能电耗监测功能实现代码如下: [mw_shl_code=c,true]void get_electric_energy() { int num=0; HAL_Delay(1000); char buf[8]={0x01,0x03,0x00,0x48,0x00,0x0A,0x45,0xDB}; HAL_UART_Transmit(&huart1,(uint8_t *)buf,8,0xff); HAL_Delay(2000); num=usart1_rec_buffer[3]*256+usart1_rec_buffer[4]; Sensor.Voltage[0]=num/10000+'0'; Sensor.Voltage[1]=num/1000%10+'0'; Sensor.Voltage[2]=num/100%10+'0'; Sensor.Voltage[3]='.'; Sensor.Voltage[4]=num/10%10+'0'; Sensor.Voltage[5]=num%10+'0'; num=0; num=usart1_rec_buffer[5]*256+usart1_rec_buffer[6]; Sensor.Electric_current[0]=num/100+'0'; Sensor.Electric_current[1]='.'; Sensor.Electric_current[2]=num/10%10+'0'; Sensor.Electric_current[3]=num%10+'0'; num=0; num=usart1_rec_buffer[7]*256+usart1_rec_buffer[8]; Sensor.Power[0]=num/10+'0'; Sensor.Power[1]=num%10+'0'; num=0; num=usart1_rec_buffer[9]*256*256*256+usart1_rec_buffer[10]*256*256+usart1_rec_buffer[11]*256+usart1_rec_buffer[12]; Sensor.Power_consumption[0]=num/100+'0'; Sensor.Power_consumption[1]='.'; Sensor.Power_consumption[2]=num/10%10+'0'; Sensor.Power_consumption[3]=num%10+'0'; num=0; num=usart1_rec_buffer[13]*256+usart1_rec_buffer[14]; Sensor.Power_factor[0]='0'; Sensor.Power_factor[1]='.'; Sensor.Power_factor[2]=num/100+'0'; Sensor.Power_factor[3]=num/10%10+'0'; Sensor.Power_factor[4]=num%10+'0'; num=0; Sensor.LED_flag[0]=usart1_rec_buffer[16]+'0'; num=usart1_rec_buffer[17]*256*256*256+usart1_rec_buffer[18]*256*256+usart1_rec_buffer[19]*256+usart1_rec_buffer[20]; Sensor.Carbon_dioxide[0]=num/1000+'0'; Sensor.Carbon_dioxide[1]='.'; Sensor.Carbon_dioxide[2]=num/100%10+'0'; Sensor.Carbon_dioxide[3]=num/10%10+'0'; Sensor.Carbon_dioxide[4]=num%10+'0'; num=0; num=usart1_rec_buffer[21]*256+usart1_rec_buffer[22]; Sensor.Frequency[0]=num/1000+'0'; Sensor.Frequency[1]=num/100%10+'0'; Sensor.Frequency[2]='.'; Sensor.Frequency[3]=num/10%10+'0'; Sensor.Frequency[4]=num%10+'0'; num=0; memset(usart1_rec_buffer,0,usart1_rcv_len); usart1_rcv_len = 0; }[/mw_shl_code] GPS定位功能实现代码如下: [mw_shl_code=c,true]void get_gps() { HAL_Delay(1000); HAL_UART_Transmit(&huart2,(uint8_t *)"AT+QGNSSRD=\"NMEA/RMC\"\r\n",23,0xff); HAL_Delay(2000); if((usart2_rec_buffer[2]=='+')&&(usart2_rec_buffer[3]=='Q')&&(usart2_rec_buffer[4]=='G')) { Sensor.Latitude[0]=usart2_rec_buffer[31]; Sensor.Latitude[1]=usart2_rec_buffer[32]; Sensor.Latitude[2]='.'; Sensor.Latitude[3]=usart2_rec_buffer[33]; Sensor.Latitude[4]=usart2_rec_buffer[34]; Sensor.Latitude[5]=usart2_rec_buffer[36]; Sensor.Latitude[6]=usart2_rec_buffer[37]; Sensor.Latitude[7]=usart2_rec_buffer[38]; Sensor.Latitude[8]=usart2_rec_buffer[39]; Sensor.Longitude[0]=usart2_rec_buffer[43]; Sensor.Longitude[1]=usart2_rec_buffer[44]; Sensor.Longitude[2]=usart2_rec_buffer[45]; Sensor.Longitude[3]='.'; Sensor.Longitude[4]=usart2_rec_buffer[46]; Sensor.Longitude[5]=usart2_rec_buffer[47]; Sensor.Longitude[6]=usart2_rec_buffer[49]; Sensor.Longitude[7]=usart2_rec_buffer[50]; Sensor.Longitude[8]=usart2_rec_buffer[51]; Sensor.Longitude[9]=usart2_rec_buffer[52]; } memset(usart2_rec_buffer,0,usart2_rcv_len); usart2_rcv_len = 0; }[/mw_shl_code] [mw_shl_code=c,true]void get_illumination() { int ADC_value=0,num=0; float voltage=0; HAL_Delay(1000); HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1,100); ADC_value = HAL_ADC_GetValue(&hadc1); voltage = (float)(ADC_value *3.3/4096); voltage*=100; num=100-(int)(voltage*100/329); Sensor.Illumination[0]=num/100+'0'; Sensor.Illumination[1]=num/10%10+'0'; Sensor.Illumination[2]=num%10+'0'; ´ num=100-num; Sensor.LED_brightness[0]=num/100+'0'; Sensor.LED_brightness[1]=num/10%10+'0'; Sensor.LED_brightness[2]=num%10+'0'; LED_brightness_flag=num*21; }[/mw_shl_code] [mw_shl_code=c,true]void BC20_LWM2M_Send() { char send_buf[100],bufout[100]; char *bufin=(char*)&Sensor; for (int i = 0; i < strlen(bufin); i++) { sprintf(bufout+i*2, "%02X",bufin); HAL_Delay(20); } sprintf((char *)send_buf, "AT+QLWDATASEND=19,0,0,%d,%s,0x0100\r\n",strlen(bufin),bufout); HAL_UART_Transmit(&huart2,(uint8_t *)send_buf,strlen((const char*)send_buf),0xff); //·¢ËÍÊý¾Ý memset(bufin,0,strlen(bufin)); memset(bufout,0,strlen(bufout)); memset(send_buf,0,strlen(send_buf)); memset(usart2_rec_buffer,0,usart2_rcv_len); usart2_rcv_len = 0; }[/mw_shl_code] [mw_shl_code=c,true]int num=0; char flag=0; void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { char led_off[8]={0x01,0x05,0x00,0x00,0x00,0x00,0xCD,0xCA}; char led_on[8]={0x01,0x05,0x00,0x00,0xFF,0x00,0x8C,0x3A}; if(htim->Instance == TIM1) { if(num%500==0) { if(usart1_rcv_len>=1) { HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_3); } if(usart2_rcv_len>=1) { if((usart2_rec_buffer[2]=='+')&&(usart2_rec_buffer[3]=='Q')&&(usart2_rec_buffer[4]=='L')) { if(usart2_rec_buffer[26]=='A') { flag=0; if(usart2_rec_buffer[27]=='1') { HAL_UART_Transmit(&huart1,(uint8_t *)led_on,8,0xff); } else if(usart2_rec_buffer[27]=='0') { HAL_UART_Transmit(&huart1,(uint8_t *)led_off,8,0xff); } } else if(usart2_rec_buffer[26]=='B') { if(usart2_rec_buffer[27]=='1') { flag=1; } else if(usart2_rec_buffer[27]=='0') { flag=0; } } memset(usart2_rec_buffer,0,usart2_rcv_len); usart2_rcv_len = 0; } HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_3); } if(flag==1) { __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, LED_brightness_flag); } num=0; } num++; } }[/mw_shl_code] 【脑洞大赛】基于NB-IoT的智慧路灯监控系统(云端部署及训练):https://mc.dfrobot.com.cn/thread-296711-1-1.html |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed