├── XClk.h ├── README.md ├── Log.h ├── DMABuffer.h ├── XClk.cpp ├── BMP.h ├── I2C.h ├── I2SCamera.h ├── OV7670.h ├── ESP32_WebSocket_Camera.ino ├── I2SCamera.cpp ├── OV7670.cpp ├── LICENSE └── canvas_htm.h /XClk.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | bool ClockEnable(int pin, int Hz); 4 | void ClockDisable(); 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mudassar-tamboli/ESP32-OV7670-WebSocket-Camera/HEAD/README.md -------------------------------------------------------------------------------- /Log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Arduino.h" 3 | 4 | /* 5 | #define DEBUG_PRINTLN(a) Serial.println(a) 6 | #define DEBUG_PRINT(a) Serial.print(a) 7 | #define DEBUG_PRINTLNF(a, f) Serial.println(a, f) 8 | #define DEBUG_PRINTF(a, f) Serial.print(a, f) 9 | */ 10 | #define DEBUG_PRINTLN(a) 11 | #define DEBUG_PRINT(a) 12 | #define DEBUG_PRINTLNF(a, f) 13 | #define DEBUG_PRINTF(a, f) 14 | 15 | -------------------------------------------------------------------------------- /DMABuffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class DMABuffer 4 | { 5 | public: 6 | lldesc_t descriptor; 7 | unsigned char* buffer; 8 | DMABuffer(int bytes) 9 | { 10 | buffer = (unsigned char *)malloc(bytes); 11 | descriptor.length = bytes; 12 | descriptor.size = descriptor.length; 13 | descriptor.owner = 1; 14 | descriptor.sosf = 1; 15 | descriptor.buf = (uint8_t*) buffer; 16 | descriptor.offset = 0; 17 | descriptor.empty = 0; 18 | descriptor.eof = 1; 19 | descriptor.qe.stqe_next = 0; 20 | } 21 | 22 | void next(DMABuffer *next) 23 | { 24 | descriptor.qe.stqe_next = &(next->descriptor); 25 | } 26 | 27 | int sampleCount() const 28 | { 29 | return descriptor.length / 4; 30 | } 31 | 32 | ~DMABuffer() 33 | { 34 | if(buffer) 35 | delete(buffer); 36 | } 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /XClk.cpp: -------------------------------------------------------------------------------- 1 | //parts of his code are taken from 2 | //https://github.com/igrr/esp32-cam-demo 3 | //by Ivan Grokhotkov 4 | //released under Apache License 2.0 5 | 6 | #include "XClk.h" 7 | #include "driver/ledc.h" 8 | 9 | bool ClockEnable(int pin, int Hz) 10 | { 11 | periph_module_enable(PERIPH_LEDC_MODULE); 12 | 13 | ledc_timer_config_t timer_conf; 14 | timer_conf.bit_num = (ledc_timer_bit_t)1; 15 | timer_conf.freq_hz = Hz; 16 | timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE; 17 | timer_conf.timer_num = LEDC_TIMER_0; 18 | esp_err_t err = ledc_timer_config(&timer_conf); 19 | if (err != ESP_OK) { 20 | return false; 21 | } 22 | 23 | ledc_channel_config_t ch_conf; 24 | ch_conf.channel = LEDC_CHANNEL_0; 25 | ch_conf.timer_sel = LEDC_TIMER_0; 26 | ch_conf.intr_type = LEDC_INTR_DISABLE; 27 | ch_conf.duty = 1; 28 | ch_conf.speed_mode = LEDC_HIGH_SPEED_MODE; 29 | ch_conf.gpio_num = pin; 30 | err = ledc_channel_config(&ch_conf); 31 | if (err != ESP_OK) { 32 | return false; 33 | } 34 | return true; 35 | } 36 | 37 | void ClockDisable() 38 | { 39 | periph_module_disable(PERIPH_LEDC_MODULE); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /BMP.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //assuming pixel lines have multiples of 4 bytes sizes 4 | class BMP 5 | { 6 | 7 | static void setChar(void *buffer, int pos, char ch) 8 | { 9 | *(char*)(buffer+pos) = ch; 10 | } 11 | 12 | static void setLong(void *buffer, int pos, long l) 13 | { 14 | *(long*)(buffer+pos) = l; 15 | } 16 | 17 | static void setShort(void *buffer, int pos, short s) 18 | { 19 | *(short*)(buffer+pos) = s; 20 | } 21 | 22 | public: 23 | static const int headerSize = 54 + 12; 24 | 25 | static void construct16BitHeader(void *buffer, long xres, long yres) 26 | { 27 | setChar(buffer, 0, 'B'); 28 | setChar(buffer, 1, 'M'); 29 | 30 | long bytesPerLine = xres * 2; 31 | setLong(buffer, 2, bytesPerLine * yres + 54 + 12); //filesize 32 | setLong(buffer, 6, 0); 33 | 34 | setLong(buffer, 10, 54 + 12); //offset 35 | 36 | setLong(buffer, 14, 40); //header size 37 | setLong(buffer, 18, xres); 38 | setLong(buffer, 22, yres); 39 | setShort(buffer, 26, 1); //planes 40 | setShort(buffer, 28, 16); //bits 41 | 42 | setLong(buffer, 30, 3); //compression 3 = bit fields 43 | setLong(buffer, 38, 0); //x pix per meter 44 | setLong(buffer, 42, 0); //y pix per meter 45 | 46 | setLong(buffer, 46, 0); //biClrUsed 47 | setLong(buffer, 50, 0); //biClrImportant 48 | 49 | setLong(buffer, 54, 0xF800); //R mask 50 | setLong(buffer, 58, 0x07E0); //G mask 51 | setLong(buffer, 62, 0x001F); //B mask 52 | } 53 | }; 54 | 55 | 56 | -------------------------------------------------------------------------------- /I2C.h: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | 3 | class I2C 4 | { 5 | void inline DELAY() 6 | { 7 | delayMicroseconds(1); 8 | } 9 | 10 | void inline SCLLOW() 11 | { 12 | pinMode(SCL, OUTPUT); 13 | digitalWrite(SCL, 0); 14 | } 15 | 16 | void inline SCLHIGH() 17 | { 18 | pinMode(SCL, INPUT_PULLUP); 19 | digitalWrite(SCL, 1); 20 | } 21 | 22 | void inline CLOCK() 23 | { 24 | DELAY(); 25 | SCLHIGH(); 26 | DELAY(); 27 | DELAY(); 28 | SCLLOW(); 29 | DELAY(); 30 | } 31 | 32 | void inline SDALOW() 33 | { 34 | pinMode(SDA, OUTPUT); 35 | digitalWrite(SDA, 0); 36 | } 37 | 38 | void inline SDAHIGH() 39 | { 40 | pinMode(SDA, OUTPUT); 41 | digitalWrite(SDA, 1); 42 | } 43 | 44 | void inline SDAPULLUP() 45 | { 46 | pinMode(SDA, INPUT_PULLUP); 47 | } 48 | 49 | void pushByte(unsigned char b) 50 | { 51 | for(char i = 0; i < 8; i++) 52 | { 53 | if(b & 0x80) 54 | SDAHIGH(); 55 | else 56 | SDALOW(); 57 | b <<= 1; 58 | CLOCK(); 59 | } 60 | } 61 | 62 | bool getAck() 63 | { 64 | SDAPULLUP(); 65 | DELAY(); 66 | SCLHIGH(); 67 | DELAY(); 68 | int r = digitalRead(SDA); 69 | SDALOW(); 70 | DELAY(); 71 | SCLLOW(); 72 | DELAY(); 73 | return r == 0; 74 | } 75 | 76 | void start() 77 | { 78 | SDAPULLUP(); 79 | DELAY(); 80 | SCLHIGH(); 81 | DELAY(); 82 | SDALOW(); 83 | DELAY(); 84 | SCLLOW(); 85 | DELAY(); 86 | } 87 | 88 | void end() 89 | { 90 | SCLHIGH(); 91 | DELAY(); 92 | SDAPULLUP(); 93 | DELAY(); 94 | } 95 | 96 | public: 97 | int SDA; 98 | int SCL; 99 | I2C(const int data, const int clock) 100 | { 101 | SDA = data; 102 | SCL = clock; 103 | pinMode(SDA, INPUT_PULLUP); 104 | pinMode(SCL, INPUT_PULLUP); 105 | digitalWrite(SDA, 0); 106 | digitalWrite(SCL, 0); 107 | } 108 | 109 | bool writeRegister(unsigned char addr, unsigned char reg, unsigned char data) 110 | { 111 | start(); 112 | pushByte(addr); 113 | 114 | if(!getAck()) 115 | { 116 | end(); 117 | return false; 118 | } 119 | 120 | pushByte(reg); 121 | if(!getAck()) 122 | { 123 | end(); 124 | return false; 125 | } 126 | 127 | pushByte(data); 128 | if(!getAck()) 129 | { 130 | end(); 131 | return false; 132 | } 133 | 134 | end(); 135 | return true; 136 | } 137 | }; 138 | -------------------------------------------------------------------------------- /I2SCamera.h: -------------------------------------------------------------------------------- 1 | //parts of his code are taken from 2 | //https://github.com/igrr/esp32-cam-demo 3 | //by Ivan Grokhotkov 4 | //released under Apache License 2.0 5 | 6 | #pragma once 7 | 8 | #include "soc/soc.h" 9 | #include "soc/gpio_sig_map.h" 10 | #include "soc/i2s_reg.h" 11 | #include "soc/i2s_struct.h" 12 | #include "soc/io_mux_reg.h" 13 | #include "driver/gpio.h" 14 | #include "driver/periph_ctrl.h" 15 | #include "rom/lldesc.h" 16 | #include "XClk.h" 17 | #include "DMABuffer.h" 18 | 19 | class I2SCamera 20 | { 21 | public: 22 | static gpio_num_t vSyncPin; 23 | static int blocksReceived; 24 | static int framesReceived; 25 | static int xres; 26 | static int yres; 27 | static intr_handle_t i2sInterruptHandle; 28 | static intr_handle_t vSyncInterruptHandle; 29 | static int dmaBufferCount; 30 | static int dmaBufferActive; 31 | static DMABuffer **dmaBuffer; 32 | static unsigned char* frame; 33 | static int framePointer; 34 | static int frameBytes; 35 | static volatile bool stopSignal; 36 | 37 | static int startBlock; 38 | static int endBlock; 39 | static int blockSlice; 40 | 41 | typedef enum { 42 | /* camera sends byte sequence: s1, s2, s3, s4, ... 43 | * fifo receives: 00 s1 00 s2, 00 s2 00 s3, 00 s3 00 s4, ... 44 | */ 45 | SM_0A0B_0B0C = 0, 46 | /* camera sends byte sequence: s1, s2, s3, s4, ... 47 | * fifo receives: 00 s1 00 s2, 00 s3 00 s4, ... 48 | */ 49 | SM_0A0B_0C0D = 1, 50 | /* camera sends byte sequence: s1, s2, s3, s4, ... 51 | * fifo receives: 00 s1 00 00, 00 s2 00 00, 00 s3 00 00, ... 52 | */ 53 | SM_0A00_0B00 = 3, 54 | } i2s_sampling_mode_t; 55 | 56 | 57 | static inline void i2sConfReset() 58 | { 59 | const uint32_t lc_conf_reset_flags = I2S_IN_RST_M | I2S_AHBM_RST_M | I2S_AHBM_FIFO_RST_M; 60 | I2S0.lc_conf.val |= lc_conf_reset_flags; 61 | I2S0.lc_conf.val &= ~lc_conf_reset_flags; 62 | 63 | const uint32_t conf_reset_flags = I2S_RX_RESET_M | I2S_RX_FIFO_RESET_M | I2S_TX_RESET_M | I2S_TX_FIFO_RESET_M; 64 | I2S0.conf.val |= conf_reset_flags; 65 | I2S0.conf.val &= ~conf_reset_flags; 66 | while (I2S0.state.rx_fifo_reset_back); 67 | } 68 | 69 | void start() 70 | { 71 | i2sRun(); 72 | } 73 | 74 | void stop() 75 | { 76 | stopSignal = true; 77 | while(stopSignal); 78 | } 79 | 80 | void oneFrame() 81 | { 82 | start(); 83 | stop(); 84 | } 85 | 86 | static void i2sStop(); 87 | static void i2sRun(); 88 | 89 | static void dmaBufferInit(int bytes); 90 | static void dmaBufferDeinit(); 91 | 92 | static bool initVSync(int pin); 93 | static void deinitVSync(); 94 | 95 | static void IRAM_ATTR i2sInterrupt(void* arg); 96 | static void IRAM_ATTR vSyncInterrupt(void* arg); 97 | 98 | static bool i2sInit(const int VSYNC, const int HREF, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7); 99 | 100 | static bool init(const int XRES, const int YRES, const int VSYNC, const int HREF, const int XCLK, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7); 101 | }; 102 | -------------------------------------------------------------------------------- /OV7670.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "I2SCamera.h" 3 | #include "I2C.h" 4 | 5 | class OV7670: public I2SCamera 6 | { 7 | public: 8 | enum Mode 9 | { 10 | QQQVGA_RGB565, 11 | QQVGA_RGB565, 12 | QVGA_RGB565, 13 | VGA_RGB565, 14 | }; 15 | int xres, yres; 16 | 17 | protected: 18 | static const int ADDR = 0x42; 19 | 20 | Mode mode; 21 | I2C i2c; 22 | 23 | void testImage(); 24 | void saturation(int s); 25 | void frameControl(int hStart, int hStop, int vStart, int vStop); 26 | void QVGA(); 27 | void QVGARGB565(); 28 | void VGA(); 29 | void VGARGB565(); 30 | 31 | void QQVGA(); 32 | void QQVGARGB565(); 33 | void QQQVGA(); 34 | void QQQVGARGB565(); 35 | void inline writeRegister(unsigned char reg, unsigned char data) 36 | { 37 | i2c.writeRegister(ADDR, reg, data); 38 | } 39 | 40 | public: 41 | OV7670(OV7670::Mode m, const int SIOD, const int SIOC, const int VSYNC, const int HREF, const int XCLK, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7); 42 | 43 | 44 | //camera registers 45 | static const int REG_GAIN = 0x00; 46 | static const int REG_BLUE = 0x01; 47 | static const int REG_RED = 0x02; 48 | static const int REG_COM1 = 0x04; 49 | static const int REG_VREF = 0x03; 50 | static const int REG_COM4 = 0x0d; 51 | static const int REG_COM5 = 0x0e; 52 | static const int REG_COM6 = 0x0f; 53 | static const int REG_AECH = 0x10; 54 | static const int REG_CLKRC = 0x11; 55 | static const int REG_COM7 = 0x12; 56 | static const int COM7_RGB = 0x04; 57 | static const int REG_COM8 = 0x13; 58 | static const int COM8_FASTAEC = 0x80; // Enable fast AGC/AEC 59 | static const int COM8_AECSTEP = 0x40; // Unlimited AEC step size 60 | static const int COM8_BFILT = 0x20; // Band filter enable 61 | static const int COM8_AGC = 0x04; // Auto gain enable 62 | static const int COM8_AWB = 0x02; // White balance enable 63 | static const int COM8_AEC = 0x0; 64 | static const int REG_COM9 = 0x14; 65 | static const int REG_COM10 = 0x15; 66 | static const int REG_COM14 = 0x3E; 67 | static const int REG_COM11 = 0x3B; 68 | static const int COM11_NIGHT = 0x80; 69 | static const int COM11_NMFR = 0x60; 70 | static const int COM11_HZAUTO = 0x10; 71 | static const int COM11_50HZ = 0x08; 72 | static const int COM11_EXP = 0x0; 73 | static const int REG_TSLB = 0x3A; 74 | static const int REG_RGB444 = 0x8C; 75 | static const int REG_COM15 = 0x40; 76 | static const int COM15_RGB565 = 0x10; 77 | static const int COM15_R00FF = 0xc0; 78 | static const int REG_HSTART = 0x17; 79 | static const int REG_HSTOP = 0x18; 80 | static const int REG_HREF = 0x32; 81 | static const int REG_VSTART = 0x19; 82 | static const int REG_VSTOP = 0x1A; 83 | static const int REG_COM3 = 0x0C; 84 | static const int REG_MVFP = 0x1E; 85 | static const int REG_COM13 = 0x3d; 86 | static const int COM13_UVSAT = 0x40; 87 | static const int REG_SCALING_XSC = 0x70; 88 | static const int REG_SCALING_YSC = 0x71; 89 | static const int REG_SCALING_DCWCTR = 0x72; 90 | static const int REG_SCALING_PCLK_DIV = 0x73; 91 | static const int REG_SCALING_PCLK_DELAY = 0xa2; 92 | static const int REG_BD50MAX = 0xa5; 93 | static const int REG_BD60MAX = 0xab; 94 | static const int REG_AEW = 0x24; 95 | static const int REG_AEB = 0x25; 96 | static const int REG_VPT = 0x26; 97 | static const int REG_HAECC1 = 0x9f; 98 | static const int REG_HAECC2 = 0xa0; 99 | static const int REG_HAECC3 = 0xa6; 100 | static const int REG_HAECC4 = 0xa7; 101 | static const int REG_HAECC5 = 0xa8; 102 | static const int REG_HAECC6 = 0xa9; 103 | static const int REG_HAECC7 = 0xaa; 104 | static const int REG_COM12 = 0x3c; 105 | static const int REG_GFIX = 0x69; 106 | static const int REG_COM16 = 0x41; 107 | static const int COM16_AWBGAIN = 0x08; 108 | static const int REG_EDGE = 0x3f; 109 | static const int REG_REG76 = 0x76; 110 | static const int ADCCTR0 = 0x20; 111 | 112 | }; 113 | 114 | -------------------------------------------------------------------------------- /ESP32_WebSocket_Camera.ino: -------------------------------------------------------------------------------- 1 | 2 | //Author : Mudassar Tamboli 3 | 4 | #include "OV7670.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "canvas_htm.h" 13 | 14 | const char *ap_ssid = "Esp32AP"; 15 | const char *ap_password = "thereisnospoon"; 16 | 17 | const char *ssid_AP_1 = "XXXXXXXXXX"; 18 | const char *pwd_AP_1 = "xxxxxxxxxx"; 19 | 20 | const char *ssid_AP_2 = "XXXXXXXXX"; 21 | const char *pwd_AP_2 = "xxxxxxxxx"; 22 | 23 | const char *ssid_AP_3 = "XXXXXXXXX"; 24 | const char *pwd_AP_3 = "xxxxxxxxx"; 25 | 26 | const int SIOD = 21; //SDA 27 | const int SIOC = 22; //SCL 28 | 29 | const int VSYNC = 34; 30 | const int HREF = 35; 31 | 32 | const int XCLK = 32; 33 | const int PCLK = 33; 34 | 35 | const int D0 = 27; 36 | const int D1 = 17; 37 | const int D2 = 16; 38 | const int D3 = 15; 39 | const int D4 = 14; 40 | const int D5 = 13; 41 | const int D6 = 12; 42 | const int D7 = 4; 43 | 44 | const int TFT_DC = 2; 45 | const int TFT_CS = 5; 46 | //DIN <- MOSI 23 47 | //CLK <- SCK 18 48 | 49 | #define ssid1 "YOUR_WIFI_SSID" 50 | #define password1 "YOUR_PASSWORD" 51 | //#define ssid2 "" 52 | //#define password2 "" 53 | 54 | OV7670 *camera; 55 | 56 | WiFiMulti wifiMulti; 57 | WiFiServer server(80); 58 | 59 | unsigned char pix = 0; 60 | 61 | //unsigned char bmpHeader[BMP::headerSize]; 62 | 63 | unsigned char start_flag = 0xAA; 64 | unsigned char end_flag = 0xFF; 65 | unsigned char ip_flag = 0x11; 66 | 67 | WebSocketsServer webSocket(81); // create a websocket server on port 81 68 | 69 | void startWebSocket() { // Start a WebSocket server 70 | webSocket.begin(); // start the websocket server 71 | webSocket.onEvent(webSocketEvent); // if there's an incomming websocket message, go to function 'webSocketEvent' 72 | Serial.println("WebSocket server started."); 73 | } 74 | 75 | void startWebServer() 76 | { 77 | server.begin(); 78 | Serial.println("Http web server started."); 79 | } 80 | void serve() { 81 | WiFiClient client = server.available(); 82 | if (client) 83 | { 84 | //Serial.println("New Client."); 85 | String currentLine = ""; 86 | while (client.connected()) 87 | { 88 | if (client.available()) 89 | { 90 | char c = client.read(); 91 | //Serial.write(c); 92 | if (c == '\n') 93 | { 94 | if (currentLine.length() == 0) 95 | { 96 | client.println("HTTP/1.1 200 OK"); 97 | client.println("Content-type:text/html"); 98 | client.println(); 99 | client.print(canvas_htm); 100 | client.println(); 101 | break; 102 | } 103 | else 104 | { 105 | currentLine = ""; 106 | } 107 | } 108 | else if (c != '\r') 109 | { 110 | currentLine += c; 111 | } 112 | 113 | } 114 | } 115 | // close the connection: 116 | client.stop(); 117 | 118 | } 119 | } 120 | 121 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t payloadlength) { // When a WebSocket message is received 122 | 123 | int blk_count = 0; 124 | 125 | char canvas_VGA[] = "canvas-VGA"; 126 | char canvas_Q_VGA[] = "canvas-Q-VGA"; 127 | char canvas_QQ_VGA[] = "canvas-QQ-VGA"; 128 | char canvas_QQQ_VGA[] = "canvas-QQQ-VGA"; 129 | char ipaddr[26] ; 130 | IPAddress localip; 131 | 132 | switch (type) { 133 | case WStype_DISCONNECTED: // if the websocket is disconnected 134 | Serial.printf("[%u] Disconnected!\n", num); 135 | break; 136 | case WStype_CONNECTED: { // if a new websocket connection is established 137 | IPAddress ip = webSocket.remoteIP(num); 138 | Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); 139 | webSocket.sendBIN(0, &ip_flag, 1); 140 | localip = WiFi.localIP(); 141 | sprintf(ipaddr, "%d.%d.%d.%d", localip[0], localip[1], localip[2], localip[3]); 142 | webSocket.sendTXT(0, (const char *)ipaddr); 143 | 144 | } 145 | break; 146 | case WStype_TEXT: // if new text data is received 147 | if (payloadlength == sizeof(canvas_QQQ_VGA)-1) { 148 | if (memcmp(canvas_QQQ_VGA, payload, payloadlength) == 0) { 149 | Serial.printf("canvas_QQQ_VGA"); 150 | webSocket.sendBIN(0, &end_flag, 1); 151 | camera = new OV7670(OV7670::Mode::QQQVGA_RGB565, SIOD, SIOC, VSYNC, HREF, XCLK, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 152 | } 153 | } else if (payloadlength == sizeof(canvas_QQ_VGA)-1) { 154 | if (memcmp(canvas_QQ_VGA, payload, payloadlength) == 0) { 155 | Serial.printf("canvas_QQ_VGA"); 156 | webSocket.sendBIN(0, &end_flag, 1); 157 | camera = new OV7670(OV7670::Mode::QQVGA_RGB565, SIOD, SIOC, VSYNC, HREF, XCLK, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 158 | } 159 | } else if (payloadlength == sizeof(canvas_Q_VGA)-1) { 160 | if (memcmp(canvas_Q_VGA, payload, payloadlength) == 0) { 161 | Serial.printf("canvas_Q_VGA"); 162 | webSocket.sendBIN(0, &end_flag, 1); 163 | camera = new OV7670(OV7670::Mode::QVGA_RGB565, SIOD, SIOC, VSYNC, HREF, XCLK, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 164 | } 165 | } else if (payloadlength == sizeof(canvas_VGA)-1) { 166 | if (memcmp(canvas_VGA, payload, payloadlength) == 0) { 167 | Serial.printf("canvas_VGA"); 168 | webSocket.sendBIN(0, &end_flag, 1); 169 | camera = new OV7670(OV7670::Mode::VGA_RGB565, SIOD, SIOC, VSYNC, HREF, XCLK, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 170 | } 171 | } 172 | 173 | 174 | blk_count = camera->yres/I2SCamera::blockSlice;//30, 60, 120 175 | for (int i=0; istartBlock = 1; 179 | camera->endBlock = I2SCamera::blockSlice; 180 | webSocket.sendBIN(0, &start_flag, 1); 181 | } 182 | 183 | if (i == blk_count-1) { 184 | webSocket.sendBIN(0, &end_flag, 1); 185 | } 186 | 187 | camera->oneFrame(); 188 | webSocket.sendBIN(0, camera->frame, camera->xres * I2SCamera::blockSlice * 2); 189 | camera->startBlock += I2SCamera::blockSlice; 190 | camera->endBlock += I2SCamera::blockSlice; 191 | } 192 | 193 | break; 194 | case WStype_ERROR: // if new text data is received 195 | Serial.printf("Error \n"); 196 | default: 197 | Serial.printf("WStype %x not handled \n", type); 198 | 199 | } 200 | } 201 | void initWifiStation() { 202 | 203 | WiFi.mode(WIFI_AP_STA); 204 | WiFi.begin(ssid, password); 205 | Serial.print("\nConnecting to WiFi"); 206 | while (WiFi.status() != WL_CONNECTED) { 207 | delay(5000); 208 | Serial.print("."); 209 | } 210 | Serial.println(String("\nConnected to the WiFi network (") + ssid + ")" ); 211 | 212 | Serial.print("\nStation IP address: "); 213 | Serial.println(WiFi.localIP()); 214 | 215 | } 216 | 217 | void initWifiMulti() 218 | { 219 | 220 | wifiMulti.addAP(ssid_AP_1, pwd_AP_1); 221 | wifiMulti.addAP(ssid_AP_2, pwd_AP_2); 222 | wifiMulti.addAP(ssid_AP_3, pwd_AP_3); 223 | 224 | Serial.println("Connecting Wifi..."); 225 | while(wifiMulti.run() != WL_CONNECTED) { 226 | delay(5000); 227 | Serial.print("."); 228 | } 229 | 230 | Serial.print("\n"); 231 | Serial.print("WiFi connected : "); 232 | Serial.print("IP address : "); 233 | Serial.println(WiFi.localIP()); 234 | } 235 | 236 | void initWifiAP() { 237 | 238 | WiFi.mode(WIFI_AP_STA); 239 | WiFi.softAP(ap_ssid, ap_password); 240 | IPAddress myIP = WiFi.softAPIP(); 241 | Serial.print("AP IP address: "); 242 | Serial.println(myIP); 243 | } 244 | 245 | 246 | void setup() { 247 | Serial.begin(115200); 248 | initWifiMulti(); 249 | initWifiAP(); 250 | startWebSocket(); 251 | startWebServer(); 252 | } 253 | 254 | void loop() 255 | { 256 | webSocket.loop(); 257 | serve(); 258 | } 259 | 260 | 261 | 262 | -------------------------------------------------------------------------------- /I2SCamera.cpp: -------------------------------------------------------------------------------- 1 | //parts of his code are taken from 2 | //https://github.com/igrr/esp32-cam-demo 3 | //by Ivan Grokhotkov 4 | //released under Apache License 2.0 5 | 6 | #include "I2SCamera.h" 7 | #include "Log.h" 8 | #include 9 | #include 10 | #include 11 | 12 | int I2SCamera::blocksReceived = 0; 13 | int I2SCamera::framesReceived = 0; 14 | int I2SCamera::xres = 160; 15 | int I2SCamera::yres = 120; 16 | int I2SCamera::startBlock = 0; 17 | int I2SCamera::endBlock = 0; 18 | int I2SCamera::blockSlice = 0; 19 | 20 | 21 | gpio_num_t I2SCamera::vSyncPin = (gpio_num_t)0; 22 | intr_handle_t I2SCamera::i2sInterruptHandle = 0; 23 | intr_handle_t I2SCamera::vSyncInterruptHandle = 0; 24 | int I2SCamera::dmaBufferCount = 0; 25 | int I2SCamera::dmaBufferActive = 0; 26 | DMABuffer **I2SCamera::dmaBuffer = 0; 27 | unsigned char* I2SCamera::frame = 0; 28 | int I2SCamera::framePointer = 0; 29 | int I2SCamera::frameBytes = 0; 30 | volatile bool I2SCamera::stopSignal = false; 31 | 32 | void IRAM_ATTR I2SCamera::i2sInterrupt(void* arg) 33 | { 34 | I2S0.int_clr.val = I2S0.int_raw.val; 35 | blocksReceived++; 36 | unsigned char* buf = dmaBuffer[dmaBufferActive]->buffer; 37 | dmaBufferActive = (dmaBufferActive + 1) % dmaBufferCount; 38 | 39 | // 1-30, 31-60, 61-90 ..... 40 | 41 | if (blocksReceived >= startBlock && blocksReceived <= endBlock) { 42 | if(framePointer < frameBytes) { 43 | for(int i = 0; i < xres * 4; i += 4) 44 | { 45 | frame[framePointer++] = buf[i + 2]; 46 | frame[framePointer++] = buf[i]; 47 | } 48 | } 49 | } 50 | 51 | if (blocksReceived == yres) // default yres 52 | { 53 | //Serial.printf("frameBytes %d , framePointer %d, blocksReceived %d\n", frameBytes, framePointer, blocksReceived); 54 | framePointer = 0; 55 | blocksReceived = 0; 56 | framesReceived++; 57 | if(stopSignal) 58 | { 59 | i2sStop(); 60 | stopSignal = false; 61 | } 62 | } 63 | 64 | // i2sStop(); 65 | } 66 | 67 | void IRAM_ATTR I2SCamera::vSyncInterrupt(void* arg) 68 | { 69 | GPIO.status1_w1tc.val = GPIO.status1.val; 70 | GPIO.status_w1tc = GPIO.status; 71 | if(gpio_get_level(vSyncPin)) 72 | { 73 | //frame done 74 | } 75 | } 76 | 77 | void I2SCamera::i2sStop() 78 | { 79 | esp_intr_disable(i2sInterruptHandle); 80 | esp_intr_disable(vSyncInterruptHandle); 81 | i2sConfReset(); 82 | I2S0.conf.rx_start = 0; 83 | } 84 | 85 | void I2SCamera::i2sRun() 86 | { 87 | DEBUG_PRINTLN("I2S Run"); 88 | while (gpio_get_level(vSyncPin) == 0); 89 | while (gpio_get_level(vSyncPin) != 0); 90 | 91 | esp_intr_disable(i2sInterruptHandle); 92 | i2sConfReset(); 93 | blocksReceived = 0; 94 | dmaBufferActive = 0; 95 | framePointer = 0; 96 | DEBUG_PRINT("Sample count "); 97 | DEBUG_PRINTLN(dmaBuffer[0]->sampleCount()); 98 | I2S0.rx_eof_num = dmaBuffer[0]->sampleCount(); 99 | I2S0.in_link.addr = (uint32_t)&(dmaBuffer[0]->descriptor); 100 | I2S0.in_link.start = 1; 101 | I2S0.int_clr.val = I2S0.int_raw.val; 102 | I2S0.int_ena.val = 0; 103 | I2S0.int_ena.in_done = 1; 104 | esp_intr_enable(i2sInterruptHandle); 105 | esp_intr_enable(vSyncInterruptHandle); 106 | I2S0.conf.rx_start = 1; 107 | } 108 | 109 | bool I2SCamera::initVSync(int pin) 110 | { 111 | DEBUG_PRINT("Initializing VSYNC... "); 112 | vSyncPin = (gpio_num_t)pin; 113 | gpio_set_intr_type(vSyncPin, GPIO_INTR_POSEDGE); 114 | gpio_intr_enable(vSyncPin); 115 | if(gpio_isr_register(&vSyncInterrupt, (void*)"vSyncInterrupt", ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_IRAM, &vSyncInterruptHandle) != ESP_OK) 116 | { 117 | DEBUG_PRINTLN("failed!"); 118 | return false; 119 | } 120 | DEBUG_PRINTLN("done."); 121 | return true; 122 | } 123 | 124 | void I2SCamera::deinitVSync() 125 | { 126 | esp_intr_disable(vSyncInterruptHandle); 127 | } 128 | 129 | bool I2SCamera::init(const int XRES, const int YRES, const int VSYNC, const int HREF, const int XCLK, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7) 130 | { 131 | xres = XRES; 132 | yres = YRES; 133 | frameBytes = XRES * blockSlice * 2; 134 | 135 | if (frame) { 136 | free (frame); 137 | Serial.printf("frame memory freed\n"); 138 | } 139 | 140 | frame = (unsigned char*)malloc(frameBytes); 141 | if(!frame) 142 | { 143 | DEBUG_PRINTLN("Not enough memory for frame buffer!"); 144 | return false; 145 | } 146 | i2sInit(VSYNC, HREF, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 147 | dmaBufferInit(xres * 2 * 2); //two bytes per dword packing, two bytes per pixel 148 | initVSync(VSYNC); 149 | return true; 150 | } 151 | 152 | bool I2SCamera::i2sInit(const int VSYNC, const int HREF, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7) 153 | { 154 | int pins[] = {VSYNC, HREF, PCLK, D0, D1, D2, D3, D4, D5, D6, D7}; 155 | gpio_config_t conf = { 156 | .pin_bit_mask = 0, 157 | .mode = GPIO_MODE_INPUT, 158 | .pull_up_en = GPIO_PULLUP_DISABLE, 159 | .pull_down_en = GPIO_PULLDOWN_DISABLE, 160 | .intr_type = GPIO_INTR_DISABLE 161 | }; 162 | for (int i = 0; i < sizeof(pins) / sizeof(gpio_num_t); ++i) { 163 | conf.pin_bit_mask = 1LL << pins[i]; 164 | gpio_config(&conf); 165 | } 166 | 167 | // Route input GPIOs to I2S peripheral using GPIO matrix, last parameter is invert 168 | gpio_matrix_in(D0, I2S0I_DATA_IN0_IDX, false); 169 | gpio_matrix_in(D1, I2S0I_DATA_IN1_IDX, false); 170 | gpio_matrix_in(D2, I2S0I_DATA_IN2_IDX, false); 171 | gpio_matrix_in(D3, I2S0I_DATA_IN3_IDX, false); 172 | gpio_matrix_in(D4, I2S0I_DATA_IN4_IDX, false); 173 | gpio_matrix_in(D5, I2S0I_DATA_IN5_IDX, false); 174 | gpio_matrix_in(D6, I2S0I_DATA_IN6_IDX, false); 175 | gpio_matrix_in(D7, I2S0I_DATA_IN7_IDX, false); 176 | gpio_matrix_in(0x30, I2S0I_DATA_IN8_IDX, false); 177 | gpio_matrix_in(0x30, I2S0I_DATA_IN9_IDX, false); 178 | gpio_matrix_in(0x30, I2S0I_DATA_IN10_IDX, false); 179 | gpio_matrix_in(0x30, I2S0I_DATA_IN11_IDX, false); 180 | gpio_matrix_in(0x30, I2S0I_DATA_IN12_IDX, false); 181 | gpio_matrix_in(0x30, I2S0I_DATA_IN13_IDX, false); 182 | gpio_matrix_in(0x30, I2S0I_DATA_IN14_IDX, false); 183 | gpio_matrix_in(0x30, I2S0I_DATA_IN15_IDX, false); 184 | 185 | gpio_matrix_in(VSYNC, I2S0I_V_SYNC_IDX, true); 186 | gpio_matrix_in(0x38, I2S0I_H_SYNC_IDX, false); //0x30 sends 0, 0x38 sends 1 187 | gpio_matrix_in(HREF, I2S0I_H_ENABLE_IDX, false); 188 | gpio_matrix_in(PCLK, I2S0I_WS_IN_IDX, false); 189 | 190 | // Enable and configure I2S peripheral 191 | periph_module_enable(PERIPH_I2S0_MODULE); 192 | 193 | // Toggle some reset bits in LC_CONF register 194 | // Toggle some reset bits in CONF register 195 | i2sConfReset(); 196 | // Enable slave mode (sampling clock is external) 197 | I2S0.conf.rx_slave_mod = 1; 198 | // Enable parallel mode 199 | I2S0.conf2.lcd_en = 1; 200 | // Use HSYNC/VSYNC/HREF to control sampling 201 | I2S0.conf2.camera_en = 1; 202 | // Configure clock divider 203 | I2S0.clkm_conf.clkm_div_a = 1; 204 | I2S0.clkm_conf.clkm_div_b = 0; 205 | I2S0.clkm_conf.clkm_div_num = 2; 206 | // FIFO will sink data to DMA 207 | I2S0.fifo_conf.dscr_en = 1; 208 | // FIFO configuration 209 | //two bytes per dword packing 210 | I2S0.fifo_conf.rx_fifo_mod = SM_0A0B_0C0D; //pack two bytes in one dword see :https://github.com/igrr/esp32-cam-demo/issues/29 211 | I2S0.fifo_conf.rx_fifo_mod_force_en = 1; 212 | I2S0.conf_chan.rx_chan_mod = 1; 213 | // Clear flags which are used in I2S serial mode 214 | I2S0.sample_rate_conf.rx_bits_mod = 0; 215 | I2S0.conf.rx_right_first = 0; 216 | I2S0.conf.rx_msb_right = 0; 217 | I2S0.conf.rx_msb_shift = 0; 218 | I2S0.conf.rx_mono = 0; 219 | I2S0.conf.rx_short_sync = 0; 220 | I2S0.timing.val = 0; 221 | 222 | // Allocate I2S interrupt, keep it disabled 223 | esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_LEVEL1 | ESP_INTR_FLAG_IRAM, &i2sInterrupt, NULL, &i2sInterruptHandle); 224 | return true; 225 | } 226 | 227 | void I2SCamera::dmaBufferInit(int bytes) 228 | { 229 | dmaBufferDeinit(); 230 | 231 | dmaBufferCount = 2; 232 | dmaBuffer = (DMABuffer**) malloc(sizeof(DMABuffer*) * dmaBufferCount); 233 | for(int i = 0; i < dmaBufferCount; i++) 234 | { 235 | dmaBuffer[i] = new DMABuffer(bytes); 236 | if(i) 237 | dmaBuffer[i-1]->next(dmaBuffer[i]); 238 | } 239 | dmaBuffer[dmaBufferCount - 1]->next(dmaBuffer[0]); 240 | } 241 | 242 | void I2SCamera::dmaBufferDeinit() 243 | { 244 | 245 | if (!dmaBuffer) return; 246 | for(int i = 0; i < dmaBufferCount; i++) 247 | delete(dmaBuffer[i]); 248 | delete(dmaBuffer); 249 | dmaBuffer = 0; 250 | dmaBufferCount = 0; 251 | 252 | Serial.printf("dma buffer memory freed\n"); 253 | } 254 | -------------------------------------------------------------------------------- /OV7670.cpp: -------------------------------------------------------------------------------- 1 | #include "OV7670.h" 2 | #include "XClk.h" 3 | #include "Log.h" 4 | 5 | OV7670::OV7670(Mode m, const int SIOD, const int SIOC, const int VSYNC, const int HREF, const int XCLK, const int PCLK, const int D0, const int D1, const int D2, const int D3, const int D4, const int D5, const int D6, const int D7) 6 | :i2c(SIOD, SIOC) 7 | { 8 | ClockEnable(XCLK, 20000000); //base is 80MHz 9 | 10 | DEBUG_PRINT("Waiting for VSYNC..."); 11 | pinMode(VSYNC, INPUT); 12 | while(!digitalRead(VSYNC)); 13 | while(digitalRead(VSYNC)); 14 | DEBUG_PRINTLN(" done"); 15 | 16 | mode = m; 17 | switch(mode) 18 | { 19 | 20 | case VGA_RGB565: 21 | blockSlice = 60; 22 | xres = 640; 23 | yres = 480; 24 | VGARGB565(); 25 | 26 | break; 27 | 28 | case QVGA_RGB565: 29 | blockSlice = 120; 30 | xres = 320; 31 | yres = 240; 32 | QVGARGB565(); 33 | break; 34 | 35 | case QQVGA_RGB565: 36 | blockSlice = 120; 37 | xres = 160; 38 | yres = 120; 39 | QQVGARGB565(); 40 | break; 41 | 42 | case QQQVGA_RGB565: 43 | blockSlice = 60; 44 | xres = 80; 45 | yres = 60; 46 | QQQVGARGB565(); 47 | break; 48 | 49 | default: 50 | xres = 0; 51 | yres = 0; 52 | } 53 | //testImage(); 54 | I2SCamera::init(xres, yres, VSYNC, HREF, XCLK, PCLK, D0, D1, D2, D3, D4, D5, D6, D7); 55 | } 56 | 57 | void OV7670::testImage() 58 | { 59 | i2c.writeRegister(ADDR, 0x71, 0x35 | 0x80); 60 | } 61 | 62 | void OV7670::saturation(int s) //-2 to 2 63 | { 64 | //color matrix values 65 | i2c.writeRegister(ADDR, 0x4f, 0x80 + 0x20 * s); 66 | i2c.writeRegister(ADDR, 0x50, 0x80 + 0x20 * s); 67 | i2c.writeRegister(ADDR, 0x51, 0x00); 68 | i2c.writeRegister(ADDR, 0x52, 0x22 + (0x11 * s) / 2); 69 | i2c.writeRegister(ADDR, 0x53, 0x5e + (0x2f * s) / 2); 70 | i2c.writeRegister(ADDR, 0x54, 0x80 + 0x20 * s); 71 | i2c.writeRegister(ADDR, 0x58, 0x9e); //matrix signs 72 | } 73 | 74 | void OV7670::frameControl(int hStart, int hStop, int vStart, int vStop) 75 | { 76 | i2c.writeRegister(ADDR, REG_HSTART, hStart >> 3); 77 | i2c.writeRegister(ADDR, REG_HSTOP, hStop >> 3); 78 | i2c.writeRegister(ADDR, REG_HREF, ((hStop & 0b111) << 3) | (hStart & 0b111)); 79 | 80 | i2c.writeRegister(ADDR, REG_VSTART, vStart >> 2); 81 | i2c.writeRegister(ADDR, REG_VSTOP, vStop >> 2); 82 | i2c.writeRegister(ADDR, REG_VREF, ((vStop & 0b11) << 2) | (vStart & 0b11)); 83 | } 84 | 85 | /////////////////////////////////////// 86 | 87 | 88 | void OV7670::QVGA() 89 | { 90 | i2c.writeRegister(ADDR, REG_COM3, 0x04); //DCW enable -- done 91 | i2c.writeRegister(ADDR, REG_COM14, 0x19); //pixel clock divided by 4, manual scaling enable, DCW and PCLK controlled by register -- done 92 | i2c.writeRegister(ADDR, REG_SCALING_XSC, 0x3a); // -- done 93 | i2c.writeRegister(ADDR, REG_SCALING_YSC, 0x35); // -- done 94 | i2c.writeRegister(ADDR, REG_SCALING_DCWCTR, 0x11); //downsample by 2 95 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DIV, 0xf1); //pixel clock divided by 8 96 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DELAY, 0x02); 97 | } 98 | 99 | void OV7670::VGA() 100 | { 101 | i2c.writeRegister(ADDR, REG_COM3, 0x0); //DCW enable -- done 102 | // i2c.writeRegister(ADDR, REG_COM14, 0x19); //pixel clock divided by 4, manual scaling enable, DCW and PCLK controlled by register -- done 103 | i2c.writeRegister(ADDR, REG_COM14, 0); //pixel clock divided by 4, manual scaling enable, DCW and PCLK controlled by register -- done 104 | // i2c.writeRegister(ADDR, REG_SCALING_XSC, 0x3a); // -- done 105 | // i2c.writeRegister(ADDR, REG_SCALING_YSC, 0x35); // -- done 106 | // i2c.writeRegister(ADDR, REG_SCALING_DCWCTR, 0x11); //downsample by 2 107 | // i2c.writeRegister(ADDR, REG_SCALING_PCLK_DIV, 0xf0); //pixel clock divided by 8 108 | // i2c.writeRegister(ADDR, REG_SCALING_PCLK_DELAY, 0x02); 109 | } 110 | 111 | 112 | void OV7670::VGARGB565() 113 | { 114 | i2c.writeRegister(ADDR, REG_COM7, 0b10000000); //all registers default 115 | 116 | i2c.writeRegister(ADDR, REG_CLKRC, 0b10000000); //double clock 117 | i2c.writeRegister(ADDR, REG_COM11, 0b1000 | 0b10); //enable auto 50/60Hz detect + exposure timing can be less... 118 | 119 | i2c.writeRegister(ADDR, REG_COM7, 0b100); //RGB 120 | i2c.writeRegister(ADDR, REG_COM15, 0b11000000 | 0b010000); //RGB565 121 | 122 | /* 123 | i2c.writeRegister(ADDR, REG_COM7, 0b10000000); //all registers default 124 | 125 | i2c.writeRegister(ADDR, REG_CLKRC, 0b10000000); //double clock 126 | i2c.writeRegister(ADDR, REG_COM11, 0b1000 | 0b10); //enable auto 50/60Hz detect + exposure timing can be less... 127 | 128 | i2c.writeRegister(ADDR, REG_COM7, 0b100); //RGB 129 | i2c.writeRegister(ADDR, REG_COM15, 0b11000000 | 0b010000); //RGB565 130 | */ 131 | VGA(); 132 | 133 | // hstart, hstop, vstart, vstop 134 | frameControl(168, 24, 12, 492); //no clue why horizontal needs such strange values, vertical works ok 135 | 136 | //i2c.writeRegister(ADDR, REG_COM10, 0x02); //VSYNC negative 137 | //i2c.writeRegister(ADDR, REG_MVFP, 0x2b); //mirror flip 138 | 139 | i2c.writeRegister(ADDR, 0xb0, 0x84);// no clue what this is but it's most important for colors 140 | saturation(0); 141 | i2c.writeRegister(ADDR, 0x13, 0xe7); //AWB on 142 | i2c.writeRegister(ADDR, 0x6f, 0x9f); // Simple AWB 143 | } 144 | 145 | 146 | void OV7670::QVGARGB565() 147 | { 148 | i2c.writeRegister(ADDR, REG_COM7, 0b10000000); //all registers default 149 | 150 | i2c.writeRegister(ADDR, REG_CLKRC, 0b10000000); //double clock 151 | i2c.writeRegister(ADDR, REG_COM11, 0b1000 | 0b10); //enable auto 50/60Hz detect + exposure timing can be less... 152 | 153 | i2c.writeRegister(ADDR, REG_COM7, 0b100); //RGB 154 | i2c.writeRegister(ADDR, REG_COM15, 0b11000000 | 0b010000); //RGB565 155 | 156 | QVGA(); 157 | 158 | // hstart, hstop, vstart, vstop 159 | frameControl(168, 24, 12, 492); //no clue why horizontal needs such strange values, vertical works ok 160 | 161 | //i2c.writeRegister(ADDR, REG_COM10, 0x02); //VSYNC negative 162 | //i2c.writeRegister(ADDR, REG_MVFP, 0x2b); //mirror flip 163 | 164 | i2c.writeRegister(ADDR, 0xb0, 0x84);// no clue what this is but it's most important for colors 165 | saturation(0); 166 | i2c.writeRegister(ADDR, 0x13, 0xe7); //AWB on 167 | i2c.writeRegister(ADDR, 0x6f, 0x9f); // Simple AWB 168 | } 169 | 170 | 171 | 172 | 173 | 174 | 175 | //////////////////////////////////// 176 | void OV7670::QQQVGA() 177 | { 178 | i2c.writeRegister(ADDR, REG_COM3, 0x04); //DCW enable 179 | i2c.writeRegister(ADDR, REG_COM14, 0x1b); //pixel clock divided by 4, manual scaling enable, DCW and PCLK controlled by register 180 | i2c.writeRegister(ADDR, REG_SCALING_XSC, 0x3a); 181 | i2c.writeRegister(ADDR, REG_SCALING_YSC, 0x35); 182 | i2c.writeRegister(ADDR, REG_SCALING_DCWCTR, 0x33); //downsample by 8 183 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DIV, 0xf3); //pixel clock divided by 8 184 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DELAY, 0x02); 185 | } 186 | 187 | void OV7670::QQVGA() 188 | { 189 | //160x120 (1/4) 190 | //i2c.writeRegister(ADDR, REG_CLKRC, 0x01); 191 | i2c.writeRegister(ADDR, REG_COM3, 0x04); //DCW enable 192 | 193 | i2c.writeRegister(ADDR, REG_COM14, 0x1a); //pixel clock divided by 4, manual scaling enable, DCW and PCLK controlled by register 194 | i2c.writeRegister(ADDR, REG_SCALING_XSC, 0x3a); 195 | i2c.writeRegister(ADDR, REG_SCALING_YSC, 0x35); 196 | 197 | i2c.writeRegister(ADDR, REG_SCALING_DCWCTR, 0x22); //downsample by 4 198 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DIV, 0xf2); //pixel clock divided by 4 199 | i2c.writeRegister(ADDR, REG_SCALING_PCLK_DELAY, 0x02); 200 | } 201 | 202 | void OV7670::QQVGARGB565() 203 | { 204 | i2c.writeRegister(ADDR, REG_COM7, 0b10000000); //all registers default 205 | 206 | i2c.writeRegister(ADDR, REG_CLKRC, 0b10000000); //double clock 207 | i2c.writeRegister(ADDR, REG_COM11, 0b1000 | 0b10); //enable auto 50/60Hz detect + exposure timing can be less... 208 | 209 | i2c.writeRegister(ADDR, REG_COM7, 0b100); //RGB 210 | i2c.writeRegister(ADDR, REG_COM15, 0b11000000 | 0b010000); //RGB565 211 | 212 | QQVGA(); 213 | 214 | frameControl(196, 52, 8, 488); //no clue why horizontal needs such strange values, vertical works ok 215 | 216 | //i2c.writeRegister(ADDR, REG_COM10, 0x02); //VSYNC negative 217 | //i2c.writeRegister(ADDR, REG_MVFP, 0x2b); //mirror flip 218 | 219 | i2c.writeRegister(ADDR, 0xb0, 0x84);// no clue what this is but it's most important for colors 220 | saturation(0); 221 | i2c.writeRegister(ADDR, 0x13, 0xe7); //AWB on 222 | i2c.writeRegister(ADDR, 0x6f, 0x9f); // Simple AWB 223 | } 224 | 225 | void OV7670::QQQVGARGB565() 226 | { 227 | i2c.writeRegister(ADDR, REG_COM7, 0b10000000); //all registers default 228 | 229 | i2c.writeRegister(ADDR, REG_CLKRC, 0b10000000); //double clock 230 | i2c.writeRegister(ADDR, REG_COM11, 0b1000 | 0b10); //enable auto 50/60Hz detect + exposure timing can be less... 231 | 232 | i2c.writeRegister(ADDR, REG_COM7, 0b100); //RGB 233 | i2c.writeRegister(ADDR, REG_COM15, 0b11000000 | 0b010000); //RGB565 234 | 235 | QQQVGA(); 236 | 237 | frameControl(196, 52, 8, 488); //no clue why horizontal needs such strange values, vertical works ok 238 | 239 | //i2c.writeRegister(ADDR, REG_MVFP, 0x2b); //mirror flip 240 | 241 | i2c.writeRegister(ADDR, 0xb0, 0x84);// no clue what this is but it's most important for colors 242 | saturation(0); 243 | i2c.writeRegister(ADDR, 0x13, 0xe7); //AWB on 244 | i2c.writeRegister(ADDR, 0x6f, 0x9f); // Simple AWB 245 | } 246 | 247 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /canvas_htm.h: -------------------------------------------------------------------------------- 1 | static const char canvas_htm[] PROGMEM = "\n"\ 2 | "\n"\ 3 | "\n"\ 4 | "\n"\ 5 | " ESP32-OV7670 - Websocket Demo By Mudassar Tamboli \n"\ 6 | "\n"\ 226 | "\n"\ 227 | "\n"\ 228 | "\n"\ 229 | "
Connecting Camera ...\n"\ 240 | "
\n"\ 241 | "\n"\ 242 | "

