Skip to content

Commit

Permalink
rename variable in ble. add timeout in exModule
Browse files Browse the repository at this point in the history
  • Loading branch information
drinktoomuchsax committed Jul 18, 2023
1 parent 4f75392 commit 5322dff
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 85 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ _This page is not available in [English](README.en.md) yet._
#### b.排线
### 2.开发板刷入程序
#### a.更改参数
- bleServerName
- BLE UUIDs
- WiFi name
- WiFi pswd
- realpswd
-
### 3.安装
#### a.继电器接门内地线
#### b.继电器接开发版控制线、电源线、地线
Expand Down
248 changes: 248 additions & 0 deletions doorKit_exModule/exModule_backup_0.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/**COPYRIGHT DELARE */
/*********
Rui Santos
Complete instructions at https://RandomNerdTutorials.com/esp32-ble-server-client/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-client-server-wi-fi/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/**
* @author [email protected]
* @date 2023-07-18
* @copyright The copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <Arduino.h>
// keyboard part
#include <vector>
using namespace std;

#include <keyboardMatrix.h>

// STATE indicate the state of esp32. It could be syandby, verifyPSWD_wipeJitter, undifened and etc
String STATE = "undifeined";
// face recognition part
// #include <fr1002.h>

HardwareSerial SERfr1002(2); // rename Serial2 into SERfr1002 (stand for serial for fr1002)

// keyboard part set your password in keyboardMatrix lib
vector<int> pswd; // initialize a password vector(array) to store the key user press
vector<int> realpswd; // realpswd
int keyboardOutput[3] = {3, 4, 5}; // defining gpio output pin
#define key0 9
#define key1 12
#define key2 13
#define key3 8
int keyboardInput[4] = {key0, key1, key2, key3}; // defining gpio input pin
unsigned long lastTime = 0; // input timeout
unsigned long timerDelay = 30000; // 30s

// face recognition part
uint8_t set_standby[6] = {0xEF, 0xAA, 0x23, 0x00, 0x00, 0x23};
uint8_t get_status[6] = {0xEF, 0xAA, 0x11, 0x00, 0x00, 0x11};
uint8_t go_recognization[8] = {0xEF, 0xAA, 0x12, 0x00, 0x02, 0x00, 0x0A, 0x1A}; // timeout 10s, parity is 0x1A
uint8_t get_usernameANDid[6] = {0xEF, 0xAA, 0x24, 0x00, 0x00, 0x24};

// led setup
bool testState = false;
bool lockState = false;

const int ledA = 10;
const int ledB = 11;

void ledBlink(int whichLed, int citcleTime, int delatTime)
{
for (int Z = 0; Z < citcleTime; Z++)
{
digitalWrite(whichLed, HIGH);
delay(delatTime);
digitalWrite(whichLed, LOW);
delay(delatTime);
}
if (lockState)
{
digitalWrite(ledB, HIGH);
}
}

bool switchState(bool state)
{
if (testState)
{
Serial.println("switchState is here");
}
if (state)
{
if (testState)
{
Serial.println("ops false");
}
return false;
}
else
{
if (testState)
{
Serial.println("ops true");
}
return true;
}
}

void setup()
{
Serial.begin(115200);

// SERfr1002.begin(115200, SERIAL_8N1, 16, 17); // uart port for hlk-fr1002 face recogniton module with baud rate 115200 bps, 8_data_bit, No_parity, 1_stop_bit

// led setup
pinMode(ledA, OUTPUT);
pinMode(ledB, OUTPUT);
}
void loop()
{
// 0.5ms finish write for loop

// delay(500); // wait fr1002 to initial
// if (STATE != "sendRecg_waitRespons") // add lidar dsitance condition
// {
// if (SERfr1002.availableForWrite())
// {
// for (int i = 0; i < 8; i++)
// {
// SERfr1002.write(go_recognization[i]);
// STATE = "sendRecg_waitRespons";
// }
// }
// // face match: 0xEF 0xAA 0x00 0x00 0x26 0x12 0x00 (36bytes) 0x23
// // EF AA 00 00 26 12 00 00 01 73 61 78 00 00
// // face fail: 0xEF 0xAA 0x00 0x00 0x26 0x12 0x0D (36bytes) 0x23
// delay(1000);
// vector<uint16_t>
// DATA = {};
// uint16_t dat;
// for (int d = 0; d < 44; d++)
// {
// dat = SERfr1002.read();
// Serial.printf("%x ", dat);
// DATA.push_back(dat);
// }
// Serial.println();
// }
// // 0.7ms finish to receive

// delay(10000);

STATE = "standby";

if (whichKeyPress(keyboardOutput, keyboardInput) != -1)
{
lastTime = millis(); // start record time
STATE = "checkPSWD";
// keyboard password part, detail look on productdesign.excalidraw
if ((STATE == "checkPSWD") && ((millis() - lastTime) < timerDelay))
{
pswd.push_back(whichKeyPress(keyboardOutput, keyboardInput)); // save the key into an array(actully vector)
if (pswd.size() == 100) // deal with the jitter
{
int keyBeenPressed = wipeJitter(pswd);
if (keyBeenPressed != -1)
{
realpswd.push_back(keyBeenPressed);
ledBlink(ledA, 1, 50);
if (testState)
{
Serial.printf("add to \"%d\" password\n", keyBeenPressed);
}
}
vector<int> clearpswd; // clear the pswd vector to save memory
pswd.swap(clearpswd);
}

if (whichKeyPress(keyboardOutput, keyboardInput) == -9) // click "#" to enter
{
if (testState) // print real pswd
{
Serial.print("realpswd is: ");
for (int i = 0; i < realpswd.size(); i++)
{
Serial.printf("_%d", realpswd[i]);
}
Serial.println();
}

if (verifyPSWD(realpswd) == "TRUE") // match, open the door
{
uint8_t open = 0x01;
Serial.write(open);
ledBlink(ledA, 5, 80);
STATE = "standby";
delay(1000);
}
else if (verifyPSWD(realpswd) == "LOCK") // match, lock/unlock the door
{
lockState = switchState(lockState);

if (testState)
{
if (lockState)
Serial.print("\nsend lock door signal\n");
else
Serial.print("\nsend unlock door signal\n");
}
uint8_t lock = 0x03;
Serial.write(lock);
STATE = "standby";
delay(1000);
}
else if (verifyPSWD(realpswd) == "TEST")
{
testState = switchState(testState);
if (testState)
{
Serial.println("test mode");
}
else
{
Serial.println("exit test mode");
}
STATE = "undefined";
}
else // do nothing but red led
{
Serial.print("verify wrong\n");
ledBlink(ledB, 5, 80);
STATE = "undefined";
};
realpswd.clear();
}
else if (whichKeyPress(keyboardOutput, keyboardInput) == -6) // click "C" to clear
{
vector<int> clearpswd;
pswd.swap(clearpswd);
realpswd.swap(clearpswd);
STATE = "clearPSWD";
delay(500);
}
}
}
else if ((millis() - lastTime) > timerDelay) // timeout wipe out the pswd
{
vector<int> clearpswd;
pswd.swap(clearpswd);
realpswd.swap(clearpswd);
STATE = "clearPSWD";
ledBlink(ledB, 2, 80);
ledBlink(ledA, 2, 80);
}
}
41 changes: 16 additions & 25 deletions doorKit_exModule/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,25 @@ const int lockRED = 13;
#include <BLEUtils.h>
#include <BLE2902.h>

// Default Temperature is in Celsius
// Comment the next line for Temperature in Fahrenheit
#define temperatureCelsius

// BLE server name
#define bleServerName "BME280_ESP32"

float temp;
float tempF;
float hum;
#define bleServerName "1106_door_kit"

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 3000;

bool deviceConnected = false;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
// See the following for generating UUIDs: https://www.uuidgenerator.net/
#define SERVICE_UUID "91bad492-b950-4226-aa2b-4ede9fa42f59"

// Temperature Characteristic and Descriptor
BLECharacteristic bmeTemperatureCelsiusCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor bmeTemperatureCelsiusDescriptor(BLEUUID((uint16_t)0x2902));
BLECharacteristic openCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor openDescriptor(BLEUUID((uint16_t)0x2902));

// Humidity Characteristic and Descriptor
BLECharacteristic bmeHumidityCharacteristics("ca73b3ba-39f6-4ab3-91ae-186dc9577d99", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor bmeHumidityDescriptor(BLEUUID((uint16_t)0x2903));
BLECharacteristic lockCharacteristics("ca73b3ba-39f6-4ab3-91ae-186dc9577d99", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor lockDescriptor(BLEUUID((uint16_t)0x2903));

// Setup callbacks onConnect and onDisconnect
class MyServerCallbacks : public BLEServerCallbacks
Expand Down Expand Up @@ -66,13 +57,13 @@ void setup()

// Create BLE Characteristics and Create a BLE Descriptor
// Temperature
bmeService->addCharacteristic(&bmeTemperatureCelsiusCharacteristics);
bmeTemperatureCelsiusDescriptor.setValue("BME temperature Celsius");
bmeTemperatureCelsiusCharacteristics.addDescriptor(&bmeTemperatureCelsiusDescriptor);
bmeService->addCharacteristic(&openCharacteristics);
openDescriptor.setValue("BME temperature Celsius");
openCharacteristics.addDescriptor(&openDescriptor);
// Humidity
bmeService->addCharacteristic(&bmeHumidityCharacteristics);
bmeHumidityDescriptor.setValue("BME humidity");
bmeHumidityCharacteristics.addDescriptor(new BLE2902());
bmeService->addCharacteristic(&lockCharacteristics);
lockDescriptor.setValue("BME humidity");
lockCharacteristics.addDescriptor(new BLE2902());

// Start the service
bmeService->start();
Expand All @@ -92,14 +83,14 @@ void loop()
{
char hello[] = {'o'};
// Set temperature Characteristic value and notify connected client
bmeTemperatureCelsiusCharacteristics.setValue(hello);
bmeTemperatureCelsiusCharacteristics.notify();
openCharacteristics.setValue(hello);
openCharacteristics.notify();
Serial.println("sent hello");

// Set humidity Characteristic value and notify connected client
char world[] = {'l'};
bmeHumidityCharacteristics.setValue(world);
bmeHumidityCharacteristics.notify();
lockCharacteristics.setValue(world);
lockCharacteristics.notify();
Serial.println("world");
lastTime = millis();
}
Expand Down
Loading

0 comments on commit 5322dff

Please sign in to comment.