#include <Wire.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
// 替换为你的WiFi网络名称和密码
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// LCD地址和端口定义
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
const int BUTTON_PIN = 5; // 按钮连接到GPIO 5
// 凯撒密码偏移量
int caesarShift = 4;
// 凯撒密码加密函数
String caesarCipherEncrypt(String text, int shift) {
String encryptedText = "";
for (int i = 0; i < text.length(); i++) {
if (isalpha(text[i])) {
char base = isupper(text[i]) ? 'A' : 'a';
encryptedText += (text[i] - base + shift) % 26 + base;
} else {
encryptedText += text[i]; // 对非字母字符不做处理
}
}
return encryptedText;
}
void setup() {
Serial.begin(115200);//串口波特率
// 初始化LCD
lcd.init();
lcd.backlight();
pinMode(BUTTON_PIN, INPUT_PULLUP); // 设置BUTTON_PIN为输入并启用内部上拉电阻
// 显示欢迎信息
lcd.setCursor(0, 0);
lcd.print("WiFi Connected!");
// 连接WiFi
WiFi.begin("Wokwi-GUEST","");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
lcd.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// 更新LCD显示
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Button");
}
void loop() {
static const char* originalText = "hsy"; // 需要加密的原始文本
static bool buttonPressed = false; // 用于跟踪按钮按下的状态
if (!buttonPressed && digitalRead(BUTTON_PIN) == LOW) { // 检测按钮是否被按下
buttonPressed = true;
}
// 显示加密结果
if (buttonPressed) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Original: ");
lcd.print(originalText);
String encryptedText = caesarCipherEncrypt(String(originalText), caesarShift);// 加密文本
lcd.setCursor(0, 1);
lcd.print("Encrypted: ");
lcd.print(encryptedText);
}
delay(1000);
}