setBufferSizes(512,512) en el cliente de descarga causaba que BearSSL rechazase records TLS de datos > 512 bytes durante la descarga del binario de ~400KB. Cambiado a setBufferSizes(4096,512): - 4096 bytes entrada: suficiente para records TLS de datos - 512 bytes salida: suficiente para peticiones HTTP - El scope block del cliente de version.txt libera ~10KB antes, dejando heap suficiente para este cliente mas grande. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
259 lines
7.5 KiB
C++
259 lines
7.5 KiB
C++
#include <Servo.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiClientSecureBearSSL.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
#include <ESP8266httpUpdate.h>
|
|
|
|
// ================================================================
|
|
// OTA CONFIG
|
|
// ================================================================
|
|
#define FW_VERSION 11
|
|
#define GITEA_HOST "https://git.nacho.myds.me"
|
|
#define GITEA_OWNER "Natxo"
|
|
#define GITEA_REPO "Arduino-Car"
|
|
#define GITEA_BRANCH "main"
|
|
#define GITEA_TOKEN "d3b0b32eb0311c9b4ef569f22c8392c373ff3f9f"
|
|
// WiFi — mismas credenciales que usa el ESP32-CAM
|
|
#define WIFI_SSID "Natxo"
|
|
#define WIFI_PASS "Suprak1llm1ndS23S"
|
|
// ================================================================
|
|
|
|
// ==== Pines motores ====
|
|
const int ENA = D1;
|
|
const int ENB = D5;
|
|
const int IN1 = D6;
|
|
const int IN2 = D7;
|
|
const int IN3 = D2;
|
|
const int IN4 = D0;
|
|
|
|
// ==== Pines servos ====
|
|
const int SERVO_X_PIN = D3;
|
|
const int SERVO_Y_PIN = D4;
|
|
|
|
// ==== Pines buzzer ====
|
|
const int buzzerPin = D8;
|
|
|
|
Servo servoX;
|
|
Servo servoY;
|
|
|
|
int currentSpeed = 150;
|
|
int posX = 90;
|
|
int posY = 90;
|
|
|
|
// ── OTA: comprueba si hay firmware nuevo y actualiza si hace falta ──
|
|
static void checkOTA() {
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
|
|
int tries = 0;
|
|
while (WiFi.status() != WL_CONNECTED && tries < 20) {
|
|
delay(500);
|
|
tries++;
|
|
}
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
WiFi.disconnect();
|
|
WiFi.mode(WIFI_OFF);
|
|
return; // sin WiFi, arranque normal
|
|
}
|
|
|
|
String base = String(GITEA_HOST) + "/" + GITEA_OWNER + "/" + GITEA_REPO
|
|
+ "/raw/branch/" + GITEA_BRANCH + "/firmware/esp8266/";
|
|
String token = "?token=" + String(GITEA_TOKEN);
|
|
|
|
// ── Bloque de scope: libera el cliente BearSSL antes de la descarga ──
|
|
// BearSSL ocupa ~20KB de heap. Si los dos clientes coexisten el heap
|
|
// se agota y ESPhttpUpdate falla silenciosamente.
|
|
int remoteVer = 0;
|
|
{
|
|
BearSSL::WiFiClientSecure client;
|
|
client.setInsecure();
|
|
client.setBufferSizes(512, 512); // buffers mínimos — suficiente para la respuesta
|
|
HTTPClient http;
|
|
http.begin(client, base + "version.txt" + token);
|
|
http.setTimeout(10000);
|
|
int code = http.GET();
|
|
if (code == HTTP_CODE_OK) {
|
|
String body = http.getString();
|
|
body.trim();
|
|
remoteVer = body.toInt();
|
|
}
|
|
http.end();
|
|
} // ← client y http se destruyen aquí, ~20KB liberados
|
|
|
|
if (remoteVer > FW_VERSION) {
|
|
// 4 pitidos = ESP8266 actualizando
|
|
pinMode(buzzerPin, OUTPUT);
|
|
for (int i = 0; i < 4; i++) {
|
|
digitalWrite(buzzerPin, HIGH);
|
|
delay(150);
|
|
digitalWrite(buzzerPin, LOW);
|
|
delay(150);
|
|
}
|
|
delay(300);
|
|
|
|
// Fuerza pines de motor a LOW antes del flash para minimizar glitch de GPIO
|
|
analogWrite(ENA, 0); analogWrite(ENB, 0);
|
|
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
|
|
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
|
|
|
|
BearSSL::WiFiClientSecure updateClient;
|
|
updateClient.setInsecure();
|
|
// 4096 bytes de entrada: necesario para records TLS de datos grandes.
|
|
// 512 bytes de salida: suficiente para peticiones HTTP.
|
|
// El scope block anterior liberó ~10KB del primer cliente — hay heap.
|
|
updateClient.setBufferSizes(4096, 512);
|
|
ESPhttpUpdate.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
|
ESPhttpUpdate.rebootOnUpdate(true);
|
|
ESPhttpUpdate.update(updateClient, base + "firmware.bin" + token);
|
|
// Si llega aquí, el update falló — continúa arranque normal
|
|
}
|
|
|
|
WiFi.disconnect();
|
|
WiFi.mode(WIFI_OFF); // apaga radio para ahorrar energía
|
|
}
|
|
|
|
bool espReady = false;
|
|
|
|
// ── Handshake con ESP32-CAM ──────────────────────────────────────
|
|
// Descarta basura del bootloader ROM (115200 baud → bytes aleatorios),
|
|
// luego espera exactamente "GO\n" y responde "OK\n".
|
|
void waitForReady() {
|
|
// Descarta todo lo que haya en el buffer (logs de arranque del ESP32)
|
|
delay(300);
|
|
while (Serial.available()) Serial.read();
|
|
|
|
// Espera "GO\n" del ESP32-CAM (máx 90s por si está haciendo OTA)
|
|
unsigned long timeout = millis() + 90000UL;
|
|
String buf = "";
|
|
while (millis() < timeout) {
|
|
while (Serial.available()) {
|
|
char c = Serial.read();
|
|
if (c == '\n') {
|
|
buf.trim();
|
|
if (buf == "GO") {
|
|
Serial.print("OK\n");
|
|
delay(50);
|
|
while (Serial.available()) Serial.read(); // flush post-handshake
|
|
espReady = true;
|
|
// 2 pitidos: conexión entre placas establecida
|
|
for (int i = 0; i < 2; i++) {
|
|
digitalWrite(buzzerPin, HIGH); delay(100);
|
|
digitalWrite(buzzerPin, LOW); delay(100);
|
|
}
|
|
return;
|
|
}
|
|
buf = "";
|
|
} else if (c != '\r') {
|
|
if (buf.length() < 8) buf += c;
|
|
}
|
|
}
|
|
delay(10);
|
|
}
|
|
espReady = true; // timeout: arranca igual
|
|
}
|
|
|
|
void setup() {
|
|
// 1. Pines y parada inmediata
|
|
pinMode(ENA, OUTPUT); pinMode(ENB, OUTPUT);
|
|
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
|
|
pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
|
|
stopMotors();
|
|
|
|
// 2. Serial temprano (el ESP32 puede mandar el pitido de OTA)
|
|
Serial.begin(9600);
|
|
|
|
// 3. OTA via WiFi
|
|
checkOTA();
|
|
|
|
// 4. Resto de periféricos
|
|
analogWriteRange(255);
|
|
analogWriteFreq(1000);
|
|
servoX.attach(SERVO_X_PIN);
|
|
servoY.attach(SERVO_Y_PIN);
|
|
pinMode(buzzerPin, OUTPUT);
|
|
servoX.write(posX);
|
|
servoY.write(posY);
|
|
|
|
// 5. Esperar señal '!' del ESP32-CAM antes de procesar comandos
|
|
waitForReady();
|
|
}
|
|
|
|
void loop() {
|
|
if (Serial.available()) {
|
|
char c = Serial.read();
|
|
|
|
// Detecta re-arranque del ESP32-CAM: envía "GO\n" de nuevo
|
|
if (c == 'G') {
|
|
Serial.readStringUntil('\n'); // consume "O\n"
|
|
Serial.print("OK\n");
|
|
delay(50);
|
|
while (Serial.available()) Serial.read();
|
|
espReady = true;
|
|
return;
|
|
}
|
|
|
|
// Ignorar todo hasta completar el handshake
|
|
if (!espReady) return;
|
|
|
|
if (c == 'F') forward();
|
|
else if (c == 'B') backward();
|
|
else if (c == 'L') left();
|
|
else if (c == 'R') right();
|
|
else if (c == 'S') stopMotors();
|
|
|
|
else if (c == 'V') {
|
|
int val = Serial.parseInt();
|
|
if (val >= 0 && val <= 255) currentSpeed = val;
|
|
}
|
|
else if (c == 'X') {
|
|
int val = Serial.parseInt();
|
|
if (val >= 0 && val <= 180) { posX = val; servoX.write(posX); }
|
|
}
|
|
else if (c == 'Y') {
|
|
int val = Serial.parseInt();
|
|
if (val >= 0 && val <= 180) { posY = val; servoY.write(posY); }
|
|
}
|
|
else if (c == 'H') {
|
|
int val = Serial.parseInt();
|
|
digitalWrite(buzzerPin, val > 0 ? HIGH : LOW);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ==== Funciones de movimiento ====
|
|
void forward() {
|
|
analogWrite(ENA, currentSpeed);
|
|
analogWrite(ENB, currentSpeed);
|
|
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
|
|
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
|
|
}
|
|
|
|
void backward() {
|
|
analogWrite(ENA, currentSpeed);
|
|
analogWrite(ENB, currentSpeed);
|
|
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
|
|
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
|
|
}
|
|
|
|
void left() {
|
|
analogWrite(ENA, currentSpeed);
|
|
analogWrite(ENB, currentSpeed);
|
|
digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH);
|
|
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
|
|
}
|
|
|
|
void right() {
|
|
analogWrite(ENA, currentSpeed);
|
|
analogWrite(ENB, currentSpeed);
|
|
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
|
|
digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH);
|
|
}
|
|
|
|
void stopMotors() {
|
|
analogWrite(ENA, 0);
|
|
analogWrite(ENB, 0);
|
|
digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
|
|
digitalWrite(IN3, LOW); digitalWrite(IN4, LOW);
|
|
}
|