#include <WiFi.h>
#include <PubSubClient.h>
#define LED 4
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "mqtt.my.id";
WiFiClient espClient;
PubSubClient client(espClient);
String data;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password, 6);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
data = "";
for (int i = 0; i < length; i++) {
data += (char) payload[i];
}
String text = decrypt(data);
Serial.println(text);
// Konversi data ke integer
int value = data.toInt(); // Ubah string ke integer
// Logika untuk mengontrol LED
if (value < 200) {
digitalWrite(LED, LOW); // Matikan LED
} else {
digitalWrite(LED, HIGH); // Nyalakan LED
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
client.subscribe("wokwi/klp8/encrypt");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// the decryption function
String decrypt(String encryptedBase64Text) {
// calculate the original length before it was coded into base64 string
int originalBytesLength = base64::decodeLength(encryptedBase64Text.c_str());
// declare empty byte array (a memory storage)
byte encryptedBytes[originalBytesLength];
byte decryptedBytes[originalBytesLength];
// convert the base64 string into original bytes
// which is the encryptedBytes
base64::decode(encryptedBase64Text.c_str(), encryptedBytes);
// initializing AES engine
// Cipher Mode and Key Size are preset in AESLib
// Cipher Mode = CBC
// Key Size = 128
// declare the KEY and IV
byte aesKey[] = { 23, 45, 56, 67, 67, 87, 98, 12, 32, 34, 45, 56, 67, 87, 65, 5 };
byte aesIv[] = { 123, 43, 46, 89, 29, 187, 58, 213, 78, 50, 19, 106, 205, 1, 5, 7 };
// set the padding mode to paddingMode.CMS
aesLib.set_paddingmode((paddingMode)0);
// decrypt bytes in "encryptedBytes" and save the output in "decryptedBytes"
// param 1 = the source bytes to be decrypted
// param 2 = the length of source bytes
// param 3 = the destination of decrypted bytes that will be saved
// param 4 = KEY
// param 5 = the length of KEY bytes (16)
// param 6 = IV
aesLib.decrypt(encryptedBytes, originalBytesLength, decryptedBytes, aesKey, 16, aesIv);
// convert the decrypted bytes into original string
String decryptedText = String((char*)decryptedBytes);
return decryptedText;
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(LED, OUTPUT);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}