ESP32-OV7670 Websocket Video Camera

\n"\ 243 | "\n"\ 244 | "\n"\ 245 | "\n"\ 246 | " \n"\ 247 | " \n"\ 252 | "\n"\ 253 | " \n"\ 258 | "\n"\ 259 | " \n"\ 264 | " \n"\ 271 | " \n"\ 272 | "\n"\ 273 | " \n"\ 274 | " \n"\ 277 | "\n"\ 278 | " \n"\ 281 | " \n"\ 282 | " \n"\ 285 | " \n"\ 286 | " \n"\ 291 | " \n"\ 292 | "\n"\ 293 | "\n"\ 294 | "
\n"\ 248 | "\n"\ 249 | "Your browser does not support the HTML5 canvas tag.\n"\ 250 | "\n"\ 251 | " \n"\ 254 | "\n"\ 255 | "Your browser does not support the HTML5 canvas tag.\n"\ 256 | "\n"\ 257 | " \n"\ 260 | "\n"\ 261 | "Your browser does not support the HTML5 canvas tag.\n"\ 262 | "\n"\ 263 | "
\n"\ 275 | " QQQ-VGA
\n"\ 276 | "
\n"\ 279 | " QQ-VGA
\n"\ 280 | "
\n"\ 283 | " Q-VGA
\n"\ 284 | "
\n"\ 295 | "

\n"\ 296 | "\n"\ 297 | " \n"\ 298 | " \n"\ 299 | " \n"\ 300 | "\n"\ 301 | " \n"\ 302 | " \n"\ 303 | " \n"\ 304 | " \n"\ 305 | "\n"\ 306 | " \n"\ 307 | " \n"\ 308 | " \n"\ 309 | " \n"\ 310 | " \n"\ 311 | "
Websocket not connected
AP IP WiFi IP
192.168.4.1
\n"\ 312 | "\n"\ 313 | "\n"\ 314 | "\n"\ 315 | "\n"\ 316 | "\n"; 317 | 318 | --------------------------------------------------------------------------------