ESP32cam--上传JPEG格式图片到服务器
程序员文章站
2024-02-24 10:23:58
...
在这个的基础上ESP32cam–获取图片通过串口传到上位机上修改,并通过TCP、UDP上传到服务器。
连接WiFi、TCP实例
#include <WiFi.h>
const char *ssid = "********";
const char *password = "********";
const IPAddress serverIP(192,168,50,14); //欲访问的地址
uint16_t serverPort = 50037; //服务器端口号
WiFiClient client; //声明一个客户端对象,用于与服务器进行连接
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.setSleep(false); //关闭STA模式下wifi休眠,提高响应速度
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
}
void loop()
{
Serial.println("尝试访问服务器");
if (client.connect(serverIP, serverPort)) //尝试访问目标地址
{
Serial.println("访问成功");
client.print("Hello world!"); //向服务器发送数据
while (client.connected() || client.available()) //如果已连接或有收到的未读取的数据
{
if (client.available()) //如果有数据可读取
{
String line = client.readStringUntil('\n'); //读取数据到换行符
Serial.print("读取到数据:");
Serial.println(line);
client.write(line.c_str()); //将收到的数据回发
}
}
Serial.println("关闭当前连接");
client.stop(); //关闭客户端
}
else
{
Serial.println("访问失败");
client.stop(); //关闭客户端
}
delay(5000);
}
UDP实例
#include <WiFi.h>
#include <WiFiUdp.h> //引用以使用UDP
const char *ssid = "********";
const char *password = "********";
WiFiUDP Udp; //创建UDP对象
unsigned int localUdpPort = 2333; //本地端口号
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (!WiFi.isConnected())
{
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
Udp.begin(localUdpPort); //启用UDP监听以接收数据
}
void loop()
{
int packetSize = Udp.parsePacket(); //获取当前队首数据包长度
if (packetSize) //如果有数据可用
{
char buf[packetSize];
Udp.read(buf, packetSize); //读取当前包数据
Serial.println();
Serial.print("Received: ");
Serial.println(buf);
Serial.print("From IP: ");
Serial.println(Udp.remoteIP());
Serial.print("From Port: ");
Serial.println(Udp.remotePort());
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //准备发送数据
Udp.print("Received: "); //复制数据到发送缓存
Udp.write((const uint8_t*)buf, packetSize); //复制数据到发送缓存
Udp.endPacket(); //发送数据
}
}
UDP发送图片
#include "esp_camera.h"
#include <WiFi.h>
#include "AsyncUDP.h"
const char *ssid = "wxy2018";
const char *password = "wxy168168";
AsyncUDP udp; //创建UDP对象
int buttonState = 0;
int btnHold = 0;
#define KEY_PIN 15
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
static camera_config_t camera_config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_XGA,
.jpeg_quality = 12,
.fb_count = 1,
};
esp_err_t camera_init() {
//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
Serial.print("Camera Init Failed");
return err;
}
sensor_t * s = esp_camera_sensor_get();
//initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV2640_PID) {
// s->set_vflip(s, 1);//flip it back
// s->set_brightness(s, 1);//up the blightness just a bit
// s->set_contrast(s, 1);
}
Serial.print("Camera Init OK");
return ESP_OK;
}
void wifi_init(void)
{
WiFi.mode(WIFI_STA);
// WiFi.setSleep(false); //关闭STA模式下wifi休眠,提高响应速度
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.print("IP Address:");
Serial.println(WiFi.localIP());
if(udp.connect(IPAddress(192,168,0,103), 8888))
{
Serial.println("UDP connected");
udp.print("Hello Server!");
}
}
void setup() {
Serial.begin(115200);
camera_init(); // 摄像头初始化
wifi_init(); // WiFi初始化
pinMode(KEY_PIN, INPUT_PULLUP); //设置按键管脚输入上拉模式
Serial.println("sys is running!");
}
void loop()
{
buttonState = digitalRead(KEY_PIN); //读取按键返回状态值
if (buttonState == LOW && btnHold == 0)
{ // 若按键被按下
delay(100); //等待跳过按键抖动的不稳定过程
if (buttonState == LOW) // 若按键被按下
{
Serial.print("click\r\n");
btnHold = 1;
//acquire a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb)
{
Serial.print( "Camera capture failed");
}
else
{
// TODO
udp.write(fb->buf, fb->len); //复制数据到发送缓存
Serial.println("succes to send image for UDP");
//return the frame buffer back to the driver for reuse
esp_camera_fb_return(fb);
}
}
}
if (buttonState == HIGH)
{
btnHold = 0;
}
}
按下按键,UDP服务器接收到了JPEG格式的图片了!然而,数据长度却只有1436个字节!看来还要分几个数据包才行啊!