Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <ArduinoJson.h>
  3. #include "esp_camera.h"
  4. #include <WiFi.h>
  5. #include <WiFiMulti.h>
  6. #include "Arduino.h"
  7. #include "fb_gfx.h"
  8. #include "soc/soc.h" //disable brownout problems
  9. #include "soc/rtc_cntl_reg.h"  //disable brownout problems
  10. #include "soc/rtc_wdt.h"
  11. #include <WebSocketsClient.h>
  12.  
  13. void streamImage( void * parameter);
  14. void parseWebSocketPayLoad(String payload);
  15.  
  16. WiFiMulti WiFiMulti;
  17. WebSocketsClient webSocket;
  18. boolean conectado;
  19. boolean sended = false;
  20.  
  21. TaskHandle_t TaskStreamImage;
  22.  
  23. const char* ssid1 = "Repetior_Machado";
  24. const char* password1 = "6135643386648";
  25.  
  26. const char* ssid2 = "Claro_INFORMATIONTECNOLOGYXOA";
  27. const char* password2 = "XOAIPSAX2020";
  28.  
  29. const char* ssid3 = "ConfigAp";
  30. const char* password3 = "12345678";
  31.  
  32. String remotIp = "116.203.152.218";
  33. int remotPort = 8006;
  34.  
  35. #define PWDN_GPIO_NUM     32
  36. #define RESET_GPIO_NUM    -1
  37. #define XCLK_GPIO_NUM      0
  38. #define SIOD_GPIO_NUM     26
  39. #define SIOC_GPIO_NUM     27
  40. #define Y9_GPIO_NUM       35
  41. #define Y8_GPIO_NUM       34
  42. #define Y7_GPIO_NUM       39
  43. #define Y6_GPIO_NUM       36
  44. #define Y5_GPIO_NUM       21
  45. #define Y4_GPIO_NUM       19
  46. #define Y3_GPIO_NUM       18
  47. #define Y2_GPIO_NUM        5
  48. #define VSYNC_GPIO_NUM    25
  49. #define HREF_GPIO_NUM     23
  50. #define PCLK_GPIO_NUM     22
  51.  
  52. //IPAddress local_IP(192, 168, 0, 202);
  53. //IPAddress gateway(192, 168, 0, 1);  
  54. //IPAddress subnet(255, 255, 255, 0);
  55. //IPAddress primaryDNS(8, 8, 8, 8);   //optional
  56. //IPAddress secondaryDNS(8, 8, 4, 4); //optional
  57.  
  58. camera_config_t config_cam;
  59. camera_fb_t * fb = NULL;
  60. esp_err_t res = ESP_OK;
  61. size_t _jpg_buf_len = 0;
  62. uint8_t * _jpg_buf = NULL;
  63. char * part_buf[64];
  64. boolean streaming = false;
  65.  
  66. void configCamera(camera_config_t config)
  67. {
  68.  
  69. //  FRAMESIZE_QQVGA,    // 160x120
  70. //  FRAMESIZE_QQVGA2,   // 128x160
  71. //  FRAMESIZE_QCIF,     // 176x144
  72. //  FRAMESIZE_HQVGA,    // 240x176
  73. //  FRAMESIZE_QVGA,     // 320x240
  74. //  FRAMESIZE_CIF,      // 400x296
  75. //  FRAMESIZE_VGA,      // 640x480
  76. //  FRAMESIZE_SVGA,     // 800x600
  77. //  FRAMESIZE_XGA,      // 1024x768
  78. //  FRAMESIZE_SXGA,     // 1280x1024
  79. //  FRAMESIZE_UXGA,     // 1600x1200
  80. //  FRAMESIZE_QXGA,     // 2048*1536
  81.  
  82.   config.ledc_channel = LEDC_CHANNEL_0;
  83.   config.ledc_timer = LEDC_TIMER_0;
  84.   config.pin_d0 = Y2_GPIO_NUM;
  85.   config.pin_d1 = Y3_GPIO_NUM;
  86.   config.pin_d2 = Y4_GPIO_NUM;
  87.   config.pin_d3 = Y5_GPIO_NUM;
  88.   config.pin_d4 = Y6_GPIO_NUM;
  89.   config.pin_d5 = Y7_GPIO_NUM;
  90.   config.pin_d6 = Y8_GPIO_NUM;
  91.   config.pin_d7 = Y9_GPIO_NUM;
  92.   config.pin_xclk = XCLK_GPIO_NUM;
  93.   config.pin_pclk = PCLK_GPIO_NUM;
  94.   config.pin_vsync = VSYNC_GPIO_NUM;
  95.   config.pin_href = HREF_GPIO_NUM;
  96.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  97.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  98.   config.pin_pwdn = PWDN_GPIO_NUM;
  99.   config.pin_reset = RESET_GPIO_NUM;
  100.   config.xclk_freq_hz = 10000000;
  101.   config.pixel_format = PIXFORMAT_JPEG;
  102.   config.frame_size = FRAMESIZE_VGA;  
  103.   config.jpeg_quality = 12;
  104.   config.fb_count = 2;
  105.  
  106.   // Camera init
  107.   esp_err_t err = esp_camera_init(&config);
  108.   if (err != ESP_OK) {
  109.     return;
  110.   }  
  111.  
  112. //  sensor_t * s = esp_camera_sensor_get();
  113. //  s->set_framesize(s, FRAMESIZE_VGA);
  114. }
  115.  
  116. void webSocketEvent(WStype_t type, uint8_t * payload, size_t length){
  117.  
  118.   switch (type) {
  119.     case WStype_DISCONNECTED:{
  120.       streaming = false;
  121.       conectado = false;
  122.       break;
  123.       }      
  124.     case WStype_CONNECTED:{
  125.       Serial.println("WebSocket Conectado!!!");
  126.       webSocket.sendTXT("EspCam32");
  127.       conectado = true;
  128.         break;
  129.       }      
  130.     case WStype_TEXT:{
  131.        rtc_wdt_feed();
  132.        String command = String((char*)payload);  
  133.        parseWebSocketPayLoad(command);
  134.        break;
  135.       }
  136.      
  137.     case WStype_BIN:
  138.       break;
  139.     case WStype_ERROR:{
  140.        Serial.println("WebSocket error");
  141.        break;
  142.     }
  143.     case WStype_FRAGMENT_TEXT_START:
  144.     case WStype_FRAGMENT_BIN_START:
  145.     case WStype_FRAGMENT:
  146.     case WStype_FRAGMENT_FIN:
  147.       break;
  148.   }
  149. }
  150.  
  151. void parseWebSocketPayLoad(String payload)
  152. {  
  153.    Serial.println(payload);
  154.    DynamicJsonDocument outDoc(1024);
  155.    deserializeJson(outDoc, payload);
  156.    JsonObject obj = outDoc.as<JsonObject>();
  157.  
  158.    String event =  obj["event"];
  159.  
  160.    if(event == "start")
  161.    {
  162.      streaming = true;
  163.    }  
  164.  
  165.    if(event == "stop")
  166.    {
  167.      streaming = false;
  168.    }
  169. }
  170.  
  171. void startCameraLoop() {
  172.  
  173.   webSocket.begin(remotIp, remotPort, "/");
  174.   webSocket.onEvent(webSocketEvent);
  175.   webSocket.setReconnectInterval(3000);
  176.  
  177.   xTaskCreatePinnedToCore(
  178.     streamImage, /* Function to implement the task */
  179.     "TaskStreamImage", /* Name of the task */
  180.     10000,  /* Stack size in words */
  181.     NULL,  /* Task input parameter */
  182.     1,  /* Priority of the task */
  183.     &TaskStreamImage,  /* Task handle. */
  184.     0); /* Core where the task should run */
  185. }
  186.  
  187. void streamImage( void * parameter) {
  188.   vTaskDelay(50);
  189.   for (;;)
  190.   {
  191.     vTaskDelay(100);    
  192.     if(streaming)
  193.     {
  194.         if(conectado)
  195.         {
  196.           camera_fb_t * fb = esp_camera_fb_get();
  197.           webSocket.sendBIN(fb->buf, fb->len);
  198.           esp_camera_fb_return(fb);          
  199.         }
  200.         else
  201.         {
  202.           streaming = false;
  203.         }  
  204.     }    
  205.   }  
  206. }
  207.  
  208. void setup() {
  209.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  210.  
  211.   Serial.begin(115200);
  212.  
  213.   delay(100);
  214.  
  215.   configCamera(config_cam);  
  216.  
  217.   WiFiMulti.addAP(ssid1, password1);
  218.   WiFiMulti.addAP(ssid2, password2);
  219.   WiFiMulti.addAP(ssid3, password3);
  220.  
  221.   while(WiFiMulti.run() != WL_CONNECTED) {
  222.     delay(100);
  223.   }
  224.  
  225.   Serial.println();
  226.   Serial.println("Conectado a WiFi!!");
  227.   startCameraLoop();
  228. }
  229.  
  230. void loop() {
  231.   if(WiFi.status() != WL_CONNECTED)
  232.   {
  233.     ESP.restart();
  234.   }
  235.   else
  236.   {  
  237.     webSocket.loop();    
  238.     delay(1);
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement