-
Notifications
You must be signed in to change notification settings - Fork 0
DynamicUploading.md
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!
-
Purpose: Upload new sketches to the Arduino, replacing the current one, via
&sketch_name\ncommands. -
Key Components:
-
Arduino Side:
Bangclass sendsBANG("&sketch_name\n")over Serial-USB. -
Host Side:
arduino_exec.pyprocesses&commands, usingarduino-clito compile and upload sketches.
-
Arduino Side:
- Benefit: Unlimited app size by swapping subsystems (each ≤ 32K flash), perfect for complex projects.
-
Arduino Sends Command:
- Sketch uses
BANG("&blink\n")to send&blink\nto the host.
BANG("&blink\n"); // Sends &blink\n
- Sketch uses
-
Host Processes Command:
-
arduino_exec.pydetects&and callscompile_and_upload("blink"). - Runs
arduino-clito compileblink.inoand upload it to the Arduino.
if cmd_id == '&': result = compile_and_upload(command) # Uploads sketch
-
-
Arduino Restarts:
- The new sketch (e.g.,
blink.ino) replaces the current one, restarting the Arduino. - If
blink.inoincludes Bang, it can send more commands (e.g.,BANG("&cmd\n")).
- The new sketch (e.g.,
-
Unlimited Apps:
- Each sketch is ≤ 32K flash, but you can chain uploads (e.g.,
blink.ino→cmd.ino→music.ino), making the app size limitless.
- Each sketch is ≤ 32K flash, but you can chain uploads (e.g.,
-
Prerequisites:
-
Bang installed,
arduino_exec.pyrunning withsudo(see Installation). -
arduino-cliinstalled 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 toarduino_exec.py.
-
-
Run Agent:
sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 38400
-
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.inosendsBANG("&blink\n"). -
blink.inosendsBANG("&cmd\n"). -
cmd.inosendsBANG("&music\n"), and so on.
-
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");
-
Sketch Location: Ensure sketch files (e.g.,
blink.ino) are wherearduino_exec.pyexpects 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.
-
Upload Fails: Verify
arduino-clisetup, sketch file path, and serial port. -
No Restart: Ensure
arduino_exec.pyhassudoand the correct port. -
Macro Issues: Use
BANG("@list_macros")to check upload macros. - See Troubleshooting for fixes.
- Explore Examples for sketches like
upload.ino. - Check Python Agent for
arduino_exec.pydetails. - See FAQ for upload questions.