#!/bin/bash # Build and Deploy Script for ESP32-C5-EVB # Usage: ./build_and_deploy.sh [--flash-only] set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SKETCH_DIR="$SCRIPT_DIR/esp32_arduino" SKETCH_FILE="$SKETCH_DIR/esp32_arduino.ino" DEVICE_PORT="${DEVICE_PORT:-/dev/ttyACM0}" DEVICE_BAUD="${DEVICE_BAUD:-921600}" DEVICE_IP="${DEVICE_IP:-192.168.0.241}" # ─ Colors ───────────────────────────────────────────────────────────────────── RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # ─ Functions ────────────────────────────────────────────────────────────────── log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } # ─ Check Prerequisites ──────────────────────────────────────────────────────── log_info "Checking prerequisites..." if [ ! -f "$SKETCH_FILE" ]; then log_error "Sketch not found: $SKETCH_FILE" exit 1 fi if ! command -v esptool.py &> /dev/null; then log_error "esptool.py not found. Install: pip install esptool" exit 1 fi if [ ! -e "$DEVICE_PORT" ]; then log_error "Device port not found: $DEVICE_PORT" log_info "Available ports:" ls -la /dev/ttyUSB* /dev/ttyACM* 2>/dev/null || echo " (none found)" exit 1 fi log_success "Prerequisites OK" # ─ Skip compilation if --flash-only ─────────────────────────────────────────── if [ "$1" == "--flash-only" ]; then log_warn "Skipping compilation (--flash-only)" LATEST_BUILD=$(find /tmp -name "arduino_build_*" -type d 2>/dev/null | sort -V | tail -1) else # ─ Build ────────────────────────────────────────────────────────────────── log_info "Compiling sketch (this may take 30-60 seconds)..." log_warn "Please click 'Sketch → Verify' in Arduino IDE or use: arduino --verify --board esp32:esp32:esp32c5 '$SKETCH_FILE'" log_info "" log_info "Waiting for build to complete..." # Find the latest build directory created BUILD_BEFORE=$(find /tmp -name "arduino_build_*" -type d 2>/dev/null | sort -V | tail -1) START_TIME=$(date +%s) # Wait for new build or changes to existing build (max 120 seconds) while true; do LATEST_BUILD=$(find /tmp -name "arduino_build_*" -type d 2>/dev/null | sort -V | tail -1) if [ -n "$LATEST_BUILD" ] && [ -f "$LATEST_BUILD/esp32_arduino.ino.merged.bin" ]; then MODIFIED=$(stat -f%m "$LATEST_BUILD/esp32_arduino.ino.merged.bin" 2>/dev/null || stat -c%Y "$LATEST_BUILD/esp32_arduino.ino.merged.bin") CURRENT_TIME=$(date +%s) if [ $((CURRENT_TIME - MODIFIED)) -lt 10 ]; then log_success "Build found and appears recent" break fi fi CURRENT_TIME=$(date +%s) if [ $((CURRENT_TIME - START_TIME)) -gt 120 ]; then log_error "Timeout waiting for build. Please compile in Arduino IDE manually." exit 1 fi sleep 2 done fi # ─ Verify binary exists ─────────────────────────────────────────────────────── if [ -z "$LATEST_BUILD" ] || [ ! -f "$LATEST_BUILD/esp32_arduino.ino.merged.bin" ]; then log_error "Compiled binary not found" exit 1 fi BINARY_PATH="$LATEST_BUILD/esp32_arduino.ino.merged.bin" BINARY_SIZE=$(ls -lh "$BINARY_PATH" | awk '{print $5}') log_success "Binary found: $BINARY_PATH ($BINARY_SIZE)" # ─ Flash ────────────────────────────────────────────────────────────────────── log_info "Erasing flash on $DEVICE_PORT..." esptool.py --chip esp32c5 --port "$DEVICE_PORT" --baud "$DEVICE_BAUD" erase-flash log_info "Flashing firmware..." cd "$LATEST_BUILD" esptool.py --chip esp32c5 --port "$DEVICE_PORT" --baud "$DEVICE_BAUD" write-flash 0x0 esp32_arduino.ino.merged.bin log_success "Firmware flashed successfully!" # ─ Verify ───────────────────────────────────────────────────────────────────── log_info "Waiting for device to boot (10 seconds)..." sleep 10 log_info "Testing connectivity to $DEVICE_IP..." if curl -s -f -u Ske087:Matei@123 "http://$DEVICE_IP/relay/status?relay=1" &> /dev/null; then log_success "Device is online and responding!" # Test all endpoints log_info "Testing API endpoints..." echo -n " Relay 1: " curl -s -u Ske087:Matei@123 "http://$DEVICE_IP/relay/status?relay=1" echo -n " Relay 2: " curl -s -u Ske087:Matei@123 "http://$DEVICE_IP/relay/status?relay=2" echo -n " Input 1: " curl -s -u Ske087:Matei@123 "http://$DEVICE_IP/input/status?input=1" echo -n " Input 2: " curl -s -u Ske087:Matei@123 "http://$DEVICE_IP/input/status?input=2" log_success "All tests passed!" else log_warn "Device not yet responding. This is normal if WiFi connection is in progress." log_info "Check with: curl -u Ske087:Matei@123 http://$DEVICE_IP/relay/status?relay=1" fi log_success "Deployment complete!"