Initial commit
This commit is contained in:
commit
87e828962b
51 files changed
+34212
No files matched your search
@@ -0,0 +1,202 @@
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_http_client.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "http_client.h"
|
||||
|
||||
const char * TAG = "http_client.c";
|
||||
|
||||
static esp_err_t _http_event_handler(esp_http_client_event_t *evt)
|
||||
{
|
||||
switch (evt->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ERROR");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_CONNECTED");
|
||||
break;
|
||||
case HTTP_EVENT_HEADER_SENT:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_HEADER_SENT");
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
||||
if (!esp_http_client_is_chunked_response(evt->client)) {
|
||||
// Write out data
|
||||
printf("%.*s", evt->data_len, (char*)evt->data);
|
||||
}
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_ON_FINISH");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP_EVENT_DISCONNECTED");
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_http_client_config_t config = {
|
||||
.url = "http://tempcontroller.luethi.co/report",
|
||||
.event_handler = _http_event_handler,
|
||||
};
|
||||
|
||||
static void http_get_task(void *pvParameters) {
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
while (1) {
|
||||
// Perform the HTTP GET request
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// Wait for 1 second
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
static void send_message(const char* payload)
|
||||
{
|
||||
esp_http_client_handle_t client = esp_http_client_init(&config);
|
||||
|
||||
// Set the request headers and payload
|
||||
esp_http_client_set_method(client, HTTP_METHOD_POST);
|
||||
esp_http_client_set_header(client, "Content-Type", "application/json");
|
||||
esp_http_client_set_post_field(client, payload, strlen(payload));
|
||||
|
||||
// Perform the request
|
||||
esp_err_t err = esp_http_client_perform(client);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI("HTTP_EVENT", "HTTP POST Status = %d, content_length = %d",
|
||||
esp_http_client_get_status_code(client),
|
||||
esp_http_client_get_content_length(client));
|
||||
} else {
|
||||
ESP_LOGE("HTTP_EVENT", "HTTP POST request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
esp_http_client_cleanup(client);
|
||||
}
|
||||
|
||||
void hc_test()
|
||||
{
|
||||
xTaskCreate(&http_get_task, "http_get_task", 16384, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
void hc_send_sensor_data(int humidity, int temperature) {
|
||||
const char* msg = "{\"humidity\": 55,\"temperature\":25.6}";
|
||||
send_message(msg);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_event_loop.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_http_client.h"
|
||||
|
||||
#include <esp_err.h>
|
||||
#include "dht.h"
|
||||
|
||||
#include "sensor.h"
|
||||
#include "http_client.h"
|
||||
|
||||
#define DHT_GPIO 2 // D1 pin
|
||||
|
||||
void wifi_init(void) {
|
||||
tcpip_adapter_init();
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
wifi_config_t sta_config = {
|
||||
.sta = {
|
||||
.ssid = CONFIG_ESP_WIFI_SSID,
|
||||
.password = CONFIG_ESP_WIFI_PASSWORD,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config));
|
||||
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 Temperature: %d\n", humidity, temperature);
|
||||
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);
|
||||
}
|
||||
|
||||
void main_task(void* args) {
|
||||
while (1)
|
||||
{
|
||||
hc_send_sensor_data(50,25);
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
xTaskCreate(main_task, "main task", 2048, NULL, tskIDLE_PRIORITY, NULL);
|
||||
|
||||
// WIFI
|
||||
wifi_init();
|
||||
//hc_test();
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "sensor.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// Dummy implementations for the purpose of this example
|
||||
// Replace these with actual sensor initialization and data retrieval logic
|
||||
|
||||
// Function to initialize the sensor
|
||||
void initialize(void) {
|
||||
// Initialize the sensor hardware
|
||||
// For example, set up I2C communication, configure registers, etc.
|
||||
printf("Sensor initialized.\n");
|
||||
}
|
||||
|
||||
// Function to get the current humidity level
|
||||
// Returns the humidity as a percentage (0-100)
|
||||
float getHumidity(void) {
|
||||
// Read humidity data from the sensor
|
||||
// This is a dummy value for the purpose of this example
|
||||
float humidity = 50.0; // Replace with actual sensor reading
|
||||
printf("Humidity read: %.2f%%\n", humidity);
|
||||
return humidity;
|
||||
}
|
||||
|
||||
// Function to get the current temperature
|
||||
// Returns the temperature in degrees Celsius
|
||||
float getTemperature(void) {
|
||||
// Read temperature data from the sensor
|
||||
// This is a dummy value for the purpose of this example
|
||||
float temperature = 22.0; // Replace with actual sensor reading
|
||||
printf("Temperature read: %.2f°C\n", temperature);
|
||||
return temperature;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user