main_entry.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "iot_cloud.h"
  2. #include "iot_boardled.h"
  3. #include "iot_boardbutton.h"
  4. #include "iot_wifi.h"
  5. #include "iot_netcfg.h"
  6. #include "iot_sntp.h"
  7. #include "ohos_init.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <cmsis_os2.h>
  14. #include <stdbool.h>
  15. #include "kv_store.h"
  16. #include "iot_schedule.h"
  17. #include "network_config_service.h"
  18. #include "iot_netcfg_nan.h"
  19. #include "hi_time.h"
  20. #include "hi_reset.h"
  21. #include "iot_gpio.h"
  22. #include "iot_gpio_ex.h"
  23. #define CONFIG_TASK_MAIN_STACKSIZE 0x2000
  24. #define CONFIG_TASK_MAIN_PRIOR 25
  25. #define DHT11_DQ_GPIO 13
  26. void GPIO_mode_out(void)
  27. {
  28. IoTGpioInit(DHT11_DQ_GPIO);
  29. IoTGpioSetFunc(DHT11_DQ_GPIO, IOT_GPIO_FUNC_GPIO_13_GPIO);
  30. IoTGpioSetDir(DHT11_DQ_GPIO, IOT_GPIO_DIR_OUT); // 设置为输出模式
  31. }
  32. void GPIO_mode_in(void)
  33. {
  34. IoTGpioInit(DHT11_DQ_GPIO);
  35. IoTGpioSetFunc(DHT11_DQ_GPIO, IOT_GPIO_FUNC_GPIO_13_GPIO);
  36. IoTGpioSetDir(DHT11_DQ_GPIO, IOT_GPIO_DIR_IN); // 设置为输入模式
  37. }
  38. static uint8_t DHT11_DQ_IN(void)
  39. {
  40. IotGpioValue temp = {0};
  41. IoTGpioGetInputVal(DHT11_DQ_GPIO,&temp);
  42. return temp;
  43. }
  44. //Reset DHT11
  45. void DHT11_Rst(void)
  46. {
  47. GPIO_mode_out(); //SET OUTPUT
  48. IoTGpioSetOutputVal(DHT11_DQ_GPIO, 0); //GPIOA.0=0
  49. printf("rst start\r \n");
  50. osDelay(2); //Pull down Least 18ms
  51. printf("rst down\r \n");
  52. IoTGpioSetOutputVal(DHT11_DQ_GPIO, 1); //GPIOA.0=1
  53. hi_udelay(30); //Pull up 20~40us
  54. printf("rst ok\r \n");
  55. }
  56. uint8_t DHT11_Check(void)
  57. {
  58. uint8_t retry=0;
  59. GPIO_mode_in();//SET INPUT
  60. while (DHT11_DQ_IN()&&retry<100)//DHT11 Pull down 40~80us
  61. {
  62. retry++;
  63. hi_udelay(1);
  64. };
  65. if(retry>=100)
  66. return 1;
  67. else
  68. retry=0;
  69. while (!DHT11_DQ_IN()&&retry<100)//DHT11 Pull up 40~80us
  70. {
  71. retry++;
  72. hi_udelay(1);
  73. };
  74. if(retry>=100)
  75. return 1;//chack error
  76. return 0;
  77. }
  78. uint8_t DHT11_Read_Bit(void)
  79. {
  80. uint8_t retry=0;
  81. while(DHT11_DQ_IN()&&retry<100)//wait become Low level
  82. {
  83. retry++;
  84. hi_udelay(1);
  85. }
  86. retry=0;
  87. while(!DHT11_DQ_IN()&&retry<100)//wait become High level
  88. {
  89. retry++;
  90. hi_udelay(1);
  91. }
  92. hi_udelay(50);//wait 40us
  93. if(DHT11_DQ_IN())
  94. return 1;
  95. else
  96. return 0;
  97. }
  98. uint8_t DHT11_Read_Byte(void)
  99. {
  100. uint8_t i,dat;
  101. dat=0;
  102. for (i=0;i<8;i++)
  103. {
  104. dat<<=1;
  105. dat|=DHT11_Read_Bit();
  106. }
  107. return dat;
  108. }
  109. uint8_t DHT11_Read_Data(uint8_t *temperature,uint8_t *humidity)
  110. {
  111. uint8_t buf[5];
  112. uint8_t i;
  113. DHT11_Rst();
  114. if(DHT11_Check()==0)
  115. {
  116. for(i=0;i<5;i++)
  117. {
  118. buf[i]=DHT11_Read_Byte();
  119. }
  120. if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
  121. {
  122. *humidity=buf[0];
  123. *temperature=buf[2];
  124. }
  125. }
  126. else
  127. return 1;
  128. return 0;
  129. }
  130. static void EntryMain(void)
  131. {
  132. int ret;
  133. uint8_t dht_temperature;
  134. uint8_t dht_humidity;
  135. printf("***********************DTH11 demo******************\r\n");
  136. while(1){
  137. DHT11_Read_Data(&dht_temperature,&dht_humidity);
  138. RaiseLog(LOG_LEVEL_INFO,"temperature=%d\r\n",dht_temperature);
  139. RaiseLog(LOG_LEVEL_INFO,"humidity=%d\r\n",dht_humidity);
  140. osDelay(1000);
  141. }
  142. }
  143. // static void EntryMain(void)
  144. // {
  145. // osThreadAttr_t attr;
  146. // RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __FUNCTION__, __DATE__, __TIME__);
  147. // // Create the IoT Main task
  148. // attr.attr_bits = 0U;
  149. // attr.cb_mem = NULL;
  150. // attr.cb_size = 0U;
  151. // attr.stack_mem = NULL;
  152. // attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE;
  153. // attr.priority = CONFIG_TASK_MAIN_PRIOR;
  154. // attr.name = "IoTMain";
  155. // (void) osThreadNew((osThreadFunc_t)IotMainTaskEntry, NULL, (const osThreadAttr_t *)&attr);
  156. // return;
  157. // }
  158. APP_FEATURE_INIT(EntryMain);