#define BLYNK_TEMPLATE_ID "TMPL4rGR9Tw_T"
#define BLYNK_TEMPLATE_NAME "lab4"
#define BLYNK_AUTH_TOKEN "jEyTnhs8j6PABePISF_wHIvD74EfqyzM"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <AESLib.h>
// WiFi
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// AES
AESLib aesLib;
// AES-128 ключ
byte aesKey[] = {
0x2b, 0x7e, 0x15, 0x16,
0x28, 0xae, 0xd2, 0xa6,
0x61, 0x37, 0x10, 0x56,
0x72, 0x6b, 0x5c, 0x27};
// Фіксований IV
byte aesIv[16] = {
0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15};
// LDR pin
const int sensorPin = 34;
// Буфери
char dataToEncrypt[16];
byte encrypted[64];
char encryptedHex[128];
BlynkTimer timer;
void sendEncryptedData()
{
// ===== Зчитування освітленості =====
const float GAMMA = 0.7;
const float RL10 = 33;
int analogValue = analogRead(sensorPin);
float voltage = analogValue / 4096.0 * 3.3;
float resistance =
2000 * voltage / (1 - voltage / 3.3);
float lightValue =
pow(RL10 * 1e3 * pow(10, GAMMA) / resistance,
(1 / GAMMA));
// ===== float -> text =====
sprintf(dataToEncrypt, "%.2f", lightValue);
// ===== AES encryption =====
uint16_t encryptedLength = aesLib.encrypt(
(byte *)dataToEncrypt,
strlen(dataToEncrypt),
encrypted,
aesKey,
128,
aesIv);
// ===== bytes -> HEX =====
encryptedHex[0] = '\0';
for (int i = 0; i < encryptedLength; i++)
{
char temp[4];
sprintf(temp, "%02X", encrypted[i]);
strcat(encryptedHex, temp);
}
// ===== Serial =====
Serial.print("Original value: ");
Serial.println(lightValue);
Serial.print("Encrypted HEX: ");
Serial.println(encryptedHex);
// ===== Send to Blynk =====
Blynk.virtualWrite(V0, encryptedHex);
Serial.println("Encrypted data sent");
Serial.println("---------------------------");
}
void setup()
{
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(3000L, sendEncryptedData);
Serial.println("AES Sender Ready");
}
void loop()
{
Blynk.run();
timer.run();
}