初始化
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* main.c 示例修改(方案C - AES加密版本)
|
||||
*
|
||||
* 修改要点:
|
||||
* 1. 包含加密库头文件
|
||||
* 2. 初始化加密模块
|
||||
* 3. 使用HTTP连接替代TCP
|
||||
* 4. 签到时加密JSON数据
|
||||
*/
|
||||
|
||||
#include "stm32f10x.h"
|
||||
#include "Delay.h"
|
||||
#include "OLED.h"
|
||||
#include "Serial.h"
|
||||
#include "K210_USART.h"
|
||||
#include "esp8266.h" // 使用新版驱动
|
||||
#include "Buzzer_LED.h"
|
||||
#include "Attendance.h"
|
||||
#include "Crypto.h" // ← 新增:加密模块
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* ========== 用户配置区域 ========== */
|
||||
const char* WIFI_SSID = "Your_WiFi_Name"; // 修改为你的WiFi名称
|
||||
const char* WIFI_PASSWORD = "Your_WiFi_Password"; // 修改为你的WiFi密码
|
||||
const char* HTTP_SERVER_IP = "192.168.1.100"; // 修改为服务器IP
|
||||
const char* HTTP_SERVER_PORT = "8082"; // HTTP端口(8082)
|
||||
|
||||
// 全局变量
|
||||
uint8_t server_connected = 0;
|
||||
|
||||
// 初始化系统
|
||||
void System_Init(void)
|
||||
{
|
||||
OLED_Init();
|
||||
Serial_Init();
|
||||
K210_USART_Init();
|
||||
Buzzer_LED_Init();
|
||||
Attendance_Init();
|
||||
Crypto_Init(); // ← 新增:初始化加密模块
|
||||
|
||||
OLED_ShowString(1, 1, "Face Attendance");
|
||||
OLED_ShowString(2, 1, "System Starting");
|
||||
OLED_ShowString(3, 1, "Please wait...");
|
||||
}
|
||||
|
||||
// 连接WiFi和服务器
|
||||
uint8_t ConnectToServer(void)
|
||||
{
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "Connecting WiFi");
|
||||
|
||||
// 1. 初始化ESP8266
|
||||
if (ESP8266_Setup() == 0) {
|
||||
OLED_ShowString(2, 1, "ESP8266 Failed!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. 连接WiFi
|
||||
OLED_ShowString(2, 1, "WiFi...");
|
||||
if (ESP8266_ConnectWiFi(WIFI_SSID, WIFI_PASSWORD) == 0) {
|
||||
OLED_ShowString(2, 1, "WiFi Failed!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
OLED_ShowString(2, 1, "WiFi OK!");
|
||||
Delay_ms(500);
|
||||
|
||||
// 3. 连接HTTP服务器(使用新函数)
|
||||
OLED_ShowString(3, 1, "Server...");
|
||||
if (ESP8266_ConnectHTTPServer(HTTP_SERVER_IP, HTTP_SERVER_PORT) == 0) {
|
||||
OLED_ShowString(3, 1, "Server Failed!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
OLED_ShowString(3, 1, "Server OK!");
|
||||
Delay_ms(500);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 处理签到(加密版本)
|
||||
void ProcessCheckIn(const char* student_id, const char* name, float confidence)
|
||||
{
|
||||
char json_data[256];
|
||||
char encrypted_data[512];
|
||||
|
||||
// 1. 构建JSON数据
|
||||
sprintf(json_data,
|
||||
"{\"student_id\":\"%s\",\"name\":\"%s\",\"confidence\":%.2f,\"status\":\"success\"}",
|
||||
student_id, name, confidence);
|
||||
|
||||
printf("[CHECKIN] JSON: %s\r\n", json_data);
|
||||
|
||||
// 2. 加密JSON数据
|
||||
uint32_t encrypted_len = Crypto_EncryptJSON(json_data, encrypted_data);
|
||||
|
||||
if (encrypted_len == 0) {
|
||||
printf("[ERROR] Encryption failed!\r\n");
|
||||
Buzzer_Beep(BEEP_FAIL);
|
||||
OLED_ShowString(4, 1, "Encrypt Error!");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("[CRYPTO] Encrypted length: %u\r\n", encrypted_len);
|
||||
printf("[CRYPTO] Encrypted data: %s\r\n", encrypted_data);
|
||||
|
||||
// 3. 发送加密数据到服务器
|
||||
if (ESP8266_SendEncryptedData(encrypted_data) == 1) {
|
||||
printf("[OK] Data sent successfully\r\n");
|
||||
Buzzer_Beep(BEEP_SUCCESS);
|
||||
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "Check-in OK!");
|
||||
OLED_ShowString(2, 1, "ID:");
|
||||
OLED_ShowString(2, 4, student_id);
|
||||
OLED_ShowString(3, 1, name);
|
||||
|
||||
} else {
|
||||
printf("[ERROR] Send failed\r\n");
|
||||
Buzzer_Beep(BEEP_FAIL);
|
||||
OLED_ShowString(4, 1, "Send Failed!");
|
||||
}
|
||||
|
||||
Delay_ms(2000);
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "Ready...");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
System_Init();
|
||||
|
||||
printf("\r\n========================================\r\n");
|
||||
printf(" STM32 Face Recognition System\r\n");
|
||||
printf(" With AES-128-CBC Encryption\r\n");
|
||||
printf("========================================\r\n");
|
||||
|
||||
// 连接服务器
|
||||
if (ConnectToServer()) {
|
||||
server_connected = 1;
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "System Ready!");
|
||||
OLED_ShowString(2, 1, "Encrypted Mode");
|
||||
printf("[INFO] Server connected!\r\n");
|
||||
} else {
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "Connection");
|
||||
OLED_ShowString(2, 1, "Failed!");
|
||||
printf("[ERROR] Connection failed!\r\n");
|
||||
}
|
||||
|
||||
Delay_ms(1000);
|
||||
OLED_Clear();
|
||||
OLED_ShowString(1, 1, "Please scan");
|
||||
OLED_ShowString(2, 1, "your face...");
|
||||
|
||||
// 主循环
|
||||
while (1) {
|
||||
// 处理K210识别结果
|
||||
if (K210_GetRxFlag()) {
|
||||
char* k210_data = K210_GetRxData();
|
||||
printf("[K210] %s\r\n", k210_data);
|
||||
|
||||
// 解析K210数据(格式:ID:2,NAME:ID2,CONF:0.95,TS:xxx)
|
||||
char student_id[16] = {0};
|
||||
char name[32] = {0};
|
||||
float confidence = 0.0f;
|
||||
|
||||
if (sscanf(k210_data, "ID:%[^,],NAME:%[^,],CONF:%f",
|
||||
student_id, name, &confidence) == 3) {
|
||||
|
||||
printf("[PARSE] ID=%s, Name=%s, Conf=%.2f\r\n",
|
||||
student_id, name, confidence);
|
||||
|
||||
// 检查置信度
|
||||
if (confidence >= 0.75f) {
|
||||
// 执行签到(加密发送)
|
||||
ProcessCheckIn(student_id, name, confidence);
|
||||
} else {
|
||||
printf("[REJECT] Low confidence: %.2f\r\n", confidence);
|
||||
Buzzer_Beep(BEEP_FAIL);
|
||||
}
|
||||
} else if (strstr(k210_data, "UNKNOWN")) {
|
||||
printf("[INFO] Unknown face\r\n");
|
||||
Buzzer_Beep(BEEP_FAIL);
|
||||
}
|
||||
|
||||
K210_ClearRxFlag();
|
||||
}
|
||||
|
||||
Delay_ms(10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user