实战代码:esp32-cam按钮控制手机拍照V1.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

相关推荐
wanhengidc1 小时前
云手机 高振畅玩不踩坑
运维·服务器·安全·web安全·智能手机
易连EDI—EasyLink1 小时前
易连EDI–EasyLink实现OCR智能数据采集
网络·人工智能·安全·汽车·ocr·edi
@insist1232 小时前
信息安全工程师考点精讲:身份认证核心原理与分类体系(上篇)
大数据·网络·分类·信息安全工程师·软件水平考试
SmartRadio2 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
_.Switch2 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
金色光环3 小时前
FreeModbus释放底层的 TCP 监听端口
服务器·网络·tcp/ip
数智化精益手记局3 小时前
拆解物料管理erp系统的核心功能,看物料管理erp系统如何解决库存积压与缺料难题
大数据·网络·人工智能·安全·信息可视化·精益工程
灰子学技术5 小时前
Envoy HTTP 过滤器处理技术文档
网络·网络协议·http
非凡ghost6 小时前
可拓浏览器:给手机浏览器装上“外挂“!2W+拓展+AI搜索,玩出无限可能!
windows·智能手机·音视频·firefox
Olivia051405147 小时前
Voohu:音频变压器的屏蔽接地技术对50Hz工频噪声抑制的影响
网络·机器人·信息与通信