encrypted-walkie-talkie/code/code.ino

173 lines
4.0 KiB
C++

//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>
#include <EEPROM.h>
//Components
LiquidCrystal_I2C lcd(0x27, 16, 2);
RF24 radio(9,10); // CE, CSN
//Settings
//byte unsigned address = 1;
const byte address[6] = "00001";
unsigned short key = 0;
byte unsigned screen = 0;
const String seed = "1305b5cfa1a59e9789de"; // "random" seed ;)
String password;
bool mode = 0; // 0 = transmit, 1 = recieve, bool is 2 bytes less than byte :)
bool encryption = 1; // will the trafic be encrypted?
byte value = 0;
bool isScreenChanged = true; // we must display first time
//Switch screen button
const byte switchScreenButton = 2;
byte switchScreenLast;
//IncreaseButton
const byte increaseButton = 3;
byte increaseButtonLast;
//DecreaseButton
const byte decreaseButton = 4;
byte decreaseButtonLast;
void switchMode();
void printOutput();
void updatePassword();
void setup() {
Serial.begin(9600);
//Setting up buttons
pinMode(switchScreenButton, INPUT_PULLUP);
pinMode(increaseButton, INPUT_PULLUP);
pinMode(decreaseButton, INPUT_PULLUP);
//Setting up a radio module
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0x00);
radio.setDataRate(RF24_250KBPS);
switchMode(); //default is recieving
switchMode();
//Setting up a LCD
lcd.init();
lcd.backlight();
updatePassword();
}
void loop() {
// switchMode();
if (!mode) {// transmit
const char text[] = "Hello world!";
if (radio.write(&text, sizeof(text))) Serial.println("данные успешно отправлены");
else Serial.println("хуй там");
} else { // receive
if (radio.available()) {
char text[32] = "";
radio.read(&text, sizeof(text));
Serial.println(text);
}
}
byte switchButtonValue = digitalRead(switchScreenButton);
byte increaseValue = digitalRead(increaseButton);
byte decreaseValue = digitalRead(decreaseButton);
if (switchButtonValue == 0 && switchScreenLast == 1) {
screen = screen < 3? screen + 1 : 0;
isScreenChanged = true;
}
if ((decreaseValue == 0 && decreaseButtonLast == 1) || increaseValue == 0 && increaseButtonLast == 1) {
isScreenChanged = true;
switch (screen) {
case 0:
mode = !mode;
Serial.println("xui");
break;
case 1:
if (decreaseValue == 0 && decreaseButtonLast == 1)
// address = address > 0? address - 1 : 127;
Serial.println("");
else
// address = address < 127? address + 1 : 0;
Serial.println("");
break;
case 2:
if (decreaseValue == 0 && decreaseButtonLast == 1){
key --;
} else {
key ++;
}
break;
case 3:
encryption = !encryption;
break;
}
}
switchScreenLast = switchButtonValue;
increaseButtonLast = increaseValue;
decreaseButtonLast = decreaseValue;
if (isScreenChanged)
printOutput();
// Serial.println(analogRead(A1));
analogWrite(6, value);
value +=5;
delay(50);
}
void switchMode () {
if (!mode) { // transmit -> recieve
// radio.openReadingPipe(0, 0x0123456789LL);
radio.openReadingPipe(0, address);
radio.startListening();
} else { // recieve -> transmit
radio.openWritingPipe(address);
radio.stopListening();
}
mode = !mode;
}
void printOutput () {
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:
lcd.print("Enc.: ");
lcd.print(encryption? "yes" : "no");
}
isScreenChanged = false;
// for (int i = 0; i < 5; i ++) lcd.print(address[i] - '0');
//lcd.print(mode);
}
void updatePassword() {
password = seed + key;
}