上两篇博文分别ESP32_CAM获取图片以及ESP32_CAM上面那个SD卡的使用方法,而这一篇教大家获取图片之后以当前时间为文件名保存到我们的SD卡里面,然后大家就可以用读卡器看到里面的图片了(ESP32_CAM好像对SD卡大小有要求,不能太大)
OK,直接上代码,已经跑过了的,大家自己看看吧
#include "esp_camera.h"
#include "FS.h"
#include <SPI.h>
#include <SD.h>
#include "SD_MMC.h"
#include <time.h>
#include <WiFi.h>
#include "string.h"
// https://blog.csdn.net/LinQiPing233/article/details/83615058 -- 这个是时区的详情
const char* ntpServer = "cn.pool.ntp.org"; //pool.ntp.org为获取时间得接口,可以尝试更多得接口。比如微软的time.windows.com,美国国家标准与技术研究院的time.nist.gov
const long gmtOffset_sec = 8*60*60;//这里采用UTC计时,中国为东八区,就是 8*60*60
const int daylightOffset_sec = 0*60*60;
struct tm timeinfo; //创建一个结构体用于存储时间
char * path = "/2020_6_18_TIME_19_0_47.jpg";
char timr_str[40] = "test.jpg";
//以下是WIFI的链接用户名和密码
#define ssid "b123"
#define password "cx123456"
//*********************************************************************************************************************** wifi连接
String translateEncryptionType(wifi_auth_mode_t encryptionType) { //获取wifi的特性
switch (encryptionType){
case (WIFI_AUTH_OPEN):
return "Open";
case (WIFI_AUTH_WEP):
return "WEP";
case (WIFI_AUTH_WPA_PSK):
return "WPA_PSK";
case (WIFI_AUTH_WPA2_PSK):
return "WPA2_PSK";
case (WIFI_AUTH_WPA_WPA2_PSK):
return "WPA_WPA2_PSK";
case (WIFI_AUTH_WPA2_ENTERPRISE):
return "WPA2_ENTERPRISE";
}
}
void scanNetworks(){ //搜索附近的WIFI源并打印出来
int numberOfNetworks= WiFi.scanNetworks();
Serial.print("Numberof networks found: ");
Serial.println(numberOfNetworks);
for (int i = 0; i< numberOfNetworks; i++) {
Serial.print("Networkname: ");
Serial.println(WiFi.SSID(i));
Serial.print("Signalstrength: ");
Serial.println(WiFi.RSSI(i));
Serial.print("MACaddress: ");
Serial.println(WiFi.BSSIDstr(i));
Serial.print("Encryptiontype: ");
String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
Serial.println(encryptionTypeDescription);
Serial.println("-----------------------");
}
}
void connectToNetwork(){
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.println("try to connecting ...");
}
Serial.println("Connectedto network");
}
void wifi_connect(){
scanNetworks(); //搜索附近的wifi信号并且打印出来
connectToNetwork(); //链接到wifi
Serial.println(WiFi.macAddress()); //打印出mac地址
Serial.println(WiFi.localIP()); //打印出本地ip地址
Serial.println("wifi连接成功"); //打印出本地ip地址
//WiFi.disconnect(true); //断开wifi
//Serial.println(WiFi.localIP());
}
//************************************************************************************************************************************** 摄像头初始化参数
static camera_config_t camera_config = {
.pin_pwdn = 32,
.pin_reset = -1,
.pin_xclk = 0,
.pin_sscb_sda = 26,
.pin_sscb_scl = 27,
.pin_d7 = 35,
.pin_d6 = 34,
.pin_d5 = 39,
.pin_d4 = 36,
.pin_d3 = 21,
.pin_d2 = 19,
.pin_d1 = 18,
.pin_d0 = 5,
.pin_vsync = 25,
.pin_href = 23,
.pin_pclk = 22,
.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;
}
//************************************************************************************************************************************** SD卡初始化参数
void sd_init()
{
if(!SD_MMC.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
}
else if(cardType == CARD_SD){ Serial.println("SDSC"); }
else if(cardType == CARD_SDHC){ Serial.println("SDHC"); }
else { Serial.println("UNKNOWN"); }
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024); //获取SD卡大小,大小单位是MB
Serial.printf("SD 卡容量大小: %lluMB\n", cardSize);
}
//************************************************************************************************************************************** 获取当前时间作为返回的函数部分
void get_time_path() //获取目前时间返回字符串
{
memset(timr_str, 0, sizeof(timr_str));
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
sprintf(timr_str, "/TIME_get_failed.jpg");
}
else
{
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("现在时间是:");
Serial.print("年份是:");Serial.println(timeinfo.tm_year + 1900);
Serial.print("月份是:");Serial.println(timeinfo.tm_mon + 1);
Serial.print("号数是:");Serial.println(timeinfo.tm_mday);
Serial.print("小时数是:");Serial.println(timeinfo.tm_hour ); //中国区
Serial.print("分钟数是:");Serial.println(timeinfo.tm_min);
Serial.print("分钟数是:");Serial.println(timeinfo.tm_sec);
sprintf(timr_str, "/%d_%d_%d_TIME_%d_%d_%d.jpg",timeinfo.tm_year+1900,timeinfo.tm_mon + 1,timeinfo.tm_mday,timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);
}
}
//************************************************************************************************************************************** 主函数部分
void setup() {
Serial.begin(115200);
wifi_connect();
camera_init(); //摄像头初始化
sd_init(); //SD卡初始化
pinMode(16, INPUT_PULLUP); //设置按键管脚输入上拉模式
Serial.println("16号IO拉低将会进行拍照!!!");
}
void loop()
{
if(digitalRead(16) == 0)
{
delay(60); //延时消抖
if(digitalRead(16) == 0)
{
//这里面的舵机就代表确认拉低IO16之后,我们将要拍照并且把图片保存到SD卡当中,而照片名字就保存为目前的时间
Serial.println("即将进行拍照!!!");
camera_fb_t * fb = esp_camera_fb_get();
get_time_path();
Serial.print("存入的图片名称为:");Serial.println(timr_str);
path = timr_str;
if(fb == NULL)
{
Serial.println( "get picture failed"); //代表获取图片失败
}
else
{
//char * path = time_str;
//以下就是上一篇文章讲到的写入文件得到过程,但是一点点小区别
fs::FS &fs = SD_MMC;
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
Serial.println("文件创建失败");
}
else
{
file.write(fb->buf , fb->len); //payload , lengte vd payload
Serial.println("成功写入照片");
}
//return the frame buffer back to the driver for reuse
esp_camera_fb_return(fb);
}
}
}
}
最后
接下来会继续更一些ESP32驱动一些外设的代码,还会更最重要的,ESP32连接阿里云物联网平台的方法(onenet我不喜欢用,也觉得不如阿里云,就是好像阿里贵一些,一分钱一分货把)