/*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
bool wifiConnected= false;
String addCloth(String code) {
String formData = "content="+code; // 替换为您要发送的form-data格式的数据
HTTPClient http;
http.useHTTP10(true);
http.begin(url);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(formData);
String result = http.getString();
http.end();
return "success";
}
void getCode(String code) {
tft.fillRect(100, 100, 200, 60, ILI9341_BLACK); // 清除之前的文本
tft.setCursor(100, 100);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(code);
String data = addCloth(code);
tft.setCursor(100, 130);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println(data);
}
void printWifiStatus(){
tft.setCursor(100, 40);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
if(WiFi.status()==WL_CONNECTED){
tft.fillRect(100, 40, 200, 30, ILI9341_BLACK); // 清除之前的文本
tft.setCursor(100, 40);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Connected");
}
}
}
void toastMsg(String msg){
tft.fillRect(0, 0,320, 30, ILI9341_RED); // 清除之前的文本
// 绘制WiFi连接状态
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(msg);
}
bool logined= false;
void login(String code){
if(code=="123456"){
tft.setCursor(100, 70);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("zhangsan");
toastMsg("user login");
logined= true;
tft.setCursor(10, 100);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Code:");
tft.setCursor(10, 130);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Return:");
}else{
toastMsg("user card error!");
}
}
void reLogin(){
tft.fillRect(100, 70,320, 30, ILI9341_BLACK);
toastMsg("please login!");
logined= false;
tft.fillRect(10, 100,320, 60, ILI9341_BLACK);
}
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
Serial.begin(9600);
tft.fillRect(0, 0,320, 30, ILI9341_RED);
// 绘制WiFi连接状态
tft.setCursor(10, 40);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("WiFi:");
printWifiStatus();
tft.setCursor(10, 70);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(2);
tft.println("User:");
reLogin();
}
void loop() {
if (Serial.available() > 0) {
String inputString = Serial.readString(); // 读取串口输入的字符串
if (inputString.charAt(inputString.length() - 1) == '\r') {
inputString = inputString.substring(0, inputString.length() - 1); // 去除回车符
}else if (inputString.charAt(inputString.length() - 1) == '\n') {
inputString = inputString.substring(0, inputString.length() - 1); // 去除回车符
}
if(logined){
getCode( inputString);
}else{
login(inputString);
}
}
if (digitalRead(BTN_PIN) == LOW) {
reLogin();
}
}