• Labview+STM32无线温湿度采集


    一.介绍

    该项目采用正点原子的STM32ZET6精英板+DHT11温湿度模块+泽耀科技的无线串口作为下位机,Labview+无线串口作为上位机读取下位机发来的数据并处理。

    泽耀科技的产品是我在开发过程中经常用到的,他们不仅产品做的非常不错,而且资料齐全售后也非常给力,后期有技术支持,大大减少开发难度。这个项目中就用到了泽耀科技提供的STM32的驱动代码,不仅有STM32的例程,还有STM8,51,arduino的都有,主流单片机的例程几乎全覆盖,非常友好!!!

     


    二.STM32代码讲解

    无线串口型号:AS32-TTL-1W

    无线串口波特率:115200

    工作模式:透传模式(发送端和接收端的模块地址和信道必须相同)

    这里dht11相关的就不多做赘述了,主要的问题就在与泽耀科技提供的例程中的发送数据的函数的第一个参数是发送数据的首地址,这个地方一开始没太注意,卡了好几天。所以在接收到无线串口发来的温湿度数据后不能在无线串口发送函数中处理数据。

    想了解dht11的可以参考我之前的文章:(326条消息) DHT11温湿度模块入门_LJX的博客-CSDN博客

    LoRa.c(选自泽耀科技例程)

    1. #include "LoRa.h"
    2. //串口发送函数
    3. void drv_uart_tx_bytes( uint8_t* TxBuffer, uint8_t Length )
    4. {
    5. while( Length-- )
    6. {
    7. while( RESET == USART_GetFlagStatus( UART_PORT, USART_FLAG_TXE ));
    8. UART_PORT->DR = *TxBuffer;
    9. TxBuffer++;
    10. }
    11. }
    12. //串口接收函数
    13. uint8_t drv_uart_rx_bytes( uint8_t* RxBuffer )
    14. {
    15. uint8_t l_RxLength = 0;
    16. uint16_t l_UartRxTimOut = 0x7FFF;
    17. while( l_UartRxTimOut-- )
    18. {
    19. if( RESET != USART_GetFlagStatus( UART_PORT, USART_FLAG_RXNE ))
    20. {
    21. *RxBuffer = (uint8_t)UART_PORT->DR;
    22. RxBuffer++;
    23. l_RxLength++;
    24. l_UartRxTimOut = 0x7FFF;
    25. }
    26. if( 100 == l_RxLength )
    27. {
    28. break;
    29. }
    30. }
    31. return l_RxLength;
    32. }


    DHT11.c

    1. #include "stm32f10x.h"
    2. #include "DHT11.h"c
    3. #include "delay.h"
    4. #include "stdio.h"
    5. #include "usart.h"
    6. #include "led.h"
    7. #include "LoRa.h"
    8. #include
    9. uint16_t Rxbuff[5]={0}; //接收温湿度数据
    10. uint8_t head[1] = {0xFF}; //帧头
    11. uint8_t tail[1] = {0xEE}; //帧尾
    12. uint8_t data2[1] = {0}; //接收到温湿度数据后进行处理
    13. uint8_t data3[1] = {0};
    14. uint8_t data4[1] = {0};
    15. uint8_t data5[1] = {0};
    16. void DHT11_GPIO_Init(void) //IO输出模式
    17. {
    18. GPIO_InitTypeDef GPIO_InitStructure;
    19. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
    20. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    21. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    22. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    23. GPIO_Init(GPIOB, &GPIO_InitStructure);
    24. GPIO_SetBits(GPIOB, GPIO_Pin_11);
    25. }
    26. void DHT11_GPIO_Init1(void) //IO输入模式
    27. {
    28. GPIO_InitTypeDef GPIO_InitStructure;
    29. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE);
    30. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    31. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    32. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    33. GPIO_Init(GPIOB, &GPIO_InitStructure);
    34. GPIO_SetBits(GPIOB, GPIO_Pin_11);
    35. }
    36. static uint8_t DHT11_Back()
    37. {
    38. uint8_t i =200;
    39. while(read_data && i--); //等待低电平到来
    40. i= 200;
    41. while(!read_data && i--); //等待高电平到来
    42. return 0;
    43. }
    44. void DHT11_Start(void)
    45. {
    46. data0;
    47. delay_ms(20);
    48. data1;
    49. delay_us(10);
    50. DHT11_GPIO_Init1();
    51. while(DHT11_Back());
    52. }
    53. void DHT11_ReceptionBuff(void)
    54. {
    55. uint8_t y=1;
    56. uint16_t i;
    57. uint8_t x;
    58. LED1 = 0;
    59. for(x=0;x<5;x++)
    60. {
    61. i=0;
    62. for(y=1;y<9;y++)
    63. {
    64. while(read_data)
    65. {
    66. __nop();
    67. }
    68. delay_us(40);
    69. while(!read_data)
    70. {
    71. __nop();
    72. }
    73. i= i<<1;
    74. delay_us(30);
    75. if(read_data)
    76. {
    77. i |=1;
    78. }
    79. while(read_data);
    80. }
    81. Rxbuff[x] =i;
    82. data2[0] = Rxbuff[0]/10 + 0x30; //处理接收到的数据并赋给要发送的数组
    83. data3[0] = Rxbuff[0]%10 + 0x30;
    84. data4[0] = Rxbuff[2]/10 + 0x30;
    85. data5[0] = Rxbuff[2]%10 + 0x30;
    86. }
    87. }
    88. void DHT11_Check(void) //检测DHT11是否存在
    89. {
    90. DHT11_GPIO_Init(); //切换成发送模式
    91. data1;
    92. data0;
    93. delay_ms(30);
    94. data1;
    95. delay_us(60); //在60us后读取DHT11状态,如果是低电平则模块存在
    96. DHT11_GPIO_Init1(); //切换成接收模式
    97. if(read_data == 0){
    98. LED0 = 0; //模块存在
    99. }
    100. }
    101. void DHT11_UpdateData(void)
    102. {
    103. DHT11_GPIO_Init();
    104. DHT11_Start();
    105. DHT11_ReceptionBuff();
    106. DHT11_LoRaSend();
    107. }
    108. void DHT11_LoRaSend(void)
    109. {
    110. drv_uart_tx_bytes((uint8_t*)head,1); //发送帧头
    111. drv_uart_tx_bytes((uint8_t*)data2,1); //发送湿度整数位
    112. drv_uart_tx_bytes((uint8_t*)data3,1);
    113. drv_uart_tx_bytes((uint8_t*)data4,1); //发送温度整数位
    114. drv_uart_tx_bytes((uint8_t*)data5,1);
    115. drv_uart_tx_bytes((uint8_t*)tail,1); //发送帧尾
    116. memset(Rxbuff,'\0',5);
    117. }

    我的main函数的参考意义不大,直接在例程上改的,对于想做这个项目的小伙伴可以多借鉴DHT11.c的代码。

    main.c

    1. #include "led.h"
    2. #include "delay.h"
    3. #include "sys.h"
    4. #include "usart.h"
    5. #include "DHT11.h"
    6. #include "LoRa.h"
    7. #define __AS62_TX_MODE__ //½«ÎÞÏß´®¿ÚÉèÖÃΪ·¢ËÍģʽ.ÆÁ±ÎÔòΪ½ÓÊÕģʽ
    8. #ifdef __AS62_TX_MODE__
    9. char *pAshining = "ashining";
    10. #else
    11. uint8_t g_ashining[ 8 ] ={ 'a', 's', 'h', 'i', 'n', 'i', 'n', 'g' };
    12. uint8_t g_As62_rx_buffer[ 100 ] = { 0 };
    13. uint8_t g_RxLength = 0;
    14. #endif
    15. int main(void)
    16. {
    17. initSysTick();
    18. usart_init();
    19. LED_Init();
    20. delay_ms(2000);
    21. while(1)
    22. {
    23. DHT11_UpdateData();
    24. delay_ms(2000); //小于2s数据无法传输
    25. }
    26. }

    三.Labview上位机

    这个温湿度的上位机大概流程就是打开串口,接收数据,判断帧头帧尾和数据长度,帧头帧尾和数据长度符合设定的要求后截取字符串,转化成数字数值。

    串口配置

    判断数据长度 

    处理数据:首先判断帧头帧尾是否和设定的一样,判断成功后把温度和湿度的数据分离开分别处理,由于数据是不断发送的(每隔两秒发送一次),所以要注意截取字符串的时候也需要动态地取截取,不然你只能收到一次数据。

    调用labview中的控件,最终得到可视化数据分析。

  • 相关阅读:
    AQS之排斥锁分析
    【Paper】2013_Attitude and Altitude Controller Design for Quad-Rotor Type MAVs
    OpenGL 绘制文本(QPainter)
    树莓派4b安装xenomai3(xenomai3 on raspberry4b)
    token
    TypeError: Cannot read properties of undefined (reading ‘NormalModule‘) 错误的解决方案
    拓端tecdat|R语言时间序列分解和异常检测方法应用案例
    Linux查看指定端口是否被占用
    Linux snap 软件包管理
    实验33:RFID门禁卡实验
  • 原文地址:https://blog.csdn.net/aaaaaaaa123345/article/details/127927594