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

相关推荐
开开心心就好4 分钟前
手机不同App音量自动调节软件
网络·windows·python·安全·智能手机·电脑·音视频
GalaxyPokemon12 分钟前
MySQL基础 [二] - 数据库基础
linux·网络·数据库·mysql
智联视频超融合平台20 分钟前
国网B接口注册流程详解以及注册失败原因(电网B接口)
网络·人工智能·后端·网络协议·安全·音视频·实时音视频
W说编程1 小时前
《UNIX网络编程卷1:套接字联网API》第4章 基本TCP套接字编程
c语言·网络·网络协议·tcp/ip·架构·unix·tcp
互联网之声1 小时前
绿舟与亚马逊云科技达成全方位合作,助力出海品牌绿色可持续发展!
网络
qq_260241233 小时前
宝塔面板使用CDN 部署后获取真实客户端 IP教程
网络·网络协议·tcp/ip
菜只因C3 小时前
深入剖析嵌入式系统:从基础到实践的全面指南
大数据·网络·人工智能
courniche4 小时前
CSMA/CA与CSMA/CD的区别
网络·网络协议·信息与通信·信号处理
niuTaylor4 小时前
嵌入式工程师多线程编程(二)生产者-消费者模式
网络·多线程