#include <DHT.h> // Include the correct DHT library
#include "mbedtls/aes.h"
#define dht_dpin 21 // DHT11 connected to GPIO 21 (IO2)
#define AES_KEY_SIZE 128
DHT dht(dht_dpin, DHT22); // Create a DHT object for the sensor
void setup()
{
Serial.begin(115200);
dht.begin(); // Initialize the DHT sensor
delay(10);
}
void loop()
{
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Convert temperature and humidity to strings
char data[32];
sprintf(data, "Humidity = %.2f%% Temperature = %.2f°C", humidity, temperature);
// Encrypt data
char encrypted_data[32];
mbedtls_aes_context aes;
char * key = "abcdefghijklmnop";
mbedtls_aes_init( &aes );
mbedtls_aes_setkey_enc( &aes, (const unsigned char*) key, AES_KEY_SIZE );
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)data, (unsigned char*)encrypted_data);
mbedtls_aes_free( &aes );
// Print encrypted data
Serial.print("Encrypted Data: ");
for (int i = 0; i < 16; i++) {
char str[3];
sprintf(str, "%02x", (int)encrypted_data[i]);
Serial.print(str);
}
Serial.println();
}