1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
#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"
#include <WebServer.h>
WebServer server(80);
const char* ntpServer = "cn.pool.ntp.org"; const long gmtOffset_sec = 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";
#define ssid "MERCURY" #define password "11235813"
void connectToNetwork(){ WiFi.begin(ssid,password); WiFi.setAutoReconnect(true); while (WiFi.status()!= WL_CONNECTED) { delay(1000); Serial.println("try to connecting ..."); } Serial.println("Connectedto network"); }
void wifi_connect(){ connectToNetwork(); Serial.println(WiFi.macAddress()); Serial.println(WiFi.localIP()); Serial.println("wifi连接成功"); }
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_UXGA, .jpeg_quality = 10, .fb_count = 1
};
esp_err_t camera_init() { 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(); if (s->id.PID == OV2640_PID) { } Serial.print("Camera Init OK"); return ESP_OK; }
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname); if(!root){ Serial.println("Failed to open directory"); return; } if(!root.isDirectory()){ Serial.println("Not a directory"); return; }
File file = root.openNextFile(); while(file){ if(file.isDirectory()){ Serial.print(" DIR : "); Serial.print (file.name()); time_t t= file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); if(levels){ listDir(fs, file.name(), levels -1); } } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print(" SIZE: "); Serial.print(file.size()); time_t t= file.getLastWrite(); struct tm * tmstruct = localtime(&t); Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec); } file = root.openNextFile(); } }
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); Serial.printf("SD 卡容量大小: %lluMB\n", cardSize);
Serial.println("现在列出目前SD卡内部根目录下所有的路径:"); listDir(SD_MMC, "/", 0); }
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.print("现在时间是:");
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); Serial.println(timr_str); } }
void handle_OnConnect() {
server.send(200, "text/html", SendHTML(timr_str)); }
void handle_NotFound(){ server.send(404, "text/plain", "Not found"); }
void webserver_init(){ server.on("/", handle_OnConnect); server.onNotFound(handle_NotFound);
server.begin(); Serial.println("HTTP server started"); }
void setup() { Serial.begin(115200); wifi_connect(); camera_init(); sd_init(); webserver_init(); }
void loop() { delay(1000); get_time_path(); server.handleClient();
while (timeinfo.tm_sec == 0) { delay(1000); get_time_path();
if (timeinfo.tm_min == 30) { Serial.println("Resetting ESP"); ESP.restart(); } else { Serial.println("即将进行拍照!!!"); camera_fb_t * fb = esp_camera_fb_get(); Serial.print("存入的图片名称为:");Serial.println(timr_str); path = timr_str; if(fb == NULL) { Serial.println( "get picture failed"); } else { 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); Serial.println("成功写入照片"); } esp_camera_fb_return(fb); } } } }
String SendHTML(char* timr_str){ String ptr = "<!DOCTYPE html> <html>\n"; ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; ptr +="<title>ESP32CAM STATUS</title>\n"; ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"; ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n"; ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n"; ptr +="</style>\n"; ptr +="</head>\n"; ptr +="<body>\n"; ptr +="<div id=\"webpage\">\n"; ptr +="<h1>ESP32CAM PIC INFOMATION</h1>\n"; ptr +="<p>TIME: "; ptr +=timr_str;
ptr +="</div>\n"; ptr +="</body>\n"; ptr +="</html>\n"; return ptr; }
|