#include #include #include #include "soc/soc.h" #include "soc/rtc_cntl_reg.h" #include "esp_camera.h" int pinPIR = 2; #define PWDN_GPIO_NUM 32 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 0 #define SIOD_GPIO_NUM 26 #define SIOC_GPIO_NUM 27 #define Y9_GPIO_NUM 35 #define Y8_GPIO_NUM 34 #define Y7_GPIO_NUM 39 #define Y6_GPIO_NUM 36 #define Y5_GPIO_NUM 21 #define Y4_GPIO_NUM 19 #define Y3_GPIO_NUM 18 #define Y2_GPIO_NUM 5 #define VSYNC_GPIO_NUM 25 #define HREF_GPIO_NUM 23 #define PCLK_GPIO_NUM 22 // Network information. #define WIFI_SSID "Home" #define WIFI_PASSWORD "165125819" // ThingSpeak information. #define IMAGE_CHANNEL_ID "b539b0xxxxx" #define IMAGE_CHANNEL_API_KEY "MFBN5XNBxxxxx" #define THINGSPEAK_ADDRESS "data.thingspeak.com" #define RESPONSE_TIMEOUT 5000 #define MAX_BLOCK_SIZE 16384 #define SNAPSHOT_PERIOD 50000 WiFiClientSecure client = NULL; unsigned long previousSnapshotTime = 0; bool cameraAvailable = false; boolean pirState = true; void connectWifi() { while (WiFi.status() != WL_CONNECTED) { WiFi.begin( WIFI_SSID, WIFI_PASSWORD ); Serial.println( "Connecting to Wi-Fi" ); delay( 5000 ); } // Skip checking of server certs. client.setInsecure(); Serial.println( "WiFi Connected" ); } bool initCamera() { static 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; //init with high specs to pre-allocate larger buffers if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 4; //0-63 lower number means higher quality config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 6; //0-63 lower number means higher quality config.fb_count = 1; } esp_err_t err = esp_camera_init(&config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x", err); delay(1000); ESP.restart(); } sensor_t * s = esp_camera_sensor_get(); s->set_framesize(s, FRAMESIZE_SXGA); // UXGA|SXGA|XGA|SVGA|VGA|CIF|QVGA|HQVGA|QQVGA s->set_brightness(s, 0); // -2 to 2 s->set_contrast(s, 2); // -2 to 2 s->set_saturation(s, -2); // -2 to 2 s->set_whitebal(s, 1); // 0 = disable , 1 = enable s->set_awb_gain(s, 1); // 0 = disable , 1 = enable s->set_wb_mode(s, 0); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home) s->set_exposure_ctrl(s, 1); // 0 = disable , 1 = enable s->set_aec2(s, 1); // 0 = disable , 1 = enable s->set_gain_ctrl(s, 0); // 0 = disable , 1 = enable s->set_agc_gain(s, 0); // 0 to 30 s->set_gainceiling(s, (gainceiling_t)6); // 0 to 6 s->set_bpc(s, 1); // 0 = disable , 1 = enable s->set_wpc(s, 1); // 0 = disable , 1 = enable s->set_raw_gma(s, 1); // 0 = disable , 1 = enable (makes much lighter and noisy) s->set_lenc(s, 1); // 0 = disable , 1 = enable s->set_hmirror(s, 0); // 0 = disable , 1 = enable s->set_vflip(s, 0); // 0 = disable , 1 = enable s->set_dcw(s, 0); // 0 = disable , 1 = enable return true; } void printResponse(){ unsigned long startTime = millis(); // Wait a few hundred milliseconds for server to process the request delay( 100 ); while ( client.available() < 1 && (( millis() - startTime ) < RESPONSE_TIMEOUT ) ){ delay( 10 ); } // Read server response and print it to serial port if( client.available() > 0 ){ do { Serial.write(client.read()); } while ( client.available() > 0 ); Serial.println(' '); } } bool snapshotToThingSpeak() { bool result = false; // Only send image to ThingSpeak if sufficient time has passed since previous send. if ( cameraAvailable && ( millis() > previousSnapshotTime + SNAPSHOT_PERIOD) ) { // Capture a new image from the camera. camera_fb_t *frame = esp_camera_fb_get(); if (!frame) return result; // Connect to ThingSpeak and send image // Echo client commands to serial port for debugging if (client.connect(THINGSPEAK_ADDRESS, 443)) { Serial.println("Writing image to ThingSpeak"); client.print( "POST /channels/"); Serial.print( "POST /channels/"); client.print( IMAGE_CHANNEL_ID ); Serial.print( IMAGE_CHANNEL_ID ); client.println( "/images HTTP/1.1" ); Serial.print( IMAGE_CHANNEL_ID ); client.println( "Connection: close" ); Serial.println( "Connection: close" ); client.print( "Host: " ); Serial.print( "Host: " ); client.println(THINGSPEAK_ADDRESS); Serial.println(THINGSPEAK_ADDRESS); client.print( "Thingspeak-image-channel-api-key: "); Serial.print( "Thingspeak-image-channel-api-key: "); client.println( IMAGE_CHANNEL_API_KEY ); Serial.println( IMAGE_CHANNEL_API_KEY ); client.println( "Content-Type: image/jpeg" ); Serial.println( "Content-Type: image/jpeg" ); client.print( "Content-Length: "); Serial.print( "Content-Length: "); client.println(frame->len); Serial.println(frame->len); client.println( ); Serial.println( ); uint8_t *fbBuf = frame->buf; long int fbLen = frame->len; do { client.write(fbBuf, ( (fbLen > MAX_BLOCK_SIZE) ? MAX_BLOCK_SIZE : fbLen) ); fbLen -= MAX_BLOCK_SIZE; fbBuf += MAX_BLOCK_SIZE; } while (fbLen > 0); client.flush(); // Print out server response to serial port. printResponse(); client.stop(); result = true; } else { Serial.print("Unable to connect to "); Serial.println(THINGSPEAK_ADDRESS); } // Update the stored time when the last image was written. previousSnapshotTime = millis(); // Free the memory buffer for the image so we don't leak memory and segmentation fault. esp_camera_fb_return(frame); return result; } } void setup() { WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // Open serial port for printing debug messages. Serial.begin( 115200 ); // Connect to WiFi connectWifi(); // Initialize the camera if (initCamera()){ Serial.println("Camera initialized"); cameraAvailable = true; } } void loop() { // If the connection is lost, recconnect. if (WiFi.status() != WL_CONNECTED) { connectWifi(); } if (pirState==true) { pinMode(pinPIR, INPUT_PULLUP); if (digitalRead(pinPIR)==1) { snapshotToThingSpeak(); delay(5000); } } }