encrypted-walkie-talkie/code/code.ino

195 lines
4.6 KiB
Arduino
Raw Permalink Normal View History

2023-04-20 03:14:36 +03:00
//TODO: Learn how to use mic. in arduino, transmit plain sound among two arduinos and play it
//TODO: Implement encryption and decryption using AES128, test it with text, then use to encrypt sound
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <AESLib.h>
2023-04-20 17:32:59 +03:00
#include <EEPROM.h>
2023-04-20 03:14:36 +03:00
//Components
LiquidCrystal_I2C lcd(0x27, 16, 2);
2023-05-16 23:21:28 +03:00
RF24 radio(9,10); // CE, CSN
2023-07-03 16:13:26 +03:00
const byte micPin = A0;
2023-04-20 03:14:36 +03:00
//Settings
//byte unsigned address = 1;
const byte address[6] = "00001";
byte key = 97;
2023-04-20 17:32:59 +03:00
byte unsigned screen = 0;
const char seed[32] = "555bfda9ba33008ceba78321683ed8e"; // random seed, MUST BE 31 chars long
char password[32];
bool mode = 0; // 0 = transmit, 1 = recieve
2023-04-20 17:32:59 +03:00
bool encryption = 1; // will the trafic be encrypted?
2023-05-16 23:21:28 +03:00
bool isScreenChanged = true; // we must display first time
2023-04-20 17:32:59 +03:00
//Switch screen button
2023-07-03 16:13:26 +03:00
const byte switchScreenButton = A1;
2023-04-20 17:32:59 +03:00
byte switchScreenLast;
//IncreaseButton
2023-07-03 16:13:26 +03:00
const byte increaseButton = A2;
2023-04-20 17:32:59 +03:00
byte increaseButtonLast;
//DecreaseButton
2023-07-03 16:13:26 +03:00
const byte decreaseButton = A3;
2023-04-20 17:32:59 +03:00
byte decreaseButtonLast;
2023-04-20 03:14:36 +03:00
void switchMode();
void printOutput();
void updatePassword();
void encryptDecyptData();
2023-04-20 03:14:36 +03:00
2023-07-03 16:13:26 +03:00
//short micValue = 337;
//const short micBaseline = 337;// 3.3v / 2 = 1.56v, 1.56/5*1024 = 337.
const byte micValuesLength = 16;
short micValues[micValuesLength];
2023-07-03 16:13:26 +03:00
2023-04-20 03:14:36 +03:00
void setup() {
Serial.begin(9600);
2023-07-03 16:13:26 +03:00
pinMode(micPin, INPUT);
2023-04-20 17:32:59 +03:00
//Setting up buttons
pinMode(switchScreenButton, INPUT_PULLUP);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(decreaseButton, INPUT_PULLUP);
2023-04-20 03:14:36 +03:00
//Setting up a radio module
radio.begin();
2023-05-16 23:21:28 +03:00
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x00);
radio.setDataRate(RF24_250KBPS);
2023-04-20 03:14:36 +03:00
switchMode(); //default is recieving
//Setting up a LCD
lcd.init();
lcd.backlight();
2023-04-20 03:14:36 +03:00
updatePassword();
}
void loop() {
if (!mode) {// transmit
for (int i = 0; i < micValuesLength; i ++) {
2023-07-03 16:13:26 +03:00
micValues[i] = analogRead(micPin) << 2;
}
2023-07-14 03:07:08 +03:00
if (encryption)
encryptDecyptData();
2023-07-03 16:13:26 +03:00
radio.write(micValues, 32);
} else { // receive
if (radio.available()) {
2023-07-03 16:13:26 +03:00
radio.read(micValues, 32);
2023-07-14 03:07:08 +03:00
if (encryption)
encryptDecyptData();
for (int i = 0; i < micValuesLength; i++) {
2023-07-03 16:13:26 +03:00
PORTD = micValues[i];
}
}
}
2023-04-20 17:32:59 +03:00
byte switchButtonValue = digitalRead(switchScreenButton);
byte increaseValue = digitalRead(increaseButton);
byte decreaseValue = digitalRead(decreaseButton);
if (switchButtonValue == 0 && switchScreenLast == 1) {
2023-05-16 23:21:28 +03:00
screen = screen < 3? screen + 1 : 0;
isScreenChanged = true;
2023-04-20 17:32:59 +03:00
}
2023-04-21 23:07:11 +03:00
if ((decreaseValue == 0 && decreaseButtonLast == 1) || increaseValue == 0 && increaseButtonLast == 1) {
2023-05-16 23:21:28 +03:00
isScreenChanged = true;
2023-04-20 17:32:59 +03:00
switch (screen) {
case 0:
2023-04-21 23:07:11 +03:00
mode = !mode;
2023-04-20 17:32:59 +03:00
break;
case 1:
2023-04-21 23:07:11 +03:00
if (decreaseValue == 0 && decreaseButtonLast == 1)
// address = address > 0? address - 1 : 127;
2023-07-03 16:13:26 +03:00
1+1;
2023-04-21 23:07:11 +03:00
else
// address = address < 127? address + 1 : 0;
2023-07-03 16:13:26 +03:00
1+1;
2023-04-20 17:32:59 +03:00
break;
case 2:
if (decreaseValue == 0 && decreaseButtonLast == 1){
2023-04-21 23:07:11 +03:00
key --;
} else {
2023-04-21 23:07:11 +03:00
key ++;
}
2023-04-20 17:32:59 +03:00
break;
case 3:
2023-04-21 23:07:11 +03:00
encryption = !encryption;
2023-04-20 17:32:59 +03:00
break;
}
}
switchScreenLast = switchButtonValue;
increaseButtonLast = increaseValue;
decreaseButtonLast = decreaseValue;
2023-05-16 23:21:28 +03:00
if (isScreenChanged)
printOutput();
2023-04-20 03:14:36 +03:00
}
void switchMode () {
if (!mode) { // transmit -> recieve
// radio.openReadingPipe(0, 0x0123456789LL);
radio.openReadingPipe(0, address);
2023-04-20 03:14:36 +03:00
radio.startListening();
} else { // recieve -> transmit
radio.openWritingPipe(address);
radio.stopListening();
}
mode = !mode;
}
void printOutput () {
2023-04-20 17:32:59 +03:00
lcd.clear();
switch (screen) {
case 0:
lcd.print("Mode: ");
2023-07-03 16:13:26 +03:00
lcd.print(mode? "receive" : "transmit");
2023-04-20 17:32:59 +03:00
break;
case 1:
2023-07-03 16:13:26 +03:00
lcd.print("Address: ");
// lcd.print(address);
2023-04-20 17:32:59 +03:00
break;
case 2:
lcd.print("Password: ");
lcd.print(key);
break;
case 3:
2023-07-03 16:13:26 +03:00
lcd.print("Encryption: ");
2023-04-20 17:32:59 +03:00
lcd.print(encryption? "yes" : "no");
}
2023-05-16 23:21:28 +03:00
isScreenChanged = false;
2023-04-20 17:32:59 +03:00
2023-04-20 03:14:36 +03:00
// for (int i = 0; i < 5; i ++) lcd.print(address[i] - '0');
//lcd.print(mode);
}
void updatePassword() {
for (int i = 0; i < 31; i ++) {
password[i] = seed[i];
}
password[31] = (char)key;
}
void encryptDecyptData () {
for (int i = 0; i < micValuesLength; i ++) {
char localKey[2];
localKey[0] = password[2 * i];
localKey[1] = password[2 * i + 1];
Serial.print("Before:");
Serial.print(micValues[i]);
micValues[i] = micValues[i] ^ (short)localKey;
Serial.print(",After:");
Serial.println(micValues[i]);
}
2023-04-20 03:14:36 +03:00
}