#include "HD44780.h" #include "dht.h" #include "freertos/idf_additions.h" #include "nvs.h" #include #include #include #include #include #include #include #include "client/client.h" #include "wifi/wifi_con.h" #include "json/json_convert.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) { esp_err_t status = nvs_flash_init(); if (status == ESP_ERR_NVS_NO_FREE_PAGES || status == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); status = nvs_flash_init(); } ESP_ERROR_CHECK(status); status = initWifi(); if (status == WIFI_FAILURE) { ESP_LOGE(WIFI_TAG, "Failed to connect to Wifi. Exiting"); return; } else { ESP_LOGI(WIFI_TAG, "Connected to Wifi Successfully"); } 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); // store in JSON object char *json = createJSON(fahrenheit, celsius, humidity); if (!json) { ESP_LOGE("JSON", "Error creating JSON string"); } else { ESP_LOGI("JSON", "JSON String %s", json); int len = strlen(json); int res = sendJSON(json, len); if (res < 0) { ESP_LOGE("CLIENT", "Failed to send JSON: %d", res); } } } 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; }