Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
20,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.25 KB | None | 0 0
  1. #include <ArduinoWebsockets.h>
  2. #include <WiFi.h>
  3. #include <ESPAsyncWebServer.h>
  4. #include "esp_camera.h"
  5. #include "soc/soc.h"      
  6. #include "soc/rtc_cntl_reg.h""NSA";
  7. const char* password = "orange";
  8.  
  9. #define PWDN_GPIO_NUM     32
  10. #define RESET_GPIO_NUM    -1
  11. #define XCLK_GPIO_NUM      0
  12. #define SIOD_GPIO_NUM     26
  13. #define SIOC_GPIO_NUM     27
  14. #define Y9_GPIO_NUM       35
  15. #define Y8_GPIO_NUM       34
  16. #define Y7_GPIO_NUM       39
  17. #define Y6_GPIO_NUM       36
  18. #define Y5_GPIO_NUM       21
  19. #define Y4_GPIO_NUM       19
  20. #define Y3_GPIO_NUM       18
  21. #define Y2_GPIO_NUM        5
  22. #define VSYNC_GPIO_NUM    25
  23. #define HREF_GPIO_NUM     23
  24. #define PCLK_GPIO_NUM     22
  25.  
  26. camera_fb_t * fb = NULL;
  27.  
  28. using namespace websockets;
  29. WebsocketsServer WSserver;
  30. AsyncWebServer webserver(80);
  31.  
  32. // Arduino like analogWrite
  33. // value has to be between 0 and valueMax
  34. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 180) {
  35.   // calculate duty, 8191 from 2 ^ 13 - 1
  36.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  37.   ledcWrite(channel, duty);
  38. }
  39.  
  40. void setup() {
  41.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  42.   Serial.begin(115200);
  43.   // Ai-Thinker: pins 2 and 12
  44.   ledcSetup(2, 50, 16); //channel, freq, resolution
  45.   ledcAttachPin(2, 2); // pin, channel
  46.   ledcSetup(4, 50, 16);
  47.   ledcAttachPin(12, 4);
  48.    
  49.   camera_config_t config;
  50.   config.ledc_channel = LEDC_CHANNEL_0;
  51.   config.ledc_timer = LEDC_TIMER_0;
  52.   config.pin_d0 = Y2_GPIO_NUM;
  53.   config.pin_d1 = Y3_GPIO_NUM;
  54.   config.pin_d2 = Y4_GPIO_NUM;
  55.   config.pin_d3 = Y5_GPIO_NUM;
  56.   config.pin_d4 = Y6_GPIO_NUM;
  57.   config.pin_d5 = Y7_GPIO_NUM;
  58.   config.pin_d6 = Y8_GPIO_NUM;
  59.   config.pin_d7 = Y9_GPIO_NUM;
  60.   config.pin_xclk = XCLK_GPIO_NUM;
  61.   config.pin_pclk = PCLK_GPIO_NUM;
  62.   config.pin_vsync = VSYNC_GPIO_NUM;
  63.   config.pin_href = HREF_GPIO_NUM;
  64.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  65.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  66.   config.pin_pwdn = PWDN_GPIO_NUM;
  67.   config.pin_reset = RESET_GPIO_NUM;
  68.   config.xclk_freq_hz = 20000000;
  69.   config.pixel_format = PIXFORMAT_JPEG;
  70.   //init with high specs to pre-allocate larger buffers
  71.   if (psramFound()) {
  72.     config.frame_size = FRAMESIZE_UXGA;
  73.     config.jpeg_quality = 10;
  74.     config.fb_count = 2;
  75.   } else {
  76.     config.frame_size = FRAMESIZE_SVGA;
  77.     config.jpeg_quality = 12;
  78.     config.fb_count = 1;
  79.   }
  80.    
  81.   // camera init
  82.   esp_err_t err = esp_camera_init(&config);
  83.   if (err != ESP_OK) {
  84.     Serial.printf("Camera init failed with error 0x%x", err);
  85.     return;
  86.   }
  87.  
  88.   sensor_t * s = esp_camera_sensor_get();
  89.   s->set_framesize(s, FRAMESIZE_QVGA);
  90.    
  91.   WiFi.mode(WIFI_STA);
  92.   WiFi.begin(ssid, password);
  93.  
  94.   // Wait for connection
  95.   while (WiFi.status() != WL_CONNECTED) {
  96.     delay(500);
  97.     Serial.print(".");
  98.   }
  99.  
  100.   Serial.println("");
  101.   Serial.print("Connected to ");
  102.   Serial.print("IP address: ");
  103.   Serial.println(WiFi.localIP());
  104.  
  105.   webserver.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  106.     AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html_gz, sizeof(index_html_gz));
  107.     response->addHeader("Content-Encoding", "gzip");
  108.     request->send(response);
  109.   });
  110.  
  111.   webserver.begin();
  112.   WSserver.listen(82);
  113. }
  114.  
  115.  
  116. void handle_message(WebsocketsMessage msg) {
  117.   int commaIndex = msg.data().indexOf(',');
  118.   int panValue = msg.data().substring(0, commaIndex).toInt();
  119.   int tiltValue = msg.data().substring(commaIndex + 1).toInt();
  120.  
  121.   panValue = map(panValue, -90, 90, 0, 180); // 0-180
  122.   tiltValue = map(tiltValue, -90, 90, 180, 0); // 0-180 reversed
  123.  
  124.   ledcAnalogWrite(2, panValue); // channel, value
  125.   ledcAnalogWrite(4, tiltValue);
  126. }
  127.  
  128. void loop() {
  129.   auto client = WSserver.accept();
  130.   client.onMessage(handle_message);
  131.   while (client.available()) {
  132.     client.poll();
  133.     fb = esp_camera_fb_get();
  134.     client.sendBinary((const char *)fb->buf, fb->len);
  135.     esp_camera_fb_return(fb);
  136.     fb = NULL;
  137.   }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement