#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
// 设定 I2C 液晶屏的地址和大小
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
const char* ssid = "Wokwi-GUEST"; // WiFi 热点名称
const char* password = ""; // WiFi 密码
void encrypt(String &text) {
for (int i = 0; i < text.length(); i++) {
text[i] = (char) (text[i] + 3);
}
}
void decrypt(String &text) {
for (int i = 0; i < text.length(); i++) {
text[i] = (char) (text[i] - 3);
}
}
void setup() {
// 初始化串行通信
Serial.begin(115200);
// 初始化液晶屏
lcd.init();
// 设置光标位置
lcd.setCursor(0, 0);
// 显示欢迎信息
lcd.print("Hello LBL!");
// 连接到 WiFi
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (Serial.available()) {
// 读取串行数据
String input = Serial.readStringUntil('\n');
String encrypted = input;
encrypt(encrypted);
String decrypted = encrypted;
decrypt(decrypted);
lcd.setCursor(0, 0);
lcd.print("jiami: " + encrypted);
lcd.setCursor(0, 1);
lcd.print("jiemi: " + decrypted);
// 回显输入数据到串行监视器
Serial.println(input);
}
delay(100); // 稍微延时以减少 CPU 占用
}