A21_Agriculture_example.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "DHT11.h"
  19. #include "BH1750.h"
  20. #include "cmsis_os2.h"
  21. #include "ohos_init.h"
  22. #define TASK_STACK_SIZE 1024 * 8
  23. #define TASK_PRIO 25
  24. uint8_t fan_status;
  25. static void FanTask(void)
  26. {
  27. while (1)
  28. {
  29. if(fan_status == 1)
  30. FanStatusSet(ON,1);
  31. else
  32. {
  33. FanStatusSet(OFF,0);
  34. sleep(1);
  35. }
  36. }
  37. }
  38. static void ExampleTask(void)
  39. {
  40. int ret;
  41. uint8_t dht_temperature;
  42. uint8_t dht_humidity;
  43. float Lux;
  44. // BoardInit();
  45. //GPIO_0复用为I2C1_SDA
  46. ret = BoardInit();
  47. if (ret != 0) {
  48. printf("E53_SC1 Init failed!\r\n");
  49. return;
  50. }
  51. while (1)
  52. {
  53. DHT11_Read_Data(&dht_temperature,&dht_humidity);
  54. printf("temperature=%d \r\n",dht_temperature);
  55. printf("humidity=%d %%\r\n",dht_humidity);
  56. ret = BH1750ReadData(&Lux);
  57. if (ret != 0) {
  58. printf("E53_SC1 Read Data failed!\r\n");
  59. return;
  60. }
  61. printf("Lux data:%.2f\r\n", Lux);
  62. if (Lux < 20) {
  63. fan_status = 1;
  64. LightStatusSet(ON);
  65. } else {
  66. LightStatusSet(OFF);
  67. fan_status = 0;
  68. FanStatusSet(OFF,1);
  69. }
  70. sleep(3);
  71. }
  72. }
  73. static void ExampleEntry(void)
  74. {
  75. osThreadAttr_t attr;
  76. attr.name = "ExampleTask";
  77. attr.attr_bits = 0U;
  78. attr.cb_mem = NULL;
  79. attr.cb_size = 0U;
  80. attr.stack_mem = NULL;
  81. attr.stack_size = TASK_STACK_SIZE;
  82. attr.priority = TASK_PRIO;
  83. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  84. printf("Failed to create ExampleTask!\n");
  85. }
  86. attr.name = "FanTask";
  87. if (osThreadNew((osThreadFunc_t)FanTask, NULL, &attr) == NULL) {
  88. printf("Failed to create FanTask!\n");
  89. }
  90. }
  91. APP_FEATURE_INIT(ExampleEntry);