|
a0a.1 32b0c
本帖最后由 Angel_YY 于 2018-4-22 12:47 编辑
上一篇 【LoRa模块_E32-TTL-100】+PC端代码实现
在上一篇,PC端代码实现之后,今天来实现节点端代码。
如图所示,节点端使用了STM32F031 Nucleo开发板,通过串口外接LoRa模块E32-TTL-100,此外连接了光照强度传感器,温湿度传感器DHT11。
STM32F031的开发板原理图如上图所示。其中PA9~PA12连接了LoRa模块,PB5~PB7连接DHT11,PB0连接光照传感器,PA3、PA4连接两路继电器。
在STM32CubeMX中进行管脚配置。
生成工程后,使用IAR开始编写代码。
其中主要代码文件main.c的内容如下:
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2018 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f0xx_hal.h"
- #include "adc.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- #include "hal_temp_hum.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- //定义继电器状态变量
- uint8_t ucWindRelayStat=0; //设置通风继电器默认为关闭
- uint8_t ucWarmRelayStat=0; //设置加温继电器默认为关闭
- uint8_t ucTemperature=0; //温度
- uint8_t ucHumidity=0; //湿度
- uint8_t ucLightValue=0; //光照
- uint16_t AD_Value = 0;
- // 定义接收和发送缓冲区
- uint8_t aRxBuffer[20];
- uint8_t aTxBuffer[20];
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_ADC_Init();
- MX_TIM17_Init();
- /* USER CODE BEGIN 2 */
- //初始化E32的M0和M1,进入透传模式
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11,GPIO_PIN_RESET);//设置E32配置引脚为低电平
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12,GPIO_PIN_RESET);//设置E32配置引脚为低电平
- //初始化DHT11的电源和地引脚,因为开发板电源和地不够用,这里使用GPIO代替
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7,GPIO_PIN_SET); //高电平给DHT11电源供电
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6,GPIO_PIN_RESET);//低电平接DHT11的GND
- //初始化LED,默认熄灭
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_RESET); //设置LED为高低平,熄灭LED
- //初始化继电器,默认关闭
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_RESET);
- //延时200毫秒
- HAL_Delay(200);
- //初始化DHT11
- DHT11_Init();
-
- // 开启串口接收中断
- HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 5);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- //获取温湿度
- DHT11_Read_Data(&ucTemperature, &ucHumidity);
- //获取光照强度
- HAL_ADC_Start(&hadc);//启动ADC装换
- HAL_ADC_PollForConversion(&hadc, 500);//等待转换完成,第二个参数表示超时时间,单位ms.
- if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc), HAL_ADC_STATE_REG_EOC))//判断转换完成标志位是否设置
- {
- AD_Value = HAL_ADC_GetValue(&hadc);//读取ADC转换数据,数据为12位右对齐,范围为0~2^12-1,即0~4095.
- ucLightValue=(uint8_t)(AD_Value>>4);//降低精度为8bit,数据范围0-255.
- }
- //组织要发送的串口数据
- aTxBuffer[0]=0x55;//目标地址高字节
- aTxBuffer[1]=0xaa;//目标地址低字节
- aTxBuffer[2]=0x17;//目标信道
- aTxBuffer[3]=0x01;//节点号
- aTxBuffer[4]=0x01;//网关号
- aTxBuffer[5]=ucTemperature;//温度
- aTxBuffer[6]=ucHumidity;//湿度
- aTxBuffer[7]=ucLightValue;//光照强度
- aTxBuffer[8]=0x00;//通风继电器和加热继电器状态清零
- aTxBuffer[9]=0x0d;//回车
- aTxBuffer[10]=0x0a;//换行
-
- //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_RESET);//LED灯关闭,只要有一个继电器打开,LED就会亮
- //设置继电器状态
- if(ucWindRelayStat>0)//如果通风继电器要求打开
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3,GPIO_PIN_SET);
- //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_SET);//led显示继电器状态
- aTxBuffer[8]|=0x01; //设置通风继电器状态
- }else{
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3,GPIO_PIN_RESET);
- }
- if(ucWarmRelayStat>0)//如果加热继电器要求打开
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_SET);
- //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_SET);//led显示继电器状态
- aTxBuffer[8]|=0x02;//设置加热继电器状态
- }else{
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4,GPIO_PIN_RESET);
- }
- //串口发送数据
- HAL_UART_Transmit(&huart1, (uint8_t *)aTxBuffer, 11,0xFFFF);
- HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
- //延时1秒
- HAL_Delay(1000);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI14;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSI14State = RCC_HSI14_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.HSI14CalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
- RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
- PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- // 实现串口中断回调函数
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(huart);
- // 功能代码,串口接收到数据后判断并设置继电器标记变量。
- if((aRxBuffer[0]==0x01)&&(aRxBuffer[1]==0x01))//如果网关和节点地址正确
- {
- if((aRxBuffer[2]&0x01)>0)//如果通风继电器要求打开
- {
- ucWindRelayStat=1;
- }else{
- ucWindRelayStat=0;
- }
- if((aRxBuffer[2]&0x02)>0)//如果加热继电器要求打开
- {
- ucWarmRelayStat=1;
- }else{
- ucWarmRelayStat=0;
- }
- }
- //开启下一次中断
- HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 5);
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码 代码中都做了详细的中文注释,这里不用在解释了。
通过测试,在节点端每隔一秒发送一次数据,在PC端接收数据后可以发送控制数据到节点端对节点继电器进行控制。节点端每发送一次数据LED灯点亮或者熄灭一次,同时在PC端代码同样是接收到节点端数据后会点亮或者熄灭一次LED灯进行提示。至此基于LoRa模块E32_TTL_100的智能农业蔬菜大棚监控系统的嵌入式代码部分的核心代码算是完成了,当然目前的代码只是实现了基本功能,距离商业化的产品还有很大差距。后期会持续跟踪需求进一步完善,朝着真正的产品持续演进。最后再次感谢社区,感谢破总给予这次LoRa模块的试用机会。
PS:本来计划做一个数据传输距离测试的,可是之前开发都是在电脑上使用USB数据线连接两块开发板,均能正常发送接收,临出门进行测试换上了两个充电宝,发现两个开发板的代码都不能正常工作了(LED灯不再闪烁),开始以为是功率不足,换到手机充电器上试试依然不行,基本可以肯定是usb的D+和D-检测不到信号,导致开发板工作异常,换到一个笔记本上,没有安装ST的驱动,发现依然无法工作,安装驱动后发现了虚拟串口,此时开发板就能正常工作了,所以判定STlink部分的代码会判断USB通讯正常,才会给STM32F031部分进行供电。于是自己把一颗USB线剪断,接了电源线直接到Nucleo外接Pin的电源管脚Vin和GND,Nucleo正常工作了。但是PC端的STM32F103最小系统板还是不能使用充电宝,我在找找原因,近期如果能解决,再补一个传输距离的测试。
节点端代码:
LoRaNode.zip
(14.51 MB, 下载次数: 270)
|
|