|
| 1 | +/* |
| 2 | +
|
| 3 | + This is a simple MJPEG streaming webserver implemented for AI-Thinker ESP32-CAM and |
| 4 | + ESP32-EYE modules. |
| 5 | + This is tested to work with VLC and Blynk video widget. |
| 6 | +
|
| 7 | + Inspired by and based on this Instructable: $9 RTSP Video Streamer Using the ESP32-CAM Board |
| 8 | + (https://www.instructables.com/id/9-RTSP-Video-Streamer-Using-the-ESP32-CAM-Board/) |
| 9 | +
|
| 10 | + Board: AI-Thinker ESP32-CAM |
| 11 | +
|
| 12 | +*/ |
| 13 | + |
| 14 | +#include "src/OV2640.h" |
| 15 | +#include <WiFi.h> |
| 16 | +#include <WebServer.h> |
| 17 | +#include <WiFiClient.h> |
| 18 | + |
| 19 | +// Select camera model |
| 20 | +//#define CAMERA_MODEL_WROVER_KIT |
| 21 | +//#define CAMERA_MODEL_ESP_EYE |
| 22 | +//#define CAMERA_MODEL_M5STACK_PSRAM |
| 23 | +//#define CAMERA_MODEL_M5STACK_WIDE |
| 24 | +#define CAMERA_MODEL_AI_THINKER |
| 25 | + |
| 26 | +#include "camera_pins.h" |
| 27 | + |
| 28 | +#define SSID1 "VIETTEL_TUNG HONDA" |
| 29 | +#define PWD1 "01685127737" |
| 30 | + |
| 31 | +OV2640 cam; |
| 32 | + |
| 33 | +WebServer server(80); |
| 34 | + |
| 35 | +const char HEADER[] = "HTTP/1.1 200 OK\r\n" \ |
| 36 | + "Access-Control-Allow-Origin: *\r\n" \ |
| 37 | + "Content-Type: multipart/x-mixed-replace; boundary=123456789000000000000987654321\r\n"; |
| 38 | +const char BOUNDARY[] = "\r\n--123456789000000000000987654321\r\n"; |
| 39 | +const char CTNTTYPE[] = "Content-Type: image/jpeg\r\nContent-Length: "; |
| 40 | +const int hdrLen = strlen(HEADER); |
| 41 | +const int bdrLen = strlen(BOUNDARY); |
| 42 | +const int cntLen = strlen(CTNTTYPE); |
| 43 | + |
| 44 | +void handle_jpg_stream(void) |
| 45 | +{ |
| 46 | + char buf[32]; |
| 47 | + int s; |
| 48 | + |
| 49 | + WiFiClient client = server.client(); |
| 50 | + |
| 51 | + client.write(HEADER, hdrLen); |
| 52 | + client.write(BOUNDARY, bdrLen); |
| 53 | + |
| 54 | + while (true) |
| 55 | + { |
| 56 | + if (!client.connected()) break; |
| 57 | + cam.run(); |
| 58 | + s = cam.getSize(); |
| 59 | + client.write(CTNTTYPE, cntLen); |
| 60 | + sprintf( buf, "%d\r\n\r\n", s ); |
| 61 | + client.write(buf, strlen(buf)); |
| 62 | + client.write((char *)cam.getfb(), s); |
| 63 | + client.write(BOUNDARY, bdrLen); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const char JHEADER[] = "HTTP/1.1 200 OK\r\n" \ |
| 68 | + "Content-disposition: inline; filename=capture.jpg\r\n" \ |
| 69 | + "Content-type: image/jpeg\r\n\r\n"; |
| 70 | +const int jhdLen = strlen(JHEADER); |
| 71 | + |
| 72 | +void handle_jpg(void) |
| 73 | +{ |
| 74 | + WiFiClient client = server.client(); |
| 75 | + |
| 76 | + cam.run(); |
| 77 | + if (!client.connected()) return; |
| 78 | + |
| 79 | + client.write(JHEADER, jhdLen); |
| 80 | + client.write((char *)cam.getfb(), cam.getSize()); |
| 81 | +} |
| 82 | + |
| 83 | +void handleNotFound() |
| 84 | +{ |
| 85 | + String message = "Server is running!\n\n"; |
| 86 | + message += "URI: "; |
| 87 | + message += server.uri(); |
| 88 | + message += "\nMethod: "; |
| 89 | + message += (server.method() == HTTP_GET) ? "GET" : "POST"; |
| 90 | + message += "\nArguments: "; |
| 91 | + message += server.args(); |
| 92 | + message += "\n"; |
| 93 | + server.send(200, "text / plain", message); |
| 94 | +} |
| 95 | + |
| 96 | +void setup() |
| 97 | +{ |
| 98 | + |
| 99 | + Serial.begin(115200); |
| 100 | + //while (!Serial); //wait for serial connection. |
| 101 | + |
| 102 | + camera_config_t config; |
| 103 | + config.ledc_channel = LEDC_CHANNEL_0; |
| 104 | + config.ledc_timer = LEDC_TIMER_0; |
| 105 | + config.pin_d0 = Y2_GPIO_NUM; |
| 106 | + config.pin_d1 = Y3_GPIO_NUM; |
| 107 | + config.pin_d2 = Y4_GPIO_NUM; |
| 108 | + config.pin_d3 = Y5_GPIO_NUM; |
| 109 | + config.pin_d4 = Y6_GPIO_NUM; |
| 110 | + config.pin_d5 = Y7_GPIO_NUM; |
| 111 | + config.pin_d6 = Y8_GPIO_NUM; |
| 112 | + config.pin_d7 = Y9_GPIO_NUM; |
| 113 | + config.pin_xclk = XCLK_GPIO_NUM; |
| 114 | + config.pin_pclk = PCLK_GPIO_NUM; |
| 115 | + config.pin_vsync = VSYNC_GPIO_NUM; |
| 116 | + config.pin_href = HREF_GPIO_NUM; |
| 117 | + config.pin_sscb_sda = SIOD_GPIO_NUM; |
| 118 | + config.pin_sscb_scl = SIOC_GPIO_NUM; |
| 119 | + config.pin_pwdn = PWDN_GPIO_NUM; |
| 120 | + config.pin_reset = RESET_GPIO_NUM; |
| 121 | + config.xclk_freq_hz = 20000000; |
| 122 | + config.pixel_format = PIXFORMAT_JPEG; |
| 123 | + |
| 124 | + // Frame parameters |
| 125 | + // config.frame_size = FRAMESIZE_UXGA; |
| 126 | + config.frame_size = FRAMESIZE_QVGA; |
| 127 | + config.jpeg_quality = 12; |
| 128 | + config.fb_count = 2; |
| 129 | + |
| 130 | +#if defined(CAMERA_MODEL_ESP_EYE) |
| 131 | + pinMode(13, INPUT_PULLUP); |
| 132 | + pinMode(14, INPUT_PULLUP); |
| 133 | +#endif |
| 134 | + |
| 135 | + cam.init(config); |
| 136 | + |
| 137 | + IPAddress ip; |
| 138 | + |
| 139 | + WiFi.mode(WIFI_STA); |
| 140 | + WiFi.begin(SSID1, PWD1); |
| 141 | + while (WiFi.status() != WL_CONNECTED) |
| 142 | + { |
| 143 | + delay(500); |
| 144 | + Serial.print(F(".")); |
| 145 | + } |
| 146 | + ip = WiFi.localIP(); |
| 147 | + Serial.println(F("WiFi connected")); |
| 148 | + Serial.println(""); |
| 149 | + Serial.println(ip); |
| 150 | + Serial.print("Stream Link: http://"); |
| 151 | + Serial.print(ip); |
| 152 | + Serial.println("/mjpeg/1"); |
| 153 | + server.on("/mjpeg/1", HTTP_GET, handle_jpg_stream); |
| 154 | + server.on("/jpg", HTTP_GET, handle_jpg); |
| 155 | + server.onNotFound(handleNotFound); |
| 156 | + server.begin(); |
| 157 | +} |
| 158 | + |
| 159 | +void loop() |
| 160 | +{ |
| 161 | + server.handleClient(); |
| 162 | +} |
0 commit comments