Advertisement
Guest User

Untitled

a guest
Feb 19th, 2021
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <WiFiClientSecure.h>
  3. #include "esp_camera.h"
  4. #include "AudioOutputI2S.h"
  5. #include "ESP8266SAM.h"
  6. #include <Wire.h>
  7. #include <hd44780.h>                       // main hd44780 header
  8. #include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
  9.  
  10. #define PWDN_GPIO_NUM     32
  11. #define RESET_GPIO_NUM    -1
  12. #define XCLK_GPIO_NUM      0
  13. #define SIOD_GPIO_NUM     26
  14. #define SIOC_GPIO_NUM     27
  15.  
  16. #define Y9_GPIO_NUM       35
  17. #define Y8_GPIO_NUM       34
  18. #define Y7_GPIO_NUM       39
  19. #define Y6_GPIO_NUM       36
  20. #define Y5_GPIO_NUM       21
  21. #define Y4_GPIO_NUM       19
  22. #define Y3_GPIO_NUM       18
  23. #define Y2_GPIO_NUM        5
  24. #define VSYNC_GPIO_NUM    25
  25. #define HREF_GPIO_NUM     23
  26. #define PCLK_GPIO_NUM     22
  27.  
  28.  
  29. StaticJsonDocument<768> doc;
  30. AudioOutputI2S *out;
  31. hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
  32. WiFiClientSecure client;
  33.  
  34. const char* ssid = "NSA";
  35. const char* password = "orange";
  36.  
  37. const char* host = "northcentralus.api.cognitive.microsoft.com";
  38. const char* Ocp_Apim_Subscription_Key = "see-tutorial-for-key";
  39. const int Port = 443;
  40. const char* boundry = "dgbfhfh";
  41.  
  42. const int LCD_COLS = 16;
  43. const int LCD_ROWS = 2;
  44.  
  45. const int trigger_button_pin = 3;
  46. long trigger_button_millis = 0;
  47.  
  48. void setup()
  49. {
  50.   Serial.begin(115200);
  51.   Wire.begin(2, 15);
  52.   lcd.begin(LCD_COLS, LCD_ROWS);
  53.  
  54.   Serial.printf("Connecting to the Wifi [%s]...\r\n", ssid);
  55.   WiFi.begin(ssid, password);
  56.   while (WiFi.status() != WL_CONNECTED)
  57.   {
  58.     delay(500);
  59.     Serial.print(".");
  60.   }
  61.   Serial.println("WiFi connected");
  62.  
  63.   pinMode(trigger_button_pin, INPUT);
  64.  
  65.   camera_config_t config;
  66.   config.ledc_channel = LEDC_CHANNEL_0;
  67.   config.ledc_timer = LEDC_TIMER_0;
  68.   config.pin_d0 = Y2_GPIO_NUM;
  69.   config.pin_d1 = Y3_GPIO_NUM;
  70.   config.pin_d2 = Y4_GPIO_NUM;
  71.   config.pin_d3 = Y5_GPIO_NUM;
  72.   config.pin_d4 = Y6_GPIO_NUM;
  73.   config.pin_d5 = Y7_GPIO_NUM;
  74.   config.pin_d6 = Y8_GPIO_NUM;
  75.   config.pin_d7 = Y9_GPIO_NUM;
  76.   config.pin_xclk = XCLK_GPIO_NUM;
  77.   config.pin_pclk = PCLK_GPIO_NUM;
  78.   config.pin_vsync = VSYNC_GPIO_NUM;
  79.   config.pin_href = HREF_GPIO_NUM;
  80.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  81.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  82.   config.pin_pwdn = PWDN_GPIO_NUM;
  83.   config.pin_reset = RESET_GPIO_NUM;
  84.   config.xclk_freq_hz = 20000000;
  85.   config.pixel_format = PIXFORMAT_JPEG;
  86.  
  87.   if (psramFound())
  88.   {
  89.     config.frame_size = FRAMESIZE_SXGA; // FRAMESIZE_+QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
  90.     config.jpeg_quality = 10;
  91.     config.fb_count = 2;
  92.   }
  93.   else
  94.   {
  95.     config.frame_size = FRAMESIZE_VGA;
  96.     config.jpeg_quality = 12;
  97.     config.fb_count = 1;
  98.   }
  99.  
  100.   // Initialise Camera
  101.   esp_err_t err = esp_camera_init(&config);
  102.   if (err != ESP_OK)
  103.   {
  104.     Serial.printf("Camera init failed with error 0x%x", err);
  105.     return;
  106.   }
  107.  
  108.   sensor_t * s = esp_camera_sensor_get();
  109.   s->set_framesize(s, FRAMESIZE_VGA);
  110.  
  111.   out = new AudioOutputI2S(1, 0, 8, 0);
  112.   out->SetPinout(12, 13, 14); // b l d
  113.  
  114.   connectToServer();
  115. }
  116.  
  117. void connectToServer()
  118. {
  119.   Serial.printf("Connecting to %s:%d... ", host, Port);
  120.   if (!client.connect(host, Port))
  121.   {
  122.     Serial.println("Failure in connection with the server");
  123.     return;
  124.   }
  125. }
  126.  
  127. void sendPhotoToServer()
  128. {
  129.  
  130.   lcd.clear();
  131.   lcd.print("Analysis");
  132.   lcd.setCursor(0, 1);
  133.   lcd.print("requested.");
  134.  
  135.   String start_request = "";
  136.   String end_request = "";
  137.  
  138.  
  139.   start_request = start_request + "--" + boundry + "\r\n";
  140.   start_request = start_request + "Content-Disposition: form-data; name=\"file\"; filename=\"CAM.jpg\"\r\n";
  141.   start_request = start_request + "Content-Type: image/jpg\r\n";
  142.   start_request = start_request + "\r\n";
  143.  
  144.   end_request = end_request + "\r\n";
  145.   end_request = end_request + "--" + boundry + "--" + "\r\n";
  146.  
  147.   camera_fb_t * fb = NULL;
  148.   fb = esp_camera_fb_get();
  149.   if (!fb)
  150.   {
  151.     Serial.println("Camera capture failed");
  152.     return;
  153.   }
  154.  
  155.  
  156.   delay(100);
  157.  
  158.   int contentLength = (int)fb->len + start_request.length() + end_request.length();
  159.  
  160.   String headers = "POST https://northcentralus.api.cognitive.microsoft.com/vision/v3.1/describe?maxCandidates=1&language=en HTTP/1.1\r\n";
  161.   headers = headers + "Host: " + host + "\r\n";
  162.   headers = headers + "User-Agent: ESP32" + "\r\n";
  163.   headers = headers + "Accept: */*\r\n";
  164.   headers = headers + "Content-Type: multipart/form-data; boundary=" + boundry + "\r\n";
  165.   headers = headers + "Ocp-Apim-Subscription-Key: " + Ocp_Apim_Subscription_Key + "\r\n";
  166.   headers = headers + "Content-Length: " + contentLength + "\r\n";
  167.   headers = headers + "\r\n";
  168.   client.print(headers);
  169.   Serial.print(headers);
  170.   client.flush();
  171.  
  172.   lcd.setCursor(10, 1);
  173.   lcd.print(".");
  174.  
  175.   Serial.print(start_request);
  176.   client.print(start_request);
  177.   client.flush();
  178.  
  179.   int iteration = fb->len / 1024;
  180.   for (int i = 0; i < iteration; i++)
  181.   {
  182.     client.write(fb->buf, 1024);
  183.     fb->buf += 1024;
  184.     client.flush();
  185.   }
  186.   size_t remain = fb->len % 1024;
  187.   client.write(fb->buf, remain);
  188.   client.flush();
  189.   client.print(end_request);
  190.  
  191.   lcd.setCursor(11, 1);
  192.   lcd.print(".");
  193.  
  194.   // header response
  195.   while (client.connected()) {
  196.     String line = client.readStringUntil('\n');
  197.     Serial.println(line);
  198.     if (line == "\r") {
  199.       Serial.println("headers received");
  200.       break;
  201.     }
  202.   }
  203.   // response body
  204.   String description;
  205.   while (client.available()) {
  206.     char c = client.read();
  207.     description = description + c;
  208.     Serial.write(c);
  209.   }
  210.  
  211.   client.flush();
  212.  
  213.   esp_camera_fb_return(fb);
  214.  
  215.   deserializeJson(doc, description);
  216.   const char* description_captions_0_text = doc["description"]["captions"][0]["text"];
  217.   char descriptionWithFullStop[100];   // array to hold the result.
  218.   strcpy(descriptionWithFullStop, description_captions_0_text); // copy string one into the result.
  219.   strcat(descriptionWithFullStop, ".");
  220.   float description_captions_0_confidence = doc["description"]["captions"][0]["confidence"];
  221.   Serial.println(descriptionWithFullStop);
  222.  
  223.   // TODO - Use confidence to add 'Might be...', 'Looks like...', 'I'm sure...' to phrase
  224.  
  225.   lcd.clear();
  226.   lcd.lineWrap();
  227.   lcd.print(description_captions_0_text);
  228.  
  229.   out->begin();
  230.   ESP8266SAM *sam = new ESP8266SAM;
  231.   sam->SetVoice(sam->SAMVoice::VOICE_SAM);
  232.   sam->Say(out, descriptionWithFullStop);
  233.   delete sam;
  234.   out->stop();
  235.  
  236.   // Chose to wrap rather than scroll
  237.   //  int scrollStepsNeeded =  strlen(description_captions_0_text) - LCD_COLS;
  238.   //  for (int i = 1; i <= scrollStepsNeeded; i++) {
  239.   //    lcd.scrollDisplayLeft();
  240.   //    delay(200);
  241.   //  }
  242.  
  243. }
  244.  
  245. void loop()
  246. {
  247.  
  248.   // add in a delay to avoid repeat presses
  249.   if (digitalRead(trigger_button_pin) == HIGH && millis() - trigger_button_millis > 1000) {
  250.     trigger_button_millis = millis();
  251.     Serial.println("button pressed");
  252.     sendPhotoToServer();
  253.   }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement