#include <WiFi.h>
#include <WiFiClient.h>
#include <AESLib.h>
#include <Base64.h>
WiFiServer server(1444);
// Replace ‘YOUR_SECRET_KEY’ with the same secure key used for AES encryption
byte SECRET_KEY[] = {0xd4, 0x99, 0x8a, 0xe7, 0x40, 0xfd, 0xe3, 0xa7, 0xdb, 0x12, 0x2c, 0xbb, 0x4f, 0x16, 0xb8, 0xf8, 0x3c, 0x0f, 0x29, 0x59, 0x6b, 0x3e, 0xf7, 0x08, 0x70, 0xe7, 0xd5, 0x37, 0x67, 0x92, 0xc1, 0xe6};
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(“DD”, “12345678”);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi”);
// Start the server
server.begin();
}
void loop() {
// Check for a new client connection
WiFiClient client = server.available();
if (client) {
Serial.println(“New client connected”);
// Read the encrypted message from the client
String encryptedMessage = client.readStringUntil('\n');
Serial.println("the encrypted message:"+ encryptedMessage);
encryptedMessage.trim();
// Decrypt the message
String decryptedMessage = decryptData(encryptedMessage);
// Print the decrypted message
Serial.println("Decrypted Message: " + decryptedMessage);
// Close the client connection
client.stop();
}
}
String decryptData(String encryptedData) {
// Decode the Base64 encoded string
byte decodedData[encryptedData.length()];
base64_decode((char*)decodedData, encryptedData.c_str(), encryptedData.length());
// Extract nonce and ciphertext
byte nonce[12];
byte ciphertext[sizeof(decodedData) – 12];
memcpy(nonce, decodedData, 12);
memcpy(ciphertext, decodedData + 12, sizeof(decodedData) – 12);
// Set up the AES decryption
AES aes;
aes.set_key(SECRET_KEY, 16);
// Decrypt the ciphertext
aes.decrypt(ciphertext, nonce);
// Convert decrypted data to a String
String decryptedMessage = String((char*)ciphertext, sizeof(ciphertext));
return decryptedMessage;
}
the python code:
import socket
import pyttsx3
import time
import speech_recognition as sr
import soundfile as sf
import numpy as np
from python_speech_features import mfcc
from sklearn.neural_network import MLPClassifier
import pickle
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from base64 import urlsafe_b64encode, urlsafe_b64decode
import secrets
SECRET_KEY = bytes.fromhex(“d4998ae740fde3a7db122cbb4f16b8f83c0f29596b3ef70870e7d5376792c1e6”)
def encrypt_data(data):
print(data)
aesgcm = AESGCM(SECRET_KEY)
print(“SECRET_KEY_bytes:”,SECRET_KEY )
nonce = secrets.token_bytes(12) # 12 bytes for the nonce
print (“nonce: “, nonce)
ciphertext = aesgcm.encrypt(nonce, data.encode(‘utf-8’), None)
print(“ciphertext: “,ciphertext)
return urlsafe_b64encode(nonce + ciphertext).decode(‘utf-8’)
def decrypt_data(encrypted_data):
aesgcm = AESGCM(SECRET_KEY)
encrypted_bytes = urlsafe_b64decode(encrypted_data.encode(‘utf-8’))
nonce = encrypted_bytes[:12]
ciphertext = encrypted_bytes[12:]
return aesgcm.decrypt(nonce, ciphertext, None).decode(‘utf-8’)
def extract_features(file_name, expected_features=200):
try:
# choose only WAV format
if file_name.lower().endswith(“.wav”):
# Extract MFCC features from the audio
audio, sample_rate = sf.read(file_name)
nfft = 2048 if sample_rate * 0.025 > 512 else 512
mfccs = mfcc(audio, samplerate=sample_rate, numcep=40, nfft=nfft)
mfccs_processed = np.mean(mfccs, axis=0)
# Pad or truncate features to match the expected number
if len(mfccs_processed) < expected_features:
mfccs_processed = np.pad(mfccs_processed, (0, expected_features - len(mfccs_processed)))
else:
mfccs_processed = mfccs_processed[:expected_features]
return mfccs_processed
except Exception as e:
print("Error encountered while processing file: ", file_name)
return np.zeros(expected_features) # Return a default feature vector of zeros
def execute_command(command):
# Connect to the ESP32
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((“192.168.137.241”, 1444))
encrypted_command = encrypt_data(command)
print(decrypt_data(encrypted_command))
s.sendall(encrypted_command.encode(‘utf-8’))
print("encrypted command:")
print(encrypted_command)
s.close()
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def get_password():
# You can replace this with a more secure way to obtain the password
print(“Please enter the password:”)
password = input()
return password.lower()
def main():
recognizer = sr.Recognizer()
loaded_model = None
# Load the trained model
with open('trained_model.pkl', 'rb') as file:
loaded_model = pickle.load(file)
while True:
if(True):
with sr.Microphone() as source:
print("Say something:")
audio = recognizer.listen(source)
try:
# Recognize speech using Google Speech Recognition
command = recognizer.recognize_google(audio).lower()
if(True):
if "open the door" in command:
print("You said:", command)
speak("Ok, what's the password?")
entered_password = get_password()
if entered_password == "51830345":
speak("Password is correct, opening the door..")
execute_command('1')
print("Door opened")
else:
speak("Password is not correct")
print("Password is not correct")
elif "close the door" in command:
print("You said:", command)
speak("Ok, what's the password?")
entered_password = get_password()
if entered_password == "51830345":
speak("Password is correct, closing the door..")
execute_command('0')
print("Door closed")
else:
speak("Password is not correct")
print("Password is not correct")
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print(f"Error with the speech recognition service; {e}")
if name == “main“:
main()