#include <WiFi.h>  // 引入WiFi库
#include <Wire.h>  // I2C 库
#include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C 库
// 定义WiFi网络名称和密码
const char* ssid = "Wokwi-GUEST";   // 替换为你的WiFi名称
const char* password = "";  // 替换为你的WiFi密码

// 定义LCD的I2C地址和引脚参数
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 0x27是LCD的I2C地址,16是列数,2是行数
// 初始化LED灯连接的GPIO引脚
const int ledPin = 25; // LED连接到GPIO 25
// 凯撒密码加密函数
String caesarEncrypt(const String& text, int shift) {
   String encrypted = "";
    for (int i = 0; i < text.length(); i++) {
         char c = text[i];
   if (isalpha(c)) {
              char base = isupper(c) ? 'A' : 'a';
               encrypted += (c - base + shift) % 26 + base;
               } else
                encrypted += c; // 对于非字母字符不加密
                }
    return encrypted;
    }


void setup() {
  Serial.begin(115200);// 初始化串口通信
  pinMode(ledPin, OUTPUT); // 设置LED引脚为输出模式
  // 初始化LCD
  lcd.init();
  lcd.backlight();// 打开背光
  lcd.print("Enter text:"); // 在LCD上打印提示信息
  WiFi.begin(ssid, password);
  // 等待WiFi连接
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000); 
   Serial.print(".");
    }
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
// 连接状态LED指示
if (WiFi.status() != WL_CONNECTED) {
 digitalWrite(ledPin, LOW);
} else {
 // 连接成功,保持LED状态,无需重复检查和点亮
 }
if (Serial.available() > 0) { // 检查是否有串口数据可用
 String inputText = Serial.readStringUntil('\n'); // 读取一行文本,直到遇到换行符
 inputText.trim(); // 去除可能的首尾空格
// 加密文本
 String encryptedMessage = caesarEncrypt(inputText,3);
 lcd.clear(); // 清空LCD屏幕
 lcd.print("miwen: "); // 打印标题
 lcd.setCursor(0, 1); // 将光标移动到第二行
 lcd.print(encryptedMessage); // 打印加密后的文本
 }

 }