encrypted-walkie-talkie/code/code.ino

143 lines
3.2 KiB
Arduino
Raw Normal View History

2023-04-20 03:14:36 +03:00
//TODO: Learn how to use radio module, transmit data with it
//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);
RF24 radio(7,8);
//Settings
2023-04-20 17:32:59 +03:00
byte unsigned address = 1;
2023-04-20 03:14:36 +03:00
unsigned short key = 0;
2023-04-20 17:32:59 +03:00
byte unsigned screen = 0;
const String seed = "1305b5cfa1a59e9789de"; // "random" seed ;)
2023-04-20 03:14:36 +03:00
String password;
bool mode = 0; // 0 = transmit, 1 = recieve, bool is 2 bytes less than byte :)
2023-04-20 17:32:59 +03:00
bool encryption = 1; // will the trafic be encrypted?
//Switch screen button
const byte switchScreenButton = 2;
byte switchScreenLast;
//IncreaseButton
const byte increaseButton = 3;
byte increaseButtonLast;
//DecreaseButton
const byte decreaseButton = 4;
byte decreaseButtonLast;
2023-04-20 03:14:36 +03:00
void switchMode();
void printOutput();
void updatePassword();
void setup() {
Serial.begin(9600);
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();
radio.setPALevel(RF24_PA_MIN);
switchMode(); //default is recieving
//Setting up a LCD
lcd.init();
lcd.backlight();
updatePassword();
}
void loop() {
2023-04-20 17:32:59 +03:00
// switchMode();
byte switchButtonValue = digitalRead(switchScreenButton);
byte increaseValue = digitalRead(increaseButton);
byte decreaseValue = digitalRead(decreaseButton);
if (switchButtonValue == 0 && switchScreenLast == 1) {
screen = screen < 3? screen + 1 : 0;
}
2023-04-21 23:07:11 +03:00
if ((decreaseValue == 0 && decreaseButtonLast == 1) || increaseValue == 0 && increaseButtonLast == 1) {
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;
else
address = address < 127? address + 1 : 0;
2023-04-20 17:32:59 +03:00
break;
case 2:
2023-04-21 23:07:11 +03:00
if (decreaseValue == 0 && decreaseButtonLast == 1)
key --;
else
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-04-20 03:14:36 +03:00
printOutput();
2023-04-20 17:32:59 +03:00
delay(100);
2023-04-20 03:14:36 +03:00
}
void switchMode () {
if (mode) { // transmit -> recieve
radio.openReadingPipe(0, address);
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: ");
lcd.print(mode? "rec." : "tra.");
break;
case 1:
lcd.print("Addr.: ");
lcd.print(address);
break;
case 2:
lcd.print("Password: ");
lcd.print(key);
break;
case 3:
2023-04-20 17:59:37 +03:00
lcd.print("Enc.: ");
2023-04-20 17:32:59 +03:00
lcd.print(encryption? "yes" : "no");
}
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() {
password = seed + key;
}