Last active
March 19, 2019 11:15
-
-
Save sonkm3/4f40386ae1f5a58f2172b7fa9d5f269d to your computer and use it in GitHub Desktop.
This file contains 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
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <WebServer.h> | |
#include <ESPmDNS.h> | |
#include <Preferences.h> | |
#include <esp_camera.h> | |
#include <img_converters.h> | |
#define APP_NAME "esp32camera" | |
Preferences preferences; | |
char ssid_buffer[20]; | |
char password_buffer[20]; | |
IPAddress myIP; | |
WebServer server ( 80 ); | |
#define PWDN_GPIO_NUM -1 | |
#define RESET_GPIO_NUM 15 | |
#define XCLK_GPIO_NUM 27 | |
#define SIOD_GPIO_NUM 22 | |
#define SIOC_GPIO_NUM 23 | |
#define Y9_GPIO_NUM 19 | |
#define Y8_GPIO_NUM 36 | |
#define Y7_GPIO_NUM 18 | |
#define Y6_GPIO_NUM 39 | |
#define Y5_GPIO_NUM 5 | |
#define Y4_GPIO_NUM 34 | |
#define Y3_GPIO_NUM 35 | |
#define Y2_GPIO_NUM 32 | |
#define VSYNC_GPIO_NUM 25 | |
#define HREF_GPIO_NUM 26 | |
#define PCLK_GPIO_NUM 21 | |
void setup() { | |
// put your setup code here, to run once: | |
IPAddress myIP; | |
Serial.begin(115200); | |
Serial.setDebugOutput(true); | |
preferences.begin(APP_NAME, false); | |
connectWiFi(); | |
Serial.println("wifi status " + String(WiFi.status())); | |
Serial.println(myIP); | |
if (MDNS.begin(APP_NAME)) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/setup", handleSetup); | |
server.on("/setwifi", handleSetWifi); | |
server.on("/reconnectwifi", handleReconnectWifi); | |
server.on("/image", handleImage); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println ( "HTTP server started" ); | |
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_VGA; | |
config.jpeg_quality = 10; | |
config.fb_count = 10; | |
//config.wb_mode = 1;//0 - 4 | |
esp_err_t err = esp_camera_init(&config); | |
if (err != ESP_OK) { | |
Serial.printf("Camera init failed with error 0x%x", err); | |
return; | |
} | |
} | |
void connectWiFi() { | |
int retry_count = 0; | |
String ssid = preferences.getString("ssid", ""); | |
String password = preferences.getString("password", ""); | |
ssid.toCharArray(ssid_buffer, ssid.length() + 1); | |
password.toCharArray(password_buffer, password.length() + 1); | |
WiFi.mode(WIFI_STA); //WIFI_AP_STA | |
WiFi.begin(ssid_buffer, password_buffer); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
retry_count++; | |
if (retry_count > 10) { | |
break; | |
} | |
} | |
if (WiFi.status() == WL_CONNECTED) { | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
myIP = WiFi.localIP(); | |
} else { | |
WiFi.disconnect(); | |
WiFi.mode(WIFI_AP); | |
WiFi.softAP(APP_NAME); | |
delay(100); | |
myIP = WiFi.softAPIP(); | |
} | |
} | |
void handleRoot(){ | |
char html[2000]; | |
int sec = millis() / 1000; | |
int min = sec / 60; | |
int hr = min / 60; | |
char uptime[20]; | |
snprintf(uptime, 20, "%02d:%02d:%02d", hr, min % 60, sec % 60); | |
snprintf (html, 2000, | |
"<html>\ | |
<head>\ | |
<title>ESP32 Camera</title>\ | |
</head>\ | |
<body>\ | |
<h1>ESP32 Camera</h1>\ | |
<p>Uptime: %s</p>\ | |
<p><img src='/image?t=%d'/></p>\ | |
<a href='/setup'>setup</a>\ | |
</body>\ | |
</html>", | |
uptime, | |
millis() | |
); | |
server.send (200, "text/html", html); | |
} | |
void handleSetup() { | |
char html[800]; | |
int sec = millis() / 1000; | |
int min = sec / 60; | |
int hr = min / 60; | |
char uptime[20]; | |
char ipaddress[20]; | |
char wifi[20]; | |
snprintf(uptime, 20, "%02d:%02d:%02d", hr, min % 60, sec % 60); | |
snprintf(ipaddress, 15, "%d.%d.%d.%d", myIP[0], myIP[1], myIP[2], myIP[3]); | |
snprintf(html, 800, | |
"<html>\ | |
<head>\ | |
<title>ESP32 Camera</title>\ | |
</head>\ | |
<body>\ | |
<h1>ESP32 Camera</h1>\ | |
<p>Uptime: %s</p>\ | |
<p>IP address: %s</p>\ | |
<form action='/setwifi' method='POST'>\ | |
<input type='text' value='%s' name='ssid'>\ | |
<input type='password' value='%s' name='password'>\ | |
<input type='submit' value='update'>\ | |
</form>\ | |
<form action='/reconnectwifi' method='POST'>\ | |
<input type='submit' value='reconnect'>\ | |
</form>\ | |
<p><a href='/'>top</a></p>\ | |
</body>\ | |
</html>", | |
uptime, | |
ipaddress, | |
ssid_buffer, | |
password_buffer | |
); | |
server.send (200, "text/html", html); | |
} | |
void handleSetWifi(){ | |
if (server.hasArg("ssid") && server.hasArg("password")) { | |
preferences.putString("ssid", server.arg("ssid")); | |
preferences.putString("password", server.arg("password")); | |
server.arg("ssid").toCharArray(ssid_buffer, 20); | |
server.arg("password").toCharArray(password_buffer, 20); | |
} | |
server.sendHeader("Location", "/setup"); | |
server.sendHeader("Cache-Control", "no-cache"); | |
server.send(301); | |
} | |
void handleReconnectWifi(){ | |
server.sendHeader("Location", "/setup"); | |
server.sendHeader("Cache-Control", "no-cache"); | |
server.send(301); | |
WiFi.disconnect(); | |
delay(500); | |
connectWiFi(); | |
} | |
void handleNotFound() { | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for(uint8_t i = 0; i < server.args(); i++){ | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
} | |
void handleImage(){ | |
camera_fb_t * fb = NULL; | |
esp_err_t res = ESP_OK; | |
int64_t fr_start = esp_timer_get_time(); | |
fb = esp_camera_fb_get(); | |
if (!fb) { | |
Serial.println("Camera capture failed"); | |
server.send(500, "text/plain", "Capture failedd"); | |
return; | |
} | |
server.send_P(200, "image/jpeg", (const char *)fb->buf, fb->len); | |
esp_camera_fb_return(fb); | |
int64_t fr_end = esp_timer_get_time(); | |
Serial.printf("JPG: %uB %ums\n", (uint32_t)(fb->len), (uint32_t)((fr_end - fr_start)/1000)); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment