实战代码:esp32-cam按钮控制手机拍照V1.0

发布于:2025-04-09 ⋅ 阅读:(28) ⋅ 点赞:(0)

#include <WiFi.h>
#include <HTTPClient.h>

// WiFi设置
const char* ssid = “MIFI-020806-2.4G”;
const char* password = “12345678”;

// 静态IP配置
IPAddress staticIP(192, 168, 1, 32); // 设置为固定IP
IPAddress gateway(192, 168, 1, 1); // 网关地址(路由器地址)
IPAddress subnet(255, 255, 255, 0); // 子网掩码
IPAddress dns1(8, 8, 8, 8); // 主DNS
IPAddress dns2(114, 114, 114, 114); // 备用DNS

// 服务器设置
const char* serverHost = “192.168.1.4”;
const int serverPort = 8081;

// 按钮设置
const int buttonPin = 13;
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

// WiFi设置
unsigned long previousWiFiCheck = 0;
const unsigned long WIFI_CHECK_INTERVAL = 30000; // WiFi检查间隔(30秒)
bool serverConnected = false;

// 检查服务器连接
bool checkServer() {
WiFiClient client;
client.setTimeout(5000); // 5秒超时

Serial.print("检查服务器连接...");

if (client.connect(serverHost, serverPort)) {
    serverConnected = true;
    Serial.println("成功");
    client.stop();
    return true;
} else {
    serverConnected = false;
    Serial.println("失败");
    return false;
}

}

// WiFi连接函数
bool connectWiFi() {
Serial.println(“尝试连接WiFi并设置静态IP…”);

// 断开之前的连接
WiFi.disconnect(true);
delay(500);

// 配置WiFi模式
WiFi.mode(WIFI_STA);
WiFi.setSleep(false);  // 禁用WiFi睡眠模式,提高响应速度

// 配置静态IP
if (!WiFi.config(staticIP, gateway, subnet, dns1, dns2)) {
    Serial.println("静态IP配置失败!");
    return false;
}

// 开始连接
WiFi.begin(ssid, password);

// 等待连接,最多尝试20次
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
    delay(500);
    Serial.print(".");
    attempts++;
}

if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi连接成功!");
    Serial.print("IP地址: ");
    Serial.println(WiFi.localIP());
    
    // 连接成功后检查服务器
    if (checkServer()) {
        return true;
    } else {
        Serial.println("WiFi已连接但服务器无响应");
        return false;
    }
} else {
    Serial.println("\nWiFi连接失败!");
    return false;
}

}

// 维持WiFi连接
void maintainWiFi() {
unsigned long currentMillis = millis();

// 检查WiFi连接
if (currentMillis - previousWiFiCheck >= WIFI_CHECK_INTERVAL) {
    previousWiFiCheck = currentMillis;
    
    if (WiFi.status() != WL_CONNECTED) {
        Serial.println("WiFi连接断开,尝试重连");
        connectWiFi();
    } else {
        Serial.println("WiFi保持连接中");
        // 仅检查WiFi连接,不主动检查服务器
    }
}

}

void takeSnapshot() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println(“WiFi未连接,无法拍照”);
return;
}

// 仅在需要拍照时检查服务器连接
if (!checkServer()) {
    Serial.println("服务器无响应,无法拍照");
    return;
}

HTTPClient http;

Serial.println("开始拍照...");

// 构建URL
String url = "http://" + String(serverHost) + ":" + String(serverPort) + "/getsnapshot";
http.begin(url);

// 添加认证头
http.addHeader("Authorization", "Basic YWRtaW46YWRtaW4="); // admin:admin Base64编码
http.addHeader("Connection", "keep-alive");

// 发送GET请求
int httpCode = http.GET();

// 处理结果
if (httpCode > 0) {
    Serial.printf("HTTP响应代码: %d\n", httpCode);
    
    if (httpCode == HTTP_CODE_OK) {
        Serial.println("拍照成功");
    } else {
        Serial.printf("请求失败,错误代码: %d\n", httpCode);
    }
} else {
    Serial.printf("HTTP请求失败: %s\n", http.errorToString(httpCode).c_str());
}

http.end();

}

// 读取按钮状态,带消抖
bool checkButton() {
int reading = digitalRead(buttonPin);
bool buttonPressed = false;

if (reading != lastButtonState) {
    lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
        buttonState = reading;
        
        if (buttonState == LOW) {
            buttonPressed = true;
        }
    }
}

lastButtonState = reading;
return buttonPressed;

}

void setup() {
Serial.begin(115200);
delay(1000);

// 初始化引脚
pinMode(buttonPin, INPUT_PULLUP);

Serial.println("ESP32-CAM启动");
Serial.println("静态IP设置为: 192.168.1.32");

// 初始化WiFi
connectWiFi();

}

void loop() {
// 仅维持WiFi连接,不进行保活
maintainWiFi();

// 检测按钮
if (checkButton()) {
    Serial.println("按钮被按下,尝试拍照");
    takeSnapshot();
}

delay(10); // 短暂延时减少CPU负载

}

-----------------------代码说明
wifi使用 随身wifi
网关192.168.1.1
esp32-cam使用IP:192.168.1.32
按钮使用GPIO13和GND