that_one_nigga_you_k

placa final

Jun 6th, 2025
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. #define TINY_GSM_DEBUG Serial
  2. #define TINY_GSM_MODEM_SIM800
  3.  
  4. #include <TinyGsmClient.h>
  5. #include "esp_camera.h"
  6.  
  7. #define FLASH_PIN 4
  8. #define CAMERA_MODEL_AI_THINKER
  9.  
  10. // --- Pinos da Câmera ---
  11. #define PWDN_GPIO_NUM  32
  12. #define RESET_GPIO_NUM -1
  13. #define XCLK_GPIO_NUM  0
  14. #define SIOD_GPIO_NUM  26
  15. #define SIOC_GPIO_NUM  27
  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. // --- Pinos para SIM800L ---
  29. #define SIM800_RX_PIN 15
  30. #define SIM800_TX_PIN 13
  31. HardwareSerial SerialSIM(2);
  32.  
  33. // --- Configurações do Servidor ---
  34. const char NGROK_TCP_ADDRESS[] = "1.tcp.sa.ngrok.io"; // <- MUDE AQUI
  35. const int NGROK_TCP_PORT       = 21302;                // <- MUDE AQUI
  36.  
  37. // --- Caminhos (Endpoints) do Servidor ---
  38. const char* UPLOAD_PATH   = "/raw_upload"; // <<< MUDANÇA IMPORTANTE
  39. const char* COMMAND_PATH  = "/command";
  40.  
  41. // --- Configurações do APN ---
  42. char apn[]  = "claro.com.br";
  43. char user[] = "claro";
  44. char pass[] = "claro";
  45.  
  46. TinyGsm modem(SerialSIM);
  47. TinyGsmClient client(modem);
  48.  
  49. unsigned long previousMillis_upload = 0;
  50. unsigned long previousMillis_command = 0;
  51. long uploadInterval = 8000;
  52. long commandInterval = 8000;
  53.  
  54. bool setupCamera(){
  55.   camera_config_t config;
  56.   config.ledc_channel = LEDC_CHANNEL_0;
  57.   config.ledc_timer = LEDC_TIMER_0;
  58.   config.pin_d0 = Y2_GPIO_NUM;
  59.   config.pin_d1 = Y3_GPIO_NUM;
  60.   config.pin_d2 = Y4_GPIO_NUM;
  61.   config.pin_d3 = Y5_GPIO_NUM;
  62.   config.pin_d4 = Y6_GPIO_NUM;
  63.   config.pin_d5 = Y7_GPIO_NUM;
  64.   config.pin_d6 = Y8_GPIO_NUM;
  65.   config.pin_d7 = Y9_GPIO_NUM;
  66.   config.pin_xclk = XCLK_GPIO_NUM;
  67.   config.pin_pclk = PCLK_GPIO_NUM;
  68.   config.pin_vsync = VSYNC_GPIO_NUM;
  69.   config.pin_href = HREF_GPIO_NUM;
  70.   config.pin_sccb_sda = SIOD_GPIO_NUM;
  71.   config.pin_sccb_scl = SIOC_GPIO_NUM;
  72.   config.pin_pwdn = PWDN_GPIO_NUM;
  73.   config.pin_reset = RESET_GPIO_NUM;
  74.   config.xclk_freq_hz = 20000000;
  75.   config.pixel_format = PIXFORMAT_JPEG;
  76.   config.frame_size = FRAMESIZE_QVGA;
  77.   config.jpeg_quality = 12;
  78.   config.fb_count = 1;
  79.   esp_err_t err = esp_camera_init(&config);
  80.   if (err != ESP_OK) { return false; }
  81.   return true;
  82. }
  83.  
  84. void uploadPhoto() {
  85.   Serial.println("Capturando foto...");
  86.   camera_fb_t *fb = esp_camera_fb_get();
  87.   if (!fb) {
  88.     Serial.println("Falha ao capturar foto.");
  89.     return;
  90.   }
  91.  
  92.   if (!client.connect(NGROK_TCP_ADDRESS, NGROK_TCP_PORT)) {
  93.     Serial.println("--> FALHA AO CONECTAR (UPLOAD).");
  94.     esp_camera_fb_return(fb);
  95.     return;
  96.   }
  97.  
  98.   Serial.println("Conectado. Enviando foto (RAW)...");
  99.  
  100.   // --- CABEÇALHOS PARA UPLOAD DIRETO (RAW) ---
  101.   client.print(String("POST ") + UPLOAD_PATH + " HTTP/1.1\r\n");
  102.   client.print(String("Host: ") + NGROK_TCP_ADDRESS + "\r\n");
  103.   client.print(String("Content-Length: ") + String(fb->len) + "\r\n");
  104.   client.print("Content-Type: image/jpeg\r\n");
  105.   client.print("Connection: close\r\n\r\n");
  106.  
  107.   // --- ENVIO EM BLOCOS PARA MÁXIMA ROBUSTEZ ---
  108.   delay(100); // Pequena pausa para o modem processar os cabeçalhos
  109.  
  110.   size_t image_len = fb->len;
  111.   size_t total_sent = 0;
  112.   uint8_t *fb_buffer = fb->buf;
  113.   bool upload_failed = false;
  114.  
  115.   while(total_sent < image_len) {
  116.     size_t chunk_size = 256; // Envia em blocos pequenos
  117.     if(total_sent + chunk_size > image_len) {
  118.       chunk_size = image_len - total_sent;
  119.     }
  120.     size_t sent = client.write(fb_buffer + total_sent, chunk_size);
  121.    
  122.     if(sent != chunk_size) {
  123.         Serial.printf("\n!!! FALHA NO ENVIO DO BLOCO !!!\n");
  124.         upload_failed = true;
  125.         break;
  126.     }
  127.     total_sent += sent;
  128.     Serial.printf("Enviado: %d / %d bytes\r \n", total_sent, image_len);
  129.   }
  130.  
  131.   Serial.println(); // Nova linha após o progresso
  132.   esp_camera_fb_return(fb);
  133.  
  134.   if (upload_failed) {
  135.     client.stop();
  136.     return;
  137.   }
  138.  
  139.   Serial.printf("Imagem enviada com sucesso (%d bytes)\n", total_sent);
  140.  
  141.   Serial.println("Aguardando resposta...");
  142.   unsigned long timeout = millis();
  143.   while (client.connected() && millis() - timeout < 10000L) {
  144.     while (client.available()) {
  145.       char c = client.read();
  146.       Serial.print(c);
  147.       timeout = millis();
  148.     }
  149.   }
  150.   Serial.println("\n--- Fim da Requisição de Upload ---");
  151.   client.stop();
  152. }
  153.  
  154. void getCommandFromServer() {
  155.   // Esta função permanece a mesma
  156.   Serial.println("Verificando comando...");
  157.   if (!client.connect(NGROK_TCP_ADDRESS, NGROK_TCP_PORT)) {
  158.     Serial.println("--> FALHA AO CONECTAR (COMANDO).");
  159.     return;
  160.   }
  161.   client.print(String("GET ") + COMMAND_PATH + " HTTP/1.1\r\n");
  162.   client.print(String("Host: ") + NGROK_TCP_ADDRESS + "\r\n");
  163.   client.print("Connection: close\r\n\r\n");
  164.   unsigned long timeout = millis();
  165.   while (!client.available() && millis() - timeout < 5000L) { delay(10); }
  166.   String response_body = "";
  167.   if(client.available()){
  168.     if(client.find("\r\n\r\n")){ response_body = client.readString(); }
  169.   }
  170.   client.stop();
  171.   response_body.trim();
  172.   Serial.print("Comando recebido: \""); Serial.print(response_body); Serial.println("\"");
  173.   if (response_body.equalsIgnoreCase("FLASH_ON")) {
  174.     digitalWrite(FLASH_PIN, HIGH);
  175.   } else if (response_body.equalsIgnoreCase("FLASH_OFF")) {
  176.     digitalWrite(FLASH_PIN, LOW);
  177.   }
  178.   Serial.println("--- Fim da Requisição de Comando ---");
  179. }
  180.  
  181. void setup() {
  182.   Serial.begin(115200);
  183.   delay(1000);
  184.   Serial.println("\n\nESP32-CAM (Upload RAW - Blocos)");
  185.   pinMode(FLASH_PIN, OUTPUT);
  186.   digitalWrite(FLASH_PIN, LOW);
  187.   if (!setupCamera()) { ESP.restart(); }
  188.   Serial.println("Câmera inicializada.");
  189.   SerialSIM.begin(9600, SERIAL_8N1, SIM800_RX_PIN, SIM800_TX_PIN);
  190.   delay(3000);
  191.   Serial.println("Inicializando modem...");
  192.   if (!modem.init()) { ESP.restart(); }
  193.   Serial.print("Modem: "); Serial.println(modem.getModemInfo());
  194. }
  195.  
  196. void loop() {
  197.   if (!modem.isGprsConnected()) {
  198.     Serial.println("GPRS Desconectado. Conectando...");
  199.     if (!modem.gprsConnect(apn, user, pass)) {
  200.       delay(10000);
  201.       return;
  202.     }
  203.     Serial.println("GPRS Conectado!");
  204.   }
  205.   unsigned long currentMillis = millis();
  206.   if (currentMillis - previousMillis_upload >= uploadInterval) {
  207.     previousMillis_upload = currentMillis;
  208.     uploadPhoto();
  209.   }
  210.   if (currentMillis - previousMillis_command >= commandInterval) {
  211.     previousMillis_command = currentMillis;
  212.     getCommandFromServer();
  213.   }
  214.   delay(100);
  215. }
  216.  
Add Comment
Please, Sign In to add comment