modified: Makefile

deleted:    main/include/dht.h
new file:   main/include/leds.h
modified:   main/include/sensor.h
deleted:    main/src/dht.c
new file:   main/src/leds.c
modified:   main/src/main.c
modified:   main/src/sensor.c
This commit is contained in:
Sven Luethi committed 2024-08-01 23:32:24 +02:00
1 parent ccc6e72ea7
commit 9bd0883aeb
8 files changed
+66 -346

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@
# project subdirectory.
#
PROJECT_NAME := wifi_station
PROJECT_NAME := dc_sensor
include $(IDF_PATH)/make/project.mk
-77
View File
@@ -1,77 +0,0 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <driver/gpio.h>
#include <esp_err.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Sensor type
*/
typedef enum
{
DHT_TYPE_DHT11 = 0, //!< DHT11
DHT_TYPE_DHT22, //!< DHT22
DHT_TYPE_SI7021 //!< Itead SI7021
} dht_sensor_type_t;
/**
* Initialize Config dht pin to be read on specified pin
* If you are usingPull up resistor
*/
/**
* @brief Initialize Config dht pin to be read on specified pin
*
* @param pin GPIO number of dht sensor
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_FAIL Init error
*/
esp_err_t dht_init(gpio_num_t pin, bool pull_up);
/**
* @brief Read data from sensor on specified pin. dht_init must be called before reading it.
* Humidity and temperature is returned as integers.
* For example: humidity=625 is 62.5 %
* temperature=244 is 24.4 degrees Celsius
*
* @param sensor_type
* @param pin GPIO number of dht sensor
* @param humidity output humidity
* @param temperature output temperature
*
* @return
* - ESP_OK Success
* - ESP_FAIL Init error
*/
esp_err_t dht_read_data(dht_sensor_type_t sensor_type, gpio_num_t pin, int16_t *humidity, int16_t *temperature);
/**
* @brief Float version of dht_read_data. Read data from sensor on specified pin. dht_init must be called before reading it.
* Humidity and temperature is returned as float.
* For example: humidity=62.5 is 62.5 %
* temperature=24.4 is 24.4 degrees Celsius
*
* @param sensor_type
* @param pin GPIO number of dht sensor
* @param humidity output humidity
* @param temperature output temperature
*
* @return
* - ESP_OK Success
* - ESP_FAIL Init error
*/
esp_err_t dht_read_float_data(dht_sensor_type_t sensor_type, gpio_num_t pin, float *humidity, float *temperature);
#ifdef __cplusplus
}
#endif
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define LED_INT GPIO_NUM_2
#define LED1 GPIO_NUM_4
#define LED2 GPIO_NUM_5
void led_blink_task(void *pvParameter);
unsigned initLEDs();
#ifdef __cplusplus
}
#endif
+1 -1
View File
@@ -6,7 +6,7 @@ extern "C" {
#endif
// Function to initialize the sensor
void initialize(void);
void initSensor(void);
// Function to get the current humidity level
// Returns the humidity as a percentage (0-100)
-202
View File
@@ -1,202 +0,0 @@
#include "dht.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
#include <driver/gpio.h>
#include <rom/ets_sys.h> // os_delay_us
// DHT timer precision in microseconds
#define DHT_TIMER_INTERVAL 2
#define DHT_DATA_BITS 40
// #define DEBUG_DHT
#ifdef DEBUG_DHT
#define debug(fmt, ...) printf("%s" fmt "\n", "dht: ", ## __VA_ARGS__);
#else
#define debug(fmt, ...) /* (do nothing) */
#endif
/*
* Note:
* A suitable pull-up resistor should be connected to the selected GPIO line
*
* __ ______ _______ ___________________________
* \ A / \ C / \ DHT duration_data_low / \
* \_______/ B \______/ D \__________________________/ DHT duration_data_high \__
*
*
* Initializing communications with the DHT requires four 'phases' as follows:
*
* Phase A - MCU pulls signal low for at least 18000 us
* Phase B - MCU allows signal to float back up and waits 20-40us for DHT to pull it low
* Phase C - DHT pulls signal low for ~80us
* Phase D - DHT lets signal float back up for ~80us
*
* After this, the DHT transmits its first bit by holding the signal low for 50us
* and then letting it float back high for a period of time that depends on the data bit.
* duration_data_high is shorter than 50us for a logic '0' and longer than 50us for logic '1'.
*
* There are a total of 40 data bits transmitted sequentially. These bits are read into a byte array
* of length 5. The first and third bytes are humidity (%) and temperature (C), respectively. Bytes 2 and 4
* are zero-filled and the fifth is a checksum such that:
*
* byte_5 == (byte_1 + byte_2 + byte_3 + btye_4) & 0xFF
*
*/
/**
* Wait specified time for pin to go to a specified state.
* If timeout is reached and pin doesn't go to a requested state
* false is returned.
* The elapsed time is returned in pointer 'duration' if it is not NULL.
*/
static bool dht_await_pin_state(uint8_t pin, uint32_t timeout,
bool expected_pin_state, uint32_t *duration)
{
for (uint32_t i = 0; i < timeout; i += DHT_TIMER_INTERVAL) {
// need to wait at least a single interval to prevent reading a jitter
os_delay_us(DHT_TIMER_INTERVAL);
if (gpio_get_level(pin) == expected_pin_state) {
if (duration) {
*duration = i;
}
return true;
}
}
return false;
}
/**
* Request data from DHT and read raw bit stream.
* The function call should be protected from task switching.
* Return false if error occurred.
*/
static inline bool dht_fetch_data(dht_sensor_type_t sensor_type, uint8_t pin, bool bits[DHT_DATA_BITS])
{
uint32_t low_duration;
uint32_t high_duration;
// Phase 'A' pulling signal low to initiate read sequence
gpio_set_level(pin, 0);
os_delay_us(sensor_type == DHT_TYPE_SI7021 ? 500 : 20000);
gpio_set_level(pin, 1);
// Step through Phase 'B', 40us
if (!dht_await_pin_state(pin, 40, false, NULL)) {
debug("Initialization error, problem in phase 'B'\n");
return false;
}
// Step through Phase 'C', 88us
if (!dht_await_pin_state(pin, 88, true, NULL)) {
debug("Initialization error, problem in phase 'C'\n");
return false;
}
// Step through Phase 'D', 88us
if (!dht_await_pin_state(pin, 88, false, NULL)) {
debug("Initialization error, problem in phase 'D'\n");
return false;
}
// Read in each of the 40 bits of data...
for (int i = 0; i < DHT_DATA_BITS; i++) {
if (!dht_await_pin_state(pin, 65, true, &low_duration)) {
debug("LOW bit timeout\n");
return false;
}
if (!dht_await_pin_state(pin, 75, false, &high_duration)) {
debug("HIGHT bit timeout\n");
return false;
}
bits[i] = high_duration > low_duration;
}
return true;
}
/**
* Pack two data bytes into single value and take into account sign bit.
*/
static inline int16_t dht_convert_data(dht_sensor_type_t sensor_type, uint8_t msb, uint8_t lsb)
{
int16_t data;
if (sensor_type == DHT_TYPE_DHT22) {
data = msb & 0x7F;
data <<= 8;
data |= lsb;
if (msb & BIT(7)) {
data = 0 - data; // convert it to negative
}
}
else {
data = msb * 10;
}
return data;
}
esp_err_t dht_read_data(dht_sensor_type_t sensor_type, gpio_num_t pin, int16_t *humidity, int16_t *temperature)
{
bool bits[DHT_DATA_BITS];
uint8_t data[DHT_DATA_BITS/8] = {0};
bool result;
taskENTER_CRITICAL();
result = dht_fetch_data(sensor_type, pin, bits);
taskEXIT_CRITICAL();
if (!result) {
return ESP_FAIL;
}
for (uint8_t i = 0; i < DHT_DATA_BITS; i++) {
// Read each bit into 'result' byte array...
data[i/8] <<= 1;
data[i/8] |= bits[i];
}
if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
debug("Checksum failed, invalid data received from sensor\n");
return ESP_FAIL;
}
*humidity = dht_convert_data(sensor_type, data[0], data[1]);
*temperature = dht_convert_data(sensor_type, data[2], data[3]);
debug("Sensor data: humidity=%d, temp=%d\n", *humidity, *temperature);
return ESP_OK;
}
esp_err_t dht_read_float_data(dht_sensor_type_t sensor_type, gpio_num_t pin, float *humidity, float *temperature)
{
int16_t i_humidity, i_temp;
if (dht_read_data(sensor_type, pin, &i_humidity, &i_temp) == ESP_OK) {
*humidity = (float)i_humidity / 10;
*temperature = (float)i_temp / 10;
return ESP_OK;
}
return ESP_FAIL;
}
esp_err_t dht_init(gpio_num_t pin, bool pull_up) {
gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT_OD,
.pin_bit_mask = 1ULL << pin,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = pull_up ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE
};
esp_err_t result = gpio_config(&io_conf);
if (result == ESP_OK) {
gpio_set_level(pin, 1);
return ESP_OK;
}
return result;
}
+40
View File
@@ -0,0 +1,40 @@
#include "leds.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "esp_log.h"
static const char * TAG = "leds.c";
void led_blink_task(void *pvParameter)
{
initLEDs();
while (1) {
ESP_LOGI(TAG, "Blink LEDs...");
// Blink the LEDs
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 0);
//gpio_set_level(LED_INT, 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 1);
//gpio_set_level(LED_INT, 0);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1);
gpio_set_level(LED2, 0);
//gpio_set_level(LED_INT, 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1);
gpio_set_level(LED2, 1);
//gpio_set_level(LED_INT, 0);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
unsigned initLEDs() {
gpio_set_direction(LED1,GPIO_MODE_OUTPUT);
gpio_set_direction(LED2,GPIO_MODE_OUTPUT);
//gpio_set_direction(LED_INT,GPIO_MODE_OUTPUT);
return 0;
}
+4 -65
View File
@@ -9,15 +9,14 @@
#include <esp_err.h>
#include "dht.h"
#include "leds.h"
#include "sensor.h"
#include "http_client.h"
#define DHT_GPIO 2 // D1 pin
const char * TAG = "main.c";
static const char * TAG = "main.c";
void wifi_init(void) {
static void wifi_init(void) {
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
@@ -33,63 +32,12 @@ void wifi_init(void) {
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
}
/*
void temperature_task(void *arg)
{
ESP_ERROR_CHECK(dht_init(DHT_GPIO, false));
vTaskDelay(2000 / portTICK_PERIOD_MS);
while (1)
{
int16_t humidity = 0;
int16_t temperature = 0;
if (dht_read_data(DHT_TYPE_DHT22, DHT_GPIO, &humidity, &temperature) == ESP_OK) {
// e.g. in dht22, 604 = 60.4%, 252 = 25.2 C
// If you want to print float data, you should run `make menuconfig`
// to enable full newlib and call dht_read_float_data() here instead
printf("Humidity: %d.%d%% Temperature: %d.%d\n", humidity/10, humidity%10, temperature/10,temperature%10);
} else {
printf("Fail to get dht temperature data\n");
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
*/
#include "driver/gpio.h"
#define LED1 GPIO_NUM_4
#define LED2 GPIO_NUM_5
void blink_task(void *pvParameter)
{
gpio_set_direction(LED1,GPIO_MODE_OUTPUT);
gpio_set_direction(LED2,GPIO_MODE_OUTPUT);
while (1) {
ESP_LOGI(TAG, "Blink LEDs...");
// Blink the LEDs
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 0);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1);
gpio_set_level(LED2, 0);
vTaskDelay(500 / portTICK_PERIOD_MS);
gpio_set_level(LED1, 1);
gpio_set_level(LED2, 1);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
void main_task(void* args) {
float humidity = 0;
float temperature = 0;
ESP_ERROR_CHECK(dht_init(DHT_GPIO, false));
vTaskDelay(2000 / portTICK_PERIOD_MS);
//Register device with Server
unsigned id;
@@ -97,15 +45,6 @@ void main_task(void* args) {
while (1)
{
hc_send_sensor_data(id,0,0); //test
/*
if (dht_read_float_data(DHT_TYPE_DHT22, DHT_GPIO, &humidity, &temperature) == ESP_OK) {
hc_send_sensor_data(id,humidity,temperature);
}
else {
printf("Fail to get dht temperature data\n");
}
vTaskDelay(5000 / portTICK_PERIOD_MS);
*/
}
}
@@ -114,6 +53,6 @@ void app_main()
//ESP_ERROR_CHECK(nvs_flash_init());
//wifi_init();
xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
xTaskCreate(&led_blink_task, "led_blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
//xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
}
+4
View File
@@ -1,5 +1,9 @@
#include "sensor.h"
#include <stdio.h>
#include "i2c_master.h"
#define I2C_SDA_PIN GPIO_NUM_2
#define I2C_CLK_PIN GPIO_NUM_14
// Dummy implementations for the purpose of this example
// Replace these with actual sensor initialization and data retrieval logic