123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #include "iot_cloud.h"
- #include "iot_boardled.h"
- #include "iot_boardbutton.h"
- #include "iot_wifi.h"
- #include "iot_netcfg.h"
- #include "iot_sntp.h"
- #include "ohos_init.h"
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <cmsis_os2.h>
- #include <stdbool.h>
- #include "kv_store.h"
- #include "iot_schedule.h"
- #include "network_config_service.h"
- #include "iot_netcfg_nan.h"
- #include "hi_time.h"
- #include "hi_reset.h"
- #include "iot_gpio.h"
- #include "iot_gpio_ex.h"
- #define CONFIG_TASK_MAIN_STACKSIZE 0x2000
- #define CONFIG_TASK_MAIN_PRIOR 25
- #define DHT11_DQ_GPIO 13
-
- void GPIO_mode_out(void)
- {
- IoTGpioInit(DHT11_DQ_GPIO);
- IoTGpioSetFunc(DHT11_DQ_GPIO, IOT_GPIO_FUNC_GPIO_13_GPIO);
- IoTGpioSetDir(DHT11_DQ_GPIO, IOT_GPIO_DIR_OUT); // 设置为输出模式
- }
- void GPIO_mode_in(void)
- {
- IoTGpioInit(DHT11_DQ_GPIO);
- IoTGpioSetFunc(DHT11_DQ_GPIO, IOT_GPIO_FUNC_GPIO_13_GPIO);
- IoTGpioSetDir(DHT11_DQ_GPIO, IOT_GPIO_DIR_IN); // 设置为输入模式
- }
- static uint8_t DHT11_DQ_IN(void)
- {
- IotGpioValue temp = {0};
- IoTGpioGetInputVal(DHT11_DQ_GPIO,&temp);
- return temp;
- }
- //Reset DHT11
- void DHT11_Rst(void)
- {
- GPIO_mode_out(); //SET OUTPUT
- IoTGpioSetOutputVal(DHT11_DQ_GPIO, 0); //GPIOA.0=0
- printf("rst start\r \n");
- osDelay(2); //Pull down Least 18ms
- printf("rst down\r \n");
- IoTGpioSetOutputVal(DHT11_DQ_GPIO, 1); //GPIOA.0=1
-
- hi_udelay(30); //Pull up 20~40us
- printf("rst ok\r \n");
- }
- uint8_t DHT11_Check(void)
- {
- uint8_t retry=0;
- GPIO_mode_in();//SET INPUT
- while (DHT11_DQ_IN()&&retry<100)//DHT11 Pull down 40~80us
- {
- retry++;
- hi_udelay(1);
- };
- if(retry>=100)
- return 1;
- else
- retry=0;
- while (!DHT11_DQ_IN()&&retry<100)//DHT11 Pull up 40~80us
- {
- retry++;
- hi_udelay(1);
- };
- if(retry>=100)
- return 1;//chack error
- return 0;
- }
- uint8_t DHT11_Read_Bit(void)
- {
- uint8_t retry=0;
- while(DHT11_DQ_IN()&&retry<100)//wait become Low level
- {
- retry++;
- hi_udelay(1);
- }
- retry=0;
- while(!DHT11_DQ_IN()&&retry<100)//wait become High level
- {
- retry++;
- hi_udelay(1);
- }
- hi_udelay(50);//wait 40us
- if(DHT11_DQ_IN())
- return 1;
- else
- return 0;
- }
- uint8_t DHT11_Read_Byte(void)
- {
- uint8_t i,dat;
- dat=0;
- for (i=0;i<8;i++)
- {
- dat<<=1;
- dat|=DHT11_Read_Bit();
- }
- return dat;
- }
- uint8_t DHT11_Read_Data(uint8_t *temperature,uint8_t *humidity)
- {
- uint8_t buf[5];
- uint8_t i;
- DHT11_Rst();
- if(DHT11_Check()==0)
- {
- for(i=0;i<5;i++)
- {
- buf[i]=DHT11_Read_Byte();
- }
- if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
- {
- *humidity=buf[0];
- *temperature=buf[2];
- }
- }
- else
- return 1;
- return 0;
- }
- static void EntryMain(void)
- {
-
- int ret;
- uint8_t dht_temperature;
- uint8_t dht_humidity;
- printf("***********************DTH11 demo******************\r\n");
- while(1){
- DHT11_Read_Data(&dht_temperature,&dht_humidity);
- RaiseLog(LOG_LEVEL_INFO,"temperature=%d\r\n",dht_temperature);
- RaiseLog(LOG_LEVEL_INFO,"humidity=%d\r\n",dht_humidity);
- osDelay(1000);
- }
- }
- // static void EntryMain(void)
- // {
- // osThreadAttr_t attr;
- // RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __FUNCTION__, __DATE__, __TIME__);
- // // Create the IoT Main task
- // attr.attr_bits = 0U;
- // attr.cb_mem = NULL;
- // attr.cb_size = 0U;
- // attr.stack_mem = NULL;
- // attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE;
- // attr.priority = CONFIG_TASK_MAIN_PRIOR;
- // attr.name = "IoTMain";
- // (void) osThreadNew((osThreadFunc_t)IotMainTaskEntry, NULL, (const osThreadAttr_t *)&attr);
- // return;
- // }
- APP_FEATURE_INIT(EntryMain);
|