实战代码: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

相关推荐
一梦浮华25 分钟前
自学嵌入式 day 42 串口通信
网络
Runner.DUT40 分钟前
SRIO入门之官方例程仿真验证
服务器·网络·数据库
慕y27442 分钟前
Java学习第一百零一部分——网关(Gateway)
java·网络·学习
Volunteer Technology44 分钟前
13015计算机系统原理-速记宝典
运维·网络·考研·总线·计算机系统原理·中央处理器
是阿建吖!2 小时前
【Linux | 网络】网络层(IP协议、NAT技术和ICMP协议)
linux·网络·tcp/ip
百川8 小时前
CMS框架漏洞
网络·安全·web安全
Bruce_Liuxiaowei8 小时前
融媒体中心网络安全应急预案(通用技术框架)
网络·web安全·网络安全·媒体
渡我白衣10 小时前
Linux网络编程:基于UDP 的聊天室雏形
linux·网络·udp
数据与人工智能律师10 小时前
智能合约漏洞导致的损失,法律责任应如何分配
大数据·网络·人工智能·算法·区块链
渡我白衣11 小时前
Linux网络编程:UDP 的echo server
linux·网络·udp