Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TINY_GSM_DEBUG Serial
- #define TINY_GSM_MODEM_SIM800
- #include <TinyGsmClient.h>
- #include "esp_camera.h"
- #define FLASH_PIN 4
- #define CAMERA_MODEL_AI_THINKER
- // --- Pinos da Câmera ---
- #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
- // --- Pinos para SIM800L ---
- #define SIM800_RX_PIN 15
- #define SIM800_TX_PIN 13
- HardwareSerial SerialSIM(2);
- // --- Configurações do Servidor ---
- const char NGROK_TCP_ADDRESS[] = "1.tcp.sa.ngrok.io"; // <- MUDE AQUI
- const int NGROK_TCP_PORT = 21302; // <- MUDE AQUI
- // --- Caminhos (Endpoints) do Servidor ---
- const char* UPLOAD_PATH = "/raw_upload"; // <<< MUDANÇA IMPORTANTE
- const char* COMMAND_PATH = "/command";
- // --- Configurações do APN ---
- char apn[] = "claro.com.br";
- char user[] = "claro";
- char pass[] = "claro";
- TinyGsm modem(SerialSIM);
- TinyGsmClient client(modem);
- unsigned long previousMillis_upload = 0;
- unsigned long previousMillis_command = 0;
- long uploadInterval = 8000;
- long commandInterval = 8000;
- bool setupCamera(){
- camera_config_t config;
- 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_sccb_sda = SIOD_GPIO_NUM;
- config.pin_sccb_scl = SIOC_GPIO_NUM;
- config.pin_pwdn = PWDN_GPIO_NUM;
- config.pin_reset = RESET_GPIO_NUM;
- config.xclk_freq_hz = 20000000;
- config.pixel_format = PIXFORMAT_JPEG;
- config.frame_size = FRAMESIZE_QVGA;
- config.jpeg_quality = 12;
- config.fb_count = 1;
- esp_err_t err = esp_camera_init(&config);
- if (err != ESP_OK) { return false; }
- return true;
- }
- void uploadPhoto() {
- Serial.println("Capturando foto...");
- camera_fb_t *fb = esp_camera_fb_get();
- if (!fb) {
- Serial.println("Falha ao capturar foto.");
- return;
- }
- if (!client.connect(NGROK_TCP_ADDRESS, NGROK_TCP_PORT)) {
- Serial.println("--> FALHA AO CONECTAR (UPLOAD).");
- esp_camera_fb_return(fb);
- return;
- }
- Serial.println("Conectado. Enviando foto (RAW)...");
- // --- CABEÇALHOS PARA UPLOAD DIRETO (RAW) ---
- client.print(String("POST ") + UPLOAD_PATH + " HTTP/1.1\r\n");
- client.print(String("Host: ") + NGROK_TCP_ADDRESS + "\r\n");
- client.print(String("Content-Length: ") + String(fb->len) + "\r\n");
- client.print("Content-Type: image/jpeg\r\n");
- client.print("Connection: close\r\n\r\n");
- // --- ENVIO EM BLOCOS PARA MÁXIMA ROBUSTEZ ---
- delay(100); // Pequena pausa para o modem processar os cabeçalhos
- size_t image_len = fb->len;
- size_t total_sent = 0;
- uint8_t *fb_buffer = fb->buf;
- bool upload_failed = false;
- while(total_sent < image_len) {
- size_t chunk_size = 256; // Envia em blocos pequenos
- if(total_sent + chunk_size > image_len) {
- chunk_size = image_len - total_sent;
- }
- size_t sent = client.write(fb_buffer + total_sent, chunk_size);
- if(sent != chunk_size) {
- Serial.printf("\n!!! FALHA NO ENVIO DO BLOCO !!!\n");
- upload_failed = true;
- break;
- }
- total_sent += sent;
- Serial.printf("Enviado: %d / %d bytes\r \n", total_sent, image_len);
- }
- Serial.println(); // Nova linha após o progresso
- esp_camera_fb_return(fb);
- if (upload_failed) {
- client.stop();
- return;
- }
- Serial.printf("Imagem enviada com sucesso (%d bytes)\n", total_sent);
- Serial.println("Aguardando resposta...");
- unsigned long timeout = millis();
- while (client.connected() && millis() - timeout < 10000L) {
- while (client.available()) {
- char c = client.read();
- Serial.print(c);
- timeout = millis();
- }
- }
- Serial.println("\n--- Fim da Requisição de Upload ---");
- client.stop();
- }
- void getCommandFromServer() {
- // Esta função permanece a mesma
- Serial.println("Verificando comando...");
- if (!client.connect(NGROK_TCP_ADDRESS, NGROK_TCP_PORT)) {
- Serial.println("--> FALHA AO CONECTAR (COMANDO).");
- return;
- }
- client.print(String("GET ") + COMMAND_PATH + " HTTP/1.1\r\n");
- client.print(String("Host: ") + NGROK_TCP_ADDRESS + "\r\n");
- client.print("Connection: close\r\n\r\n");
- unsigned long timeout = millis();
- while (!client.available() && millis() - timeout < 5000L) { delay(10); }
- String response_body = "";
- if(client.available()){
- if(client.find("\r\n\r\n")){ response_body = client.readString(); }
- }
- client.stop();
- response_body.trim();
- Serial.print("Comando recebido: \""); Serial.print(response_body); Serial.println("\"");
- if (response_body.equalsIgnoreCase("FLASH_ON")) {
- digitalWrite(FLASH_PIN, HIGH);
- } else if (response_body.equalsIgnoreCase("FLASH_OFF")) {
- digitalWrite(FLASH_PIN, LOW);
- }
- Serial.println("--- Fim da Requisição de Comando ---");
- }
- void setup() {
- Serial.begin(115200);
- delay(1000);
- Serial.println("\n\nESP32-CAM (Upload RAW - Blocos)");
- pinMode(FLASH_PIN, OUTPUT);
- digitalWrite(FLASH_PIN, LOW);
- if (!setupCamera()) { ESP.restart(); }
- Serial.println("Câmera inicializada.");
- SerialSIM.begin(9600, SERIAL_8N1, SIM800_RX_PIN, SIM800_TX_PIN);
- delay(3000);
- Serial.println("Inicializando modem...");
- if (!modem.init()) { ESP.restart(); }
- Serial.print("Modem: "); Serial.println(modem.getModemInfo());
- }
- void loop() {
- if (!modem.isGprsConnected()) {
- Serial.println("GPRS Desconectado. Conectando...");
- if (!modem.gprsConnect(apn, user, pass)) {
- delay(10000);
- return;
- }
- Serial.println("GPRS Conectado!");
- }
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis_upload >= uploadInterval) {
- previousMillis_upload = currentMillis;
- uploadPhoto();
- }
- if (currentMillis - previousMillis_command >= commandInterval) {
- previousMillis_command = currentMillis;
- getCommandFromServer();
- }
- delay(100);
- }
Add Comment
Please, Sign In to add comment