【脑洞大赛】基于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)。
https://img-blog.csdnimg.cn/20190422184705863.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzM4MTA2OTIz,size_16,color_FFFFFF,t_70 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模组配置实现代码如下: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;
} 电能电耗监测功能实现代码如下:void get_electric_energy()
{
int num=0;
HAL_Delay(1000);
char buf={0x01,0x03,0x00,0x48,0x00,0x0A,0x45,0xDB};
HAL_UART_Transmit(&huart1,(uint8_t *)buf,8,0xff);
HAL_Delay(2000);
num=usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Voltage=num/10000+'0';
Sensor.Voltage=num/1000%10+'0';
Sensor.Voltage=num/100%10+'0';
Sensor.Voltage='.';
Sensor.Voltage=num/10%10+'0';
Sensor.Voltage=num%10+'0';
num=0;
num=usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Electric_current=num/100+'0';
Sensor.Electric_current='.';
Sensor.Electric_current=num/10%10+'0';
Sensor.Electric_current=num%10+'0';
num=0;
num=usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Power=num/10+'0';
Sensor.Power=num%10+'0';
num=0;
num=usart1_rec_buffer*256*256*256+usart1_rec_buffer*256*256+usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Power_consumption=num/100+'0';
Sensor.Power_consumption='.';
Sensor.Power_consumption=num/10%10+'0';
Sensor.Power_consumption=num%10+'0';
num=0;
num=usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Power_factor='0';
Sensor.Power_factor='.';
Sensor.Power_factor=num/100+'0';
Sensor.Power_factor=num/10%10+'0';
Sensor.Power_factor=num%10+'0';
num=0;
Sensor.LED_flag=usart1_rec_buffer+'0';
num=usart1_rec_buffer*256*256*256+usart1_rec_buffer*256*256+usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Carbon_dioxide=num/1000+'0';
Sensor.Carbon_dioxide='.';
Sensor.Carbon_dioxide=num/100%10+'0';
Sensor.Carbon_dioxide=num/10%10+'0';
Sensor.Carbon_dioxide=num%10+'0';
num=0;
num=usart1_rec_buffer*256+usart1_rec_buffer;
Sensor.Frequency=num/1000+'0';
Sensor.Frequency=num/100%10+'0';
Sensor.Frequency='.';
Sensor.Frequency=num/10%10+'0';
Sensor.Frequency=num%10+'0';
num=0;
memset(usart1_rec_buffer,0,usart1_rcv_len);
usart1_rcv_len = 0;
} GPS定位功能实现代码如下: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=='+')&&(usart2_rec_buffer=='Q')&&(usart2_rec_buffer=='G'))
{
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude='.';
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Latitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude='.';
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
Sensor.Longitude=usart2_rec_buffer;
}
memset(usart2_rec_buffer,0,usart2_rcv_len);
usart2_rcv_len = 0;
} 环境光照检测功能实现代码如下: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=num/100+'0';
Sensor.Illumination=num/10%10+'0';
Sensor.Illumination=num%10+'0'; ´
num=100-num;
Sensor.LED_brightness=num/100+'0';
Sensor.LED_brightness=num/10%10+'0';
Sensor.LED_brightness=num%10+'0';
LED_brightness_flag=num*21;
} NB-IoT模组模组通信功能实现代码如下:
void BC20_LWM2M_Send()
{
char send_buf,bufout;
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;
} 云端交互功能实现代码如下:
int num=0;
char flag=0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
char led_off={0x01,0x05,0x00,0x00,0x00,0x00,0xCD,0xCA};
char led_on={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=='+')&&(usart2_rec_buffer=='Q')&&(usart2_rec_buffer=='L'))
{
if(usart2_rec_buffer=='A')
{
flag=0;
if(usart2_rec_buffer=='1')
{
HAL_UART_Transmit(&huart1,(uint8_t *)led_on,8,0xff);
}
else if(usart2_rec_buffer=='0')
{
HAL_UART_Transmit(&huart1,(uint8_t *)led_off,8,0xff);
}
}
else if(usart2_rec_buffer=='B')
{
if(usart2_rec_buffer=='1')
{
flag=1;
}
else if(usart2_rec_buffer=='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++;
}
} 【脑洞大赛】基于NB-IoT的智慧路灯监控系统(云端部署及训练):https://mc.dfrobot.com.cn/thread-296711-1-1.html
页:
[1]