Software setup
I created this example script as a reference for your own script. You only have to change the variables to match your situation. You're free to use it (Source: TheThingsUno SDK and this adafruit article)
#include "TheThingsUno.h"
#include <Keypad.h>
// Set your AppEUI and AppKey
const byte appEui[8] = { <appEui> }; //for example: {0x70, 0xB3, 0xD5, 0x7E, 0xE0, 0xE0, 0x01, 0x4A1};
const byte appKey[16] = { <appKey> }; //for example: {0x73, 0x6D, 0x24, 0xD2, 0x69, 0xBE, 0xE3, 0xAE, 0x0E, 0xCE, 0xF0, 0xBB, 0x6C, 0xA4, 0xBA, 0xFE};
#define loraSerial Serial1
TheThingsUno ttu;
//setup the keypad (be sure not to use A0, as this pin is used for randomSeed()
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Keypad pin definitions
byte rowPins[rows] = {8, 7, 6, 5};
byte colPins[cols] = {4, 3, 2};
// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
char* secretCode = "1234";
char* secretCode2 = "1324"; //will be replaced by a random number
int position = 0;
int RED_LED = 11;
int GREEN_LED = 12;
//variables setup complete!
void setup() {
//security lock setup
Serial.begin(9600);
Serial.println("Voer een code in");
pinMode(GREEN_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
randomSeed(analogRead(0));
//LoRa setup
loraSerial.begin(57600);
delay(1000);
ttu.init(loraSerial, Serial);
ttu.reset();
//the device will attempt a join every second till the join is successfull
while(!ttu.join(appEui, appKey)){
delay(1000);
}
digitalWrite(13, HIGH); //turn on LED to confirm join
delay(6000);
ttu.showStatus();
Serial.println("Setup for The Things Network complete");
delay(1000);
}
boolean checkForCode(char* rightCode) {
while(true) {
char key = keypad.getKey();
if (key != 0)
{
if (key == rightCode[position])
{
Serial.print("Matched ");
Serial.print(key);
Serial.print(" -at- ");
Serial.println(position);
position ++;
delay(500);
}
else // Invalid key - return false
{
Serial.println("Invalid Code!");
position = 0;
return false;
}
}
if (position == 4) // the code is complete - return true
{
position = 0;
return true;
}
delay(200);
}
}
void loop() {
if(checkForCode(secretCode)) {
digitalWrite(GREEN_LED, HIGH);
Serial.println("The first code matched!");
//get a random number for second authentication
for(int i=0; i < 4; i++) {
secretCode2[i] = random(49,59); //the ascii numbers for 0 - 9
}
//send the second code over LoRa
ttu.sendString(secretCode);
Serial.print("The second code is send over LoRa!");
delay(500);
digitalWrite(GREEN_LED, LOW);
if (checkForCode(secretCode2)) {
digitalWrite(GREEN_LED, HIGH);
Serial.println("The second code matched also");
delay(5000);
digitalWrite(GREEN_LED, LOW);
} else {
digitalWrite(RED_LED, HIGH);
Serial.println("The second code is wrong!");
delay(1000);
digitalWrite(RED_LED, LOW);
}
} else {
digitalWrite(RED_LED, HIGH);
Serial.println("The first code is wrong!");
delay(1000);
digitalWrite(RED_LED, LOW);
}
delay(500);
}
You can find this code also on my github: https://github.com/sven250/LoRa_two_step_authentication