感谢提供清晰的思路,3月15日发布的内容,非常好。
image.png
1、主要思路
esp32-cam定时抓图生成16进制字符串,通过esp32 走mqtt通道上报给emq(参见前文),python订阅mqtt主题把16进制转成所需图片保存;
2、定时抓图代码
初始化相机,设定jpg格式,精度和大小可自行调整;
void setupCamera() {
const camera_config_t config = {
.pin_pwdn = kCameraPin_PWDN,
.pin_reset = kCameraPin_RESET,
.pin_xclk = kCameraPin_XCLK,
.pin_sscb_sda = kCameraPin_SIOD,
.pin_sscb_scl = kCameraPin_SIOC,
.pin_d7 = kCameraPin_Y9,
.pin_d6 = kCameraPin_Y8,
.pin_d5 = kCameraPin_Y7,
.pin_d4 = kCameraPin_Y6,
.pin_d3 = kCameraPin_Y5,
.pin_d2 = kCameraPin_Y4,
.pin_d1 = kCameraPin_Y3,
.pin_d0 = kCameraPin_Y2,
.pin_vsync = kCameraPin_VSYNC,
.pin_href = kCameraPin_HREF,
.pin_pclk = kCameraPin_PCLK,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_SVGA,
.jpeg_quality = 10,
.fb_count = 1,
};
esp_err_t err = esp_camera_init(&config);
Serial.printf("esp_camera_init: 0x%x\n", err);
// sensor_t *s = esp_camera_sensor_get();
// s->set_framesize(s, FRAMESIZE_QVGA);
}
定时抓图,fb->buf就是16进制图片数据;
时间设定20秒,可自行调整间隔时间长短;
void loop() {
// 抓拍图片
camera_fb_t *fb = esp_camera_fb_get();
delay(20000); // [ms]
}
3、通过wifi方式上报mqtt
增加arduino mqtt库;
https://github.com/knolleary/pubsubclient
初始化mqtt,配置emq的ip地址和端口
void setupMqtt() {
g_pub_sub_client.setServer(kMqttServerAddress, kMqttServerPort);
}
抓拍上报代码,上报主题为test;
void loop() {
handleMqtt();
// 抓拍图片
camera_fb_t *fb = esp_camera_fb_get();
if ( fb ) {
Serial.printf("width: %d, height: %d, buf: 0x%x, len: %d\n", fb->width, fb->height, fb->buf, fb->len);
g_pub_sub_client.publish("test", fb->buf, fb->len);
esp_camera_fb_return(fb);
}
delay(20000); // [ms]
}
5、测试接收数据
image.png正常接收esp32-cam上报的数据;
6、python 16进制字符转jpg
手工试一下是否可行;复制从emq接受的数据,粘贴到data即可;
import binascii
data="FFD8FFE000104A46494600010101000000000000FFDB0043000A07080908060A0908090B0B0A0C0F19100F0E0E0F1F161712192420262624202322282D3A31282B362B2223324433363B3D4041402730474C463F4B3A3F403EFFDB0043010B0B0B0F0D0F1D10101D3E2923293E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3E3EFFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA29A0558C3140A00754A8A68623A0B4E228573F5AD78A4315C8940AC645A343FB4B03941F9D2AEA01FA2
......(数据太长省略)
FEB59DCAB1FFFD9"
data=data.strip()
data=data.replace(' ', '')
data=data.replace('\n', '')
data = binascii.a2b_hex(data)
with open('image.jpg', 'wb') as image_file:
image_file.write(data)
查看图片
image.png
可以自行加入python订阅相关的mqtt主题,然后根据子项的id,日期保存图片。
类似/test/1358852,1358852+日期为图片名字。
完整后端思路。
全文完;
网友评论