Skip to content

DynamicUploading.md

Trent M. Wyatt edited this page Apr 21, 2025 · 1 revision

Dynamic Uploading

The Bang library’s dynamic uploading feature is pure genius, letting your Arduino reprogram itself by uploading new sketches with a single BANG("&sketch_name\n")! This obliterates the 32K flash limit, enabling apps as big as your imagination—each sketch just needs to fit in 32K, and if it includes Bang, it can keep the cycle going with more &, !, or @ commands. Powered by arduino_exec.py and arduino-cli, this self-reprogramming trick makes your Arduino a shape-shifting beast. Let’s dive into how it works and unleash its power!

Overview

  • Purpose: Upload new sketches to the Arduino, replacing the current one, via &sketch_name\n commands.
  • Key Components:
    • Arduino Side: Bang class sends BANG("&sketch_name\n") over Serial-USB.
    • Host Side: arduino_exec.py processes & commands, using arduino-cli to compile and upload sketches.
  • Benefit: Unlimited app size by swapping subsystems (each ≤ 32K flash), perfect for complex projects.

How It Works

  1. Arduino Sends Command:

    • Sketch uses BANG("&blink\n") to send &blink\n to the host.
    BANG("&blink\n"); // Sends &blink\n
  2. Host Processes Command:

    • arduino_exec.py detects & and calls compile_and_upload("blink").
    • Runs arduino-cli to compile blink.ino and upload it to the Arduino.
    if cmd_id == '&':
        result = compile_and_upload(command)  # Uploads sketch
  3. Arduino Restarts:

    • The new sketch (e.g., blink.ino) replaces the current one, restarting the Arduino.
    • If blink.ino includes Bang, it can send more commands (e.g., BANG("&cmd\n")).
  4. Unlimited Apps:

    • Each sketch is ≤ 32K flash, but you can chain uploads (e.g., blink.inocmd.inomusic.ino), making the app size limitless.

Setup

  • Prerequisites:

    • Bang installed, arduino_exec.py running with sudo (see Installation).

    • arduino-cli installed and configured:

      arduino-cli config init
      arduino-cli core update-index
      arduino-cli core install arduino:avr
    • Sketch files (e.g., blink.ino) in a directory accessible to arduino_exec.py.

  • Run Agent:

    sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 38400

Example Usage

  • Sketch:

    #include <Bang.h>
    Bang bang(Serial);
    
    void setup() {
        Serial.begin(38400);
        while (!Serial) ;
    }
    
    void loop() {
        BANG("&blink\n"); // Uploads blink.ino
        delay(10000);
    }
  • Output: Arduino restarts with blink.ino, Serial Monitor shows new sketch output (e.g., Blink Example).

  • Chain Example:

    • upload.ino sends BANG("&blink\n").
    • blink.ino sends BANG("&cmd\n").
    • cmd.ino sends BANG("&music\n"), and so on.

Combining with Macros

Enhance uploads with @macro:

  • Define a macro to upload:

    BANG("@add_macro:next:blink\n"); // Defines @next to upload blink.ino
    BANG("@next"); // Sends &blink\n
  • List macros to verify:

    BANG("@list_macros"); // Shows "next": "blink\n"
  • Delete macro after use:

    BANG("@delete_macro:next");

Tips

  • Sketch Location: Ensure sketch files (e.g., blink.ino) are where arduino_exec.py expects them (e.g., same directory).
  • Test Small: Start with simple sketches to verify arduino-cli.
  • Include Bang: New sketches should include Bang for continuous uploads or commands.
  • Debugging: Check Serial Monitor for upload status or errors.

Troubleshooting

  • Upload Fails: Verify arduino-cli setup, sketch file path, and serial port.
  • No Restart: Ensure arduino_exec.py has sudo and the correct port.
  • Macro Issues: Use BANG("@list_macros") to check upload macros.
  • See Troubleshooting for fixes.

Next Steps

  • Explore Examples for sketches like upload.ino.
  • Check Python Agent for arduino_exec.py details.
  • See FAQ for upload questions.

Back to Home | Next: Security

Clone this wiki locally