State machine and lock
What this example shows
Walking the receiver through all of its states: a hardware reset into BOOTLOADER, the automatic move to RUN, a switch to CONFIGURATION, a deliberate return to BOOTLOADER that locks the receiver there, and finally releasing the lock so it falls through to RUN again. The sketch asks the receiver which state it is in at each step.
The example code
This is 5_StateMachineAndLock.ino from the pinned library, without the file's
header comment. The sketch body is exactly as shipped. It uses the RESET pin on
IO21, which this board wires to the receiver.
#include "ADSB-Receiver-SOLDERED.h"
// Create an instance of the ADS-B object
ADSBReceiver adsb;
void setup()
{
Serial.begin(115200);
adsb.begin(115200);
Serial.println("Hardware-resetting the module (RESET pin)...");
if (adsb.hardwareReset())
{
// BOOTLOADER holds for ~3s at the fixed 115200bps, then the module
// auto-transitions to RUN (unless locked). Wait it out before talking to it.
delay(3200);
}
else
{
Serial.println("No resetPin configured - skipping hardware reset, module keeps running.");
}
Serial.println("RUN -> CONFIGURATION");
if (!adsb.enterConfiguration())
{
Serial.println("Failed to enter CONFIGURATION state.");
return;
}
// Ask the module to confirm which state it is actually in.
uint8_t state;
if (adsb.getConfigState(state))
{
Serial.print("AT+CONFIG? = ");
Serial.println(state);
}
Serial.println("CONFIGURATION -> BOOTLOADER (this also sets AT+LOCK=1)");
if (!adsb.rebootToBootloader())
{
Serial.println("Failed to reboot to BOOTLOADER state.");
return;
}
// BOOTLOADER state always runs at the fixed 115200bps.
delay(200);
// Confirm the module really is in BOOTLOADER now.
bool inBootloader = false;
if (adsb.isBootloader(inBootloader))
{
Serial.print("AT+BOOT? = ");
Serial.println(inBootloader ? "1 (BOOTLOADER)" : "0 (CONFIGURATION)");
}
// The lock should be set, since rebootToBootloader() sets it automatically.
bool locked = false;
if (adsb.getLock(locked))
{
Serial.print("AT+LOCK? = ");
Serial.println(locked ? "1 (locked)" : "0 (unlocked)");
}
Serial.println("Releasing lock: module will fall through to RUN state");
adsb.setLock(false);
// Module leaves BOOTLOADER and enters RUN at its configured run baud (115200 by default).
delay(200);
Serial.println("Done. Module should now be broadcasting in RUN state again.");
}
void loop()
{
adsb.poll();
}
Expected result
Open Tools → Serial Monitor, set the baud rate to 115200 and reset the
board. The sketch runs its sequence once in setup(), which takes a few
seconds because of the 3.2 second wait after the hardware reset. The lines
appear in this order: the reset message, RUN -> CONFIGURATION, the
AT+CONFIG? = line, CONFIGURATION -> BOOTLOADER (this also sets AT+LOCK=1),
the AT+BOOT? = line, the AT+LOCK? = line, Releasing lock: module will fall through to RUN state and Done. Module should now be broadcasting in RUN state again. The three lines that report a value come from the receiver itself, and
the library describes the first as 1 for CONFIGURATION at 115200 and 2 for
CONFIGURATION at the set baud rate.
Hardware-resetting the module (RESET pin)...
RUN -> CONFIGURATION
AT+CONFIG? = 1
CONFIGURATION -> BOOTLOADER (this also sets AT+LOCK=1)
AT+BOOT? = 1 (BOOTLOADER)
AT+LOCK? = 1 (locked)
Releasing lock: module will fall through to RUN state
Done. Module should now be broadcasting in RUN state again.
Functions used
ADSBReceiver()returns NoneCreates the receiver object. With no arguments it uses the library's board defaults: hardware UART 1, IO17 for the receiver's TX line, IO18 for its RX line and IO21 for its RESET line, which is how this board is wired.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | uartNum | ESP32 hardware UART to use. Optional, default 1. Avoid 0, which is usually the USB console. |
int8_t | rxPin | ESP32 GPIO wired to the module's TX pin. Optional, default 17. |
int8_t | txPin | ESP32 GPIO wired to the module's RX pin. Optional, default 18. |
int8_t | resetPin | ESP32 GPIO wired to the module's RESET pin. Optional, default 21. Pass -1 if it is not connected. |
begin(uint32_t initialBaud, size_t rxBufferSize)returns voidOpens the UART link to the receiver. Call it once in setup().
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint32_t | initialBaud | Baud rate to start at. Optional, default 115200. |
size_t | rxBufferSize | Size of the UART receive buffer in bytes. Optional, default 4096. |
hardwareReset(uint16_t pulseMs, bool activeLow, bool resyncTo115200)returns boolResets the receiver by pulling the RESET pin low for pulseMs milliseconds and then releasing it, so you do not need a button or jumper. The receiver restarts into BOOTLOADER state at the fixed 115200 baud for about 3 seconds, then moves to RUN unless it is locked. Bytes left over from before the reset are discarded.
Returnsfalse if no reset pin was given to the constructor, in which case nothing happens; true otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | pulseMs | How long to hold the receiver in reset. Optional, default 20. |
bool | activeLow | true if the receiver resets while RESET is low. Optional, default true. |
bool | resyncTo115200 | Also switch the local UART to 115200, the baud rate the receiver uses after a reset. Optional, default true. |
enterConfiguration()returns boolMoves the receiver from RUN to CONFIGURATION state. It sends AT+CONFIG=1, switches the local UART to 115200 and confirms the new state with AT+CONFIG?.
Returnstrue if the receiver reports being in CONFIGURATION state, false otherwise.
getConfigState(uint8_t &state)returns boolAsks the receiver which state it is in with AT+CONFIG?.
Returnstrue if the receiver answered, false otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t & | state | Receives 0 for RUN state, 1 for CONFIGURATION at 115200 baud and 2 for CONFIGURATION at the baud rate you set. |
rebootToBootloader()returns boolReboots the receiver into BOOTLOADER state with AT+REBOOT_BOOTLOADER. It also sets the lock, so the receiver stays in BOOTLOADER until you release it.
Returnstrue if the receiver answered AT+OK or announced the BOOTLOADER, false otherwise.
isBootloader(bool &inBootloader)returns boolAsks the receiver whether it is in BOOTLOADER state with AT+BOOT?.
Returnstrue if the receiver answered, false otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bool & | inBootloader | Receives true when the receiver is in BOOTLOADER state. |
getLock(bool &locked)returns boolAsks the receiver whether the lock is set with AT+LOCK?.
Returnstrue if the receiver answered, false otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bool & | locked | Receives true when the lock is set. |
setLock(bool locked)returns boolSets or releases the lock with AT+LOCK=1 or AT+LOCK=0. While the lock is set, the receiver stays in BOOTLOADER state; releasing it lets the receiver move on to RUN.
Returnstrue if the receiver answered AT+OK, false otherwise.
Parameters
| Type | Name | Description |
|---|---|---|
bool | locked | true to set the lock, false to release it. |
poll()returns voidReads every byte that has arrived, parses the complete lines and fires the registered callbacks. It does not block, so call it on every pass of loop().
ReturnsNothing.
Putting it together
The receiver has three states you work with: RUN (decoding and reporting
aircraft), CONFIGURATION (reading and writing settings) and BOOTLOADER (which
runs at a fixed 115200 baud). A hardware reset always starts in BOOTLOADER and
moves on to RUN after about 3 seconds. The lock is what stops that move:
rebootToBootloader() sets it, and setLock(false) releases it so the
receiver continues into RUN.
Do not leave the lock set. A locked receiver stays in BOOTLOADER and reports
no aircraft until you release it with setLock(false).