58 lines
1.4 KiB
C
58 lines
1.4 KiB
C
#include "HD44780.h"
|
|
#include "dht.h"
|
|
#include <esp_err.h>
|
|
#include <esp_log.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
|
|
#define DHT_PIN 25
|
|
|
|
#define LCD_ADDR 0x27
|
|
#define SDA_PIN 21
|
|
#define SCL_PIN 22
|
|
#define LCD_ROWS 16
|
|
#define LCD_COLS 2
|
|
|
|
float convertValue(int16_t val);
|
|
float converTemp(int16_t celsius);
|
|
|
|
void app_main(void) {
|
|
|
|
const char *tag = "TEMP_HUMID";
|
|
int16_t temp, humid;
|
|
char tempStr[32], humidStr[32];
|
|
ESP_LOGI(tag, "Starting our temp/humidity readings");
|
|
LCD_init(LCD_ADDR, SDA_PIN, SCL_PIN, LCD_COLS, LCD_ROWS);
|
|
|
|
while (1) {
|
|
LCD_clearScreen();
|
|
|
|
esp_err_t err = dht_read_data(DHT_TYPE_AM2301, DHT_PIN, &humid, &temp);
|
|
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(tag, "Error: could not read DHT22 Sensor");
|
|
} else {
|
|
// printf("Temp: %d.%d C\n", temp / 10, temp % 10);
|
|
// printf("Humidity: %d.%d%%\n", humid / 10, humid % 10);
|
|
//
|
|
float celsius = convertValue(temp);
|
|
float humidity = convertValue(humid);
|
|
float fahrenheit = converTemp(celsius);
|
|
|
|
sprintf(tempStr, "T: %.1fC / %.1fF", celsius, fahrenheit);
|
|
sprintf(humidStr, "Humidity: %.1f%%", humidity);
|
|
LCD_writeStr(tempStr);
|
|
LCD_setCursor(0, 1);
|
|
LCD_writeStr(humidStr);
|
|
}
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
float convertValue(int16_t val) { return val / 10.0f; }
|
|
|
|
float converTemp(int16_t celsius) { return celsius * (9.0f / 5.0f) + 32.0f; }
|