Created
December 3, 2020 04:48
-
-
Save MarsTechHAN/148586ad0c57359936a111e5c2e92de4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Rui Santos | |
Complete project details at https://RandomNerdTutorials.com/esp32-cam-post-image-photo-server/ | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
#include <Arduino.h> | |
#include <WiFi.h> | |
#include "soc/soc.h" | |
#include "soc/rtc_cntl_reg.h" | |
#include "esp_camera.h" | |
#include "HTTPClient.h" | |
#include "battery.h" | |
#include "led.h" | |
#include "bmm8563.h" | |
#include "camera_pins.h" | |
const char* ssid = "WIFI_SSID"; | |
const char* password = "WIFI_PASS"; | |
String serverName = "http://YOUR_BUCKET_NAME.oss-cn-shenzhen.aliyuncs.com"; // REPLACE WITH YOUR Raspberry Pi IP ADDRESS | |
//String serverName = "example.com"; // OR REPLACE WITH YOUR DOMAIN NAME | |
const int serverPort = 80; | |
#include "EEPROM.h" | |
const int timerInterval = 30000; // time between each HTTP POST image | |
unsigned long previousMillis = 0; // last time image was sent | |
void setup() { | |
led_init(CAMERA_LED_GPIO); | |
bat_init(); | |
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); | |
Serial.begin(115200); | |
Serial.setDebugOutput(true); | |
WiFi.mode(WIFI_STA); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
int wait_time = 10; | |
while (WiFi.status() != WL_CONNECTED && wait_time) { | |
wait_time--; | |
Serial.print("."); | |
delay(500); | |
} | |
if(wait_time == 0){ | |
bmm8563_init(); | |
bmm8563_setTimerIRQ(60); | |
bat_disable_output(); | |
esp_deep_sleep(60000000); | |
esp_deep_sleep_start(); | |
} | |
Serial.println(); | |
Serial.print("ESP32-CAM IP Address: "); | |
Serial.println(WiFi.localIP()); | |
camera_config_t config; | |
config.ledc_channel = LEDC_CHANNEL_0; | |
config.ledc_timer = LEDC_TIMER_0; | |
config.pin_d0 = Y2_GPIO_NUM; | |
config.pin_d1 = Y3_GPIO_NUM; | |
config.pin_d2 = Y4_GPIO_NUM; | |
config.pin_d3 = Y5_GPIO_NUM; | |
config.pin_d4 = Y6_GPIO_NUM; | |
config.pin_d5 = Y7_GPIO_NUM; | |
config.pin_d6 = Y8_GPIO_NUM; | |
config.pin_d7 = Y9_GPIO_NUM; | |
config.pin_xclk = XCLK_GPIO_NUM; | |
config.pin_pclk = PCLK_GPIO_NUM; | |
config.pin_vsync = VSYNC_GPIO_NUM; | |
config.pin_href = HREF_GPIO_NUM; | |
config.pin_sscb_sda = SIOD_GPIO_NUM; | |
config.pin_sscb_scl = SIOC_GPIO_NUM; | |
config.pin_pwdn = PWDN_GPIO_NUM; | |
config.pin_reset = RESET_GPIO_NUM; | |
config.xclk_freq_hz = 20000000; | |
config.pixel_format = PIXFORMAT_JPEG; | |
config.frame_size = FRAMESIZE_SXGA; | |
config.jpeg_quality = 5; //0-63 lower number means higher quality | |
config.fb_count = 2; | |
// camera init | |
esp_err_t err = esp_camera_init(&config); | |
if (err != ESP_OK) { | |
Serial.printf("Camera init failed with error 0x%x", err); | |
bmm8563_init(); | |
bmm8563_setTimerIRQ(60); | |
bat_disable_output(); | |
esp_deep_sleep(60000000); | |
esp_deep_sleep_start(); | |
} | |
EEPROM.begin(64); | |
uint32_t image_idx = 0; | |
if(EEPROM.read(0) != 116){ | |
EEPROM.write(0, 116); | |
for(int i=1; i<5; i++){ | |
EEPROM.write(i, 0); | |
} | |
EEPROM.commit(); | |
} | |
for(int i=0; i<4; i++){ | |
((uint8_t *)(&image_idx))[i] = EEPROM.read(i+1); | |
} | |
uint64_t chip_id = ESP.getEfuseMac(); | |
char filename_str[100] = ""; | |
sprintf(filename_str, "/esp32_image_%04X%08X_idx_%zu_bat_%zu.jpg", (uint16_t)(chip_id>>32), (uint32_t)chip_id, image_idx, bat_get_voltage()); | |
String filename = String(filename_str); | |
sendPhoto(filename); | |
image_idx += 1; | |
for(int i=1; i<5; i++){ | |
EEPROM.write(i, ((uint8_t *)(&image_idx))[i-1]); | |
} | |
EEPROM.commit(); | |
bmm8563_init(); | |
bmm8563_setTimerIRQ(60); | |
bat_disable_output(); | |
esp_deep_sleep(60000000); | |
esp_deep_sleep_start(); | |
} | |
void loop() { | |
} | |
void sendPhoto(String filename) { | |
HTTPClient http; | |
camera_fb_t * fb = NULL; | |
fb = esp_camera_fb_get(); | |
if(!fb) { | |
Serial.println("Camera capture failed"); | |
delay(1000); | |
ESP.restart(); | |
} | |
Serial.println("Connecting to server: " + serverName); | |
Serial.println("Connection successful!"); | |
uint16_t imageLen = fb->len; | |
http.begin(serverName + filename); | |
http.addHeader("Content-Type", "image/jpeg"); | |
int httpResponseCode = http.PUT(fb->buf, fb->len); | |
if(httpResponseCode > 0) { | |
Serial.printf("[HTTP] PUT... code: %d\n", httpResponseCode); | |
HTTPClient http_text; | |
http_text.begin(serverName + String("/newest_image_file.txt")); | |
http_text.addHeader("Content-Type", "text/plain"); | |
http_text.PUT(filename); | |
} else { | |
Serial.printf("Upload file error, %s\n", http.errorToString(httpResponseCode).c_str()); | |
} | |
esp_camera_fb_return(fb); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment