Hi ChrisWard, thanks for your reply.
The HC-05 is connected to an NXP S32K144EVB microcontroller board.
It is programmed in C using the S32 Design Studio IDE.
This is not much more complicated than an Arduino sketch — it’s just using NXP’s HAL drivers.
Here is the current code running on the S32K144.
It is already able to send the string "SPEED:2;DOOR:CLOSED;TIRE:OK\n"
to the Serial Bluetooth Terminal app successfully.
I haven’t tested the "BRAKE"
command reception yet, but the TX part works fine.
#include "Cpu.h"
#include "pin_mux.h"
#include "clockMan1.h"
#include "lpuart1.h"
#include <string.h>
#include <stdbool.h>
#define TIMEOUT_MS 100U
#define BRAKE_LED_PORT PTD
#define BRAKE_LED_PIN 0U
#define LED_ACTIVE_LOW 1
#define UART_INSTANCE INST_LPUART1
#define TX_INTERVAL_MS 500U
#define BRAKE_ON_TIME_MS 5000U
static uint8_t rxByte;
static char rxBuffer[32];
static uint8_t rxIndex = 0;
volatile uint32_t brake_led_timer_ms = 0;
volatile uint32_t ms_counter = 0;
// ==================== LED control ====================
static void set_brake_led(bool on)
{
if (LED_ACTIVE_LOW) {
PINS_DRV_WritePin(BRAKE_LED_PORT, BRAKE_LED_PIN, on ? 0 : 1);
} else {
PINS_DRV_WritePin(BRAKE_LED_PORT, BRAKE_LED_PIN, on ? 1 : 0);
}
}
// ==================== UART RX Callback ====================
void rxCallback(void *driverState, uart_event_t event, void *userData)
{
(void)driverState;
(void)userData;
if (event == UART_EVENT_RX_FULL)
{
if (rxIndex < sizeof(rxBuffer) - 1) {
rxBuffer[rxIndex++] = (char)rxByte;
rxBuffer[rxIndex] = '\0';
} else {
// If full, reset
rxIndex = 0;
rxBuffer[rxIndex] = '\0';
}
if (rxIndex >= 5) {
if (memcmp(rxBuffer, "BRAKE", 5) == 0) {
brake_led_timer_ms = BRAKE_ON_TIME_MS;
set_brake_led(true);
const char *ack = "BRAKE_ACK\n";
LPUART_DRV_SendDataBlocking(UART_INSTANCE,
(const uint8_t *)ack,
strlen(ack),
TIMEOUT_MS);
// Reset buffer
rxIndex = 0;
rxBuffer[0] = '\0';
}
}
LPUART_DRV_SetRxBuffer(UART_INSTANCE, &rxByte, 1U);
}
}
// ==================== Main ====================
int main(void)
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/
// ===== Clock init =====
CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
// ===== Pin init =====
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
// ===== LPUART1 init (baud in lpuart1_InitConfig0 = 9600) =====
LPUART_DRV_Init(UART_INSTANCE, &lpuart1_State, &lpuart1_InitConfig0);
// Callback
LPUART_DRV_InstallRxCallback(UART_INSTANCE, rxCallback, NULL);
LPUART_DRV_ReceiveData(UART_INSTANCE, &rxByte, 1U);
// ===== LED blue =====
PINS_DRV_SetPinDirection(BRAKE_LED_PORT, BRAKE_LED_PIN, GPIO_OUTPUT_DIRECTION);
set_brake_led(false);
// ===== Main loop =====
while (1)
{
// Delay 1ms (FIRC 48 MHz)
for (volatile uint32_t i = 0; i < 48000; i++);
ms_counter++;
// LED BRAKE
if (brake_led_timer_ms > 0) {
brake_led_timer_ms--;
if (brake_led_timer_ms == 0) {
set_brake_led(false);
}
}
// TX_INTERVAL_MS
if (ms_counter >= TX_INTERVAL_MS) {
ms_counter = 0;
const char *msg = "SPEED:2;DOOR:CLOSED;TIRE:OK\n";
LPUART_DRV_SendDataBlocking(UART_INSTANCE,
(const uint8_t *)msg,
strlen(msg),
TIMEOUT_MS);
}
}
}
If you think the microcontroller code might be related to the MIT App Inventor connection issue (Error 507), please let me know.
I’m happy to share any further details or make adjustments for testing.