#include <DHT.h> // Include the DHT library for DHT22 sensor
#include "mbedtls/aes.h" // Include the mbedtls library for AES encryption
#define dht_dpin 21 // Define the digital pin to which the DHT22 sensor is connected
#define AES_KEY_SIZE 128 // Set the size of the AES encryption key to 128 bits
DHT dht(dht_dpin, DHT22); // Create a DHT object for the sensor connected to dht_dpin
void setup() {
Serial.begin(115200); // Initialize serial communication at 115200 bps
dht.begin(); // Initialize the DHT sensor
delay(10); // Wait for sensor initialization
}
void loop() {
delay(1000); // Pause for 1 second
float humidity = dht.readHumidity(); // Read humidity from DHT sensor
float temperature = dht.readTemperature(); // Read temperature from DHT sensor
if (isnan(humidity) || isnan(temperature)) { // Check if sensor readings are valid
Serial.println("Failed to read from DHT sensor!"); // Print error message if readings are invalid
return; // Exit loop if readings are invalid
}
// Convert temperature and humidity to strings
char data[32];
dtostrf(humidity, 4, 2, data); // Format humidity as a string
strcat(data, "% "); // Add percentage sign
dtostrf(temperature, 4, 2, data + strlen(data)); // Append temperature as a string
strcat(data, "°C"); // Add Celsius sign
// Print original DHT sensor data
Serial.print("Original Data: ");
Serial.println(data);
// 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();
}