Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- #include "esp_camera.h"
- #include <WiFi.h>
- #include <WiFiMulti.h>
- #include "Arduino.h"
- #include "fb_gfx.h"
- #include "soc/soc.h" //disable brownout problems
- #include "soc/rtc_cntl_reg.h" //disable brownout problems
- #include "soc/rtc_wdt.h"
- #include <WebSocketsClient.h>
- void streamImage( void * parameter);
- void parseWebSocketPayLoad(String payload);
- WiFiMulti WiFiMulti;
- WebSocketsClient webSocket;
- boolean conectado;
- boolean sended = false;
- TaskHandle_t TaskStreamImage;
- const char* ssid1 = "Repetior_Machado";
- const char* password1 = "6135643386648";
- const char* ssid2 = "Claro_INFORMATIONTECNOLOGYXOA";
- const char* password2 = "XOAIPSAX2020";
- const char* ssid3 = "ConfigAp";
- const char* password3 = "12345678";
- String remotIp = "116.203.152.218";
- int remotPort = 8006;
- #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
- //IPAddress local_IP(192, 168, 0, 202);
- //IPAddress gateway(192, 168, 0, 1);
- //IPAddress subnet(255, 255, 255, 0);
- //IPAddress primaryDNS(8, 8, 8, 8); //optional
- //IPAddress secondaryDNS(8, 8, 4, 4); //optional
- camera_config_t config_cam;
- camera_fb_t * fb = NULL;
- esp_err_t res = ESP_OK;
- size_t _jpg_buf_len = 0;
- uint8_t * _jpg_buf = NULL;
- char * part_buf[64];
- boolean streaming = false;
- void configCamera(camera_config_t config)
- {
- // FRAMESIZE_QQVGA, // 160x120
- // FRAMESIZE_QQVGA2, // 128x160
- // FRAMESIZE_QCIF, // 176x144
- // FRAMESIZE_HQVGA, // 240x176
- // FRAMESIZE_QVGA, // 320x240
- // FRAMESIZE_CIF, // 400x296
- // FRAMESIZE_VGA, // 640x480
- // FRAMESIZE_SVGA, // 800x600
- // FRAMESIZE_XGA, // 1024x768
- // FRAMESIZE_SXGA, // 1280x1024
- // FRAMESIZE_UXGA, // 1600x1200
- // FRAMESIZE_QXGA, // 2048*1536
- 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 = 10000000;
- config.pixel_format = PIXFORMAT_JPEG;
- config.frame_size = FRAMESIZE_VGA;
- config.jpeg_quality = 12;
- config.fb_count = 2;
- // Camera init
- esp_err_t err = esp_camera_init(&config);
- if (err != ESP_OK) {
- return;
- }
- // sensor_t * s = esp_camera_sensor_get();
- // s->set_framesize(s, FRAMESIZE_VGA);
- }
- void webSocketEvent(WStype_t type, uint8_t * payload, size_t length){
- switch (type) {
- case WStype_DISCONNECTED:{
- streaming = false;
- conectado = false;
- break;
- }
- case WStype_CONNECTED:{
- Serial.println("WebSocket Conectado!!!");
- webSocket.sendTXT("EspCam32");
- conectado = true;
- break;
- }
- case WStype_TEXT:{
- rtc_wdt_feed();
- String command = String((char*)payload);
- parseWebSocketPayLoad(command);
- break;
- }
- case WStype_BIN:
- break;
- case WStype_ERROR:{
- Serial.println("WebSocket error");
- break;
- }
- case WStype_FRAGMENT_TEXT_START:
- case WStype_FRAGMENT_BIN_START:
- case WStype_FRAGMENT:
- case WStype_FRAGMENT_FIN:
- break;
- }
- }
- void parseWebSocketPayLoad(String payload)
- {
- Serial.println(payload);
- DynamicJsonDocument outDoc(1024);
- deserializeJson(outDoc, payload);
- JsonObject obj = outDoc.as<JsonObject>();
- String event = obj["event"];
- if(event == "start")
- {
- streaming = true;
- }
- if(event == "stop")
- {
- streaming = false;
- }
- }
- void startCameraLoop() {
- webSocket.begin(remotIp, remotPort, "/");
- webSocket.onEvent(webSocketEvent);
- webSocket.setReconnectInterval(3000);
- xTaskCreatePinnedToCore(
- streamImage, /* Function to implement the task */
- "TaskStreamImage", /* Name of the task */
- 10000, /* Stack size in words */
- NULL, /* Task input parameter */
- 1, /* Priority of the task */
- &TaskStreamImage, /* Task handle. */
- 0); /* Core where the task should run */
- }
- void streamImage( void * parameter) {
- vTaskDelay(50);
- for (;;)
- {
- vTaskDelay(100);
- if(streaming)
- {
- if(conectado)
- {
- camera_fb_t * fb = esp_camera_fb_get();
- webSocket.sendBIN(fb->buf, fb->len);
- esp_camera_fb_return(fb);
- }
- else
- {
- streaming = false;
- }
- }
- }
- }
- void setup() {
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
- Serial.begin(115200);
- delay(100);
- configCamera(config_cam);
- WiFiMulti.addAP(ssid1, password1);
- WiFiMulti.addAP(ssid2, password2);
- WiFiMulti.addAP(ssid3, password3);
- while(WiFiMulti.run() != WL_CONNECTED) {
- delay(100);
- }
- Serial.println();
- Serial.println("Conectado a WiFi!!");
- startCameraLoop();
- }
- void loop() {
- if(WiFi.status() != WL_CONNECTED)
- {
- ESP.restart();
- }
- else
- {
- webSocket.loop();
- delay(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement