#include <WiFi.h>
#include <WebServer.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <qrcode_espi.h>
// 屏幕设置
TFT_eSPI display = TFT_eSPI();
QRcode_eSPI qrcode(&display);
// WiFi设置
const char* ssid = "@Ruijie-s8622";      // 替换为你的WiFi名称
const char* password = ""; // 替换为你的WiFi密码
// Web服务器设置
WebServer server(80);
// 默认显示内容
String qrContent = "http://default.url";
String line1Text = "XHP095060020FGMSY";
String line2Text = "Qty. 298500PCS";
void handleRoot() {
  String html = "<!DOCTYPE html><html><head><title>二维码生成器</title>";
  html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  html += "<meta charset=\"utf-8\">";
  html += "<style>body{font-family:Arial,sans-serif;margin:20px;}";
  html += "form{max-width:500px;margin:0 auto;}";
  html += "label{display:block;margin-top:10px;}";
  html += "input,textarea{width:100%;padding:8px;margin-top:5px;box-sizing:border-box;}";
  html += "button{background:#4CAF50;color:white;padding:10px 15px;border:none;cursor:pointer;margin-top:15px;}</style></head>";
  html += "<body><h1>二维码生成器</h1>";
  html += "<form action=\"/generate\" method=\"POST\">";
  html += "<label for=\"qr\">二维码内容:</label>";
  html += "<input type=\"text\" id=\"qr\" name=\"qr\" value=\"" + qrContent + "\" required>";
  html += "<label for=\"line1\">第一行文字:</label>";
  html += "<input type=\"text\" id=\"line1\" name=\"line1\" value=\"" + line1Text + "\" required>";
  html += "<label for=\"line2\">第二行文字:</label>";
  html += "<input type=\"text\" id=\"line2\" name=\"line2\" value=\"" + line2Text + "\" required>";
  html += "<button type=\"submit\">生成二维码</button>";
  html += "</form></body></html>";
 
  server.send(200, "text/html", html);
}
void handleGenerate() {
  if (server.hasArg("qr") && server.hasArg("line1") && server.hasArg("line2")) {
    qrContent = server.arg("qr");
    line1Text = server.arg("line1");
    line2Text = server.arg("line2");
   
    // 更新屏幕显示
    updateDisplay();
   
    server.send(200, "text/plain; charset=utf-8", "二维码已更新!");
  } else {
    server.send(400, "text/plain; charset=utf-8", "缺少参数");
  }
}
void updateDisplay() {
  display.fillScreen(TFT_WHITE); // 清屏
  qrcode.init();
 
  // 创建二维码
  qrcode.create(qrContent.c_str());
 
  // 等待二维码绘制完成
  delay(500);
 
  // 计算文字位置(假设二维码高度为120像素)
  int textStartY = 120 + 8; // 二维码下方8像素
 
  // 设置文字样式
  display.setTextColor(TFT_BLACK, TFT_WHITE);
  display.setTextSize(1);
 
  // 第一行文字
  display.setCursor(5, textStartY);
  display.println(line1Text);
 
  // 第二行文字
  display.setCursor(5, textStartY + 10);
  display.println(line2Text);
}
void setup() {
  // 初始化串口
  Serial.begin(115200);
 
  // 初始化屏幕
  display.begin();
  display.fillScreen(TFT_WHITE);
  display.setTextColor(TFT_BLACK, TFT_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.println("正在连接WiFi...");
 
  // 连接WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    display.print(".");
  }
 
  // 显示连接信息
  display.fillScreen(TFT_WHITE);
  display.setCursor(0, 0);
  display.println("WiFi已连接!");
  display.print("IP地址: ");
  display.println(WiFi.localIP());
 
  //连接成功后,5秒内显示分配的IP地址
  delay(5000);
  // 设置Web服务器路由
  server.on("/", handleRoot);
  server.on("/generate", HTTP_POST, handleGenerate);
 
  // 启动服务器
  server.begin();
 
  // 初始显示
  updateDisplay();
}
void loop() {
  server.handleClient(); // 处理客户端请求
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。