A07_WATHER_example.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include "cmsis_os2.h"
  19. #include "ohos_init.h"
  20. #include "WATHER.h"
  21. #define TASK_STACK_SIZE 1024 * 8
  22. #define TASK_PRIO 25
  23. BMP280_HandleTypedef bmp280;
  24. float pressure, temperature, asl,humidity;
  25. static void ExampleTask(void)
  26. {
  27. uint16_t dht_temperature;
  28. uint16_t dht_humidity;
  29. BoardInit();
  30. bmp280_init_default_params(&bmp280.params);
  31. while (!bmp280_init(&bmp280, &bmp280.params)) {
  32. printf("BMP280 initialization failed\n");
  33. }
  34. DHT21_Init();
  35. while (1)
  36. {
  37. printf("=======================================\r\n");
  38. printf("*************A07_WATHER_example***********\r\n");
  39. printf("=======================================\r\n");
  40. DHT21_onewire(&dht_temperature,&dht_humidity);
  41. humidity = (float)dht_humidity/10;
  42. if(humidity>80){
  43. BeepStatusSet(ON);
  44. }
  45. else{
  46. BeepStatusSet(OFF);
  47. }
  48. // printf("temperature=%d \r\n",dht_temperature);
  49. // printf("humidity=%d %%\r\n",dht_humidity);
  50. //读取温度、气压、海拔数据,学员自行补充
  51. while (!bmp280_read_float(&bmp280, &temperature, &pressure, &asl)) {
  52. printf("Temperature/pressure reading failed\n");
  53. }
  54. printf("Temperature: %.2f C\r\nHumidity: %.2f %%\r\nPressure: %.2f hPa\r\nAltiude: %.2f m\r\n",temperature,humidity, pressure,asl);
  55. sleep(3);
  56. }
  57. }
  58. static void ExampleEntry(void)
  59. {
  60. osThreadAttr_t attr;
  61. attr.name = "ExampleTask";
  62. attr.attr_bits = 0U;
  63. attr.cb_mem = NULL;
  64. attr.cb_size = 0U;
  65. attr.stack_mem = NULL;
  66. attr.stack_size = TASK_STACK_SIZE;
  67. attr.priority = TASK_PRIO;
  68. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  69. printf("Failed to create ExampleTask!\n");
  70. }
  71. }
  72. APP_FEATURE_INIT(ExampleEntry);