Fab Academy 2026

Week 14: Interface and Application Programming

Building a mobile app in MIT App Inventor that connects to my ESP32 board through BLE.

Software

MIT App Inventor

Hardware

XIAO ESP32-C3 and a phone

Main Skills

App layout, block programming and BLE

Assignments

Group Assignment

Individual Assignment

Group Assignment

Group Assignment - Interface and Application Programming

In the group assignment we compared three tools to build interfaces: Tkinter, Qt Designer with PyQt6 and LabVIEW.

Tkinter comes with Python, so it needs no extra installation. It is easy to learn and good for small projects, but the look is outdated and it is not made for large applications.

Qt Designer lets you drag and drop the widgets and then connect them to Python code. It looks more modern than Tkinter, but the setup is longer. Also, the .ui file must be converted again after every visual change.

LabVIEW uses blocks and wires instead of text. It is very good for real-time plotting and for working with sensors and instruments, but it depends on the National Instruments ecosystem. Its free educational license also needs a school account.

Tkinter and Qt Designer are made for desktop applications. Since I needed an app on my phone to connect through BLE, I used MIT App Inventor. It also uses blocks, like LabVIEW, but it is much simpler.

Tkinter

Simple and fast, but limited for complex interfaces.

Qt Designer

Visual design with a better look, but a longer setup.

LabVIEW

Block programming for data and instruments.

Workflow Overview

1. Layout Design

Build the screen with layout boxes, buttons and a background image.

2. BLE Extension

Add the BLE extension to the project.

3. Block Programming

Program the connection and the Morse code buttons.

4. Testing

Send dots and lines from the phone to the ESP.

Creating the App

I used MIT App Inventor to build the app.

I already had an account, so I only logged in and clicked New Project.

The app works together with my Week 11 project, Networking and Communications.

Layout and Design

MIT App Inventor has a Layout section. Each layout box organizes the objects you place inside it.

I had to play with these boxes until each object was in the place I wanted.

Layout section in MIT App Inventor
Layout section in MIT App Inventor.

Then I added a button in the top right corner. Its job is to connect the ESP with the phone when I tap it.

The properties of every object are in the panel on the right, which opens when I click the object.

To make the button look better, I gave it a round shape and added a Bluetooth symbol. This way, it is clear what the button does.

After that, I added a background image, using the same method I used for the button image.

Then I added a second button for the Morse code.

Here is the final layout of the app, with both buttons and the background image and the full tree of components in the app.

Button 1

Round Bluetooth button that connects the ESP to the phone.

Button 2

Morse code button.

Background

Background image added to the screen.

BLE Extension

The XIAO ESP32-C3 does not use classic Bluetooth, only BLE. Because of that, I had to download a BLE extension and add it to the app.

I downloaded the extension from this link.

Block Programming

To program the app, I switched from Designer to Blocks in the top right corner. MIT App Inventor uses block programming.

Blocks that program the connection and Morse code buttons
Blocks used in the app.

Connect

When I tap button 1, the phone connects to the ESP.

Short Tap

One tap sends an "A", which the ESP reads as a dot.

Long Press

Holding the button sends a "B", which the ESP reads as a line.

As in the previous code, after some time without receiving anything, the ESP sends the letter to the other ESP.

Getting the BLE Address

The app needs the BLE address of the ESP to connect. To get it, I used the following code.


#include <BLEDevice.h>

void setup() {
  Serial.begin(115200);
  delay(1000);
  BLEDevice::init("test");
  Serial.print("Direccion BLE: ");
  Serial.println(BLEDevice::getAddress().toString().c_str());
}

void loop() {}

Here is the final code that I used:


#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <BLEDevice.h>
#include <BLEServer.h>

uint8_t MAC_A[] = {0xAC, 0x27, 0x6E, 0x7E, 0xCC, 0x24};
uint8_t MAC_B[] = {0xAC, 0x27, 0x6E, 0x80, 0xFD, 0x10};
uint8_t peerAddress[6];

const uint8_t WIFI_CHANNEL = 1;

// Time without new symbols before the letter is sent
const unsigned long LETTER_GAP_MS = 2000;
const int MAX_SYMBOLS = 7;

// IDs used by the app to find the BLE service and characteristic
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHAR_UUID    "beb5483e-36e1-4688-b7f5-ea07361b26a8"

struct MorseEntry {
  const char *code;
  char letter;
};

const MorseEntry table[] = {
  {".-", 'A'},   {"-...", 'B'}, {"-.-.", 'C'}, {"-..", 'D'},
  {".", 'E'},    {"..-.", 'F'}, {"--.", 'G'},  {"....", 'H'},
  {"..", 'I'},   {".---", 'J'}, {"-.-", 'K'},  {".-..", 'L'},
  {"--", 'M'},   {"-.", 'N'},   {"---", 'O'},  {".--.", 'P'},
  {"--.-", 'Q'}, {".-.", 'R'},  {"...", 'S'},  {"-", 'T'},
  {"..-", 'U'},  {"...-", 'V'}, {".--", 'W'},  {"-..-", 'X'},
  {"-.--", 'Y'}, {"--..", 'Z'},
  {"-----", '0'}, {".----", '1'}, {"..---", '2'}, {"...--", '3'},
  {"....-", '4'}, {".....", '5'}, {"-....", '6'}, {"--...", '7'},
  {"---..", '8'}, {"----.", '9'}
};

char decodeMorse(const char *code) {
  for (const auto &e : table) {
    if (strcmp(code, e.code) == 0) return e.letter;
  }
  return '?';
}

// Last symbol received from the app:

volatile char pendingSymbol = 0;
char symbols[MAX_SYMBOLS + 1] = "";
int symCount = 0;
unsigned long lastSymbolTime = 0;

class ServerCallbacks : public BLEServerCallbacks {
  void onDisconnect(BLEServer *server) {
    // Advertise again so the phone can reconnect
    BLEDevice::startAdvertising();
  }
};

class CharCallbacks : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic *c) {
    auto value = c->getValue();
    if (value.length() > 0) {
      // I only save it here and process it in loop()
      pendingSymbol = value.c_str()[0];
    }
  }
};

#if ESP_ARDUINO_VERSION_MAJOR >= 3
void onDataRecv(const esp_now_recv_info_t *info,
                const uint8_t *data, int len) {
  const uint8_t *src = info->src_addr;
#else
void onDataRecv(const uint8_t *src, const uint8_t *data, int len) {
#endif
  // Ignore messages that do not come from the other board
  if (memcmp(src, peerAddress, 6) != 0) return;

  char code[16];
  int n = min(len, 15);
  memcpy(code, data, n);
  code[n] = '\0';

  Serial.print(decodeMorse(code));
}

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.setSleep(false);
  WiFi.disconnect();
  esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);

  // The same code runs on both boards, so each one checks its own MAC
  uint8_t myMac[6];
  WiFi.macAddress(myMac);

  if (memcmp(myMac, MAC_A, 6) == 0) {
    memcpy(peerAddress, MAC_B, 6);
  } else {
    memcpy(peerAddress, MAC_A, 6);
  }

  esp_now_init();
  esp_now_register_recv_cb(onDataRecv);

  esp_now_peer_info_t peerInfo = {};
  memcpy(peerInfo.peer_addr, peerAddress, 6);
  peerInfo.channel = WIFI_CHANNEL;
  peerInfo.ifidx = WIFI_IF_STA;
  peerInfo.encrypt = false;
  esp_now_add_peer(&peerInfo);

  BLEDevice::init("XIAO-C3");
  BLEServer *server = BLEDevice::createServer();
  server->setCallbacks(new ServerCallbacks());

  BLEService *service = server->createService(SERVICE_UUID);
  BLECharacteristic *characteristic = service->createCharacteristic(
      CHAR_UUID,
      BLECharacteristic::PROPERTY_READ |
      BLECharacteristic::PROPERTY_WRITE);
  characteristic->setCallbacks(new CharCallbacks());
  service->start();

  BLEDevice::getAdvertising()->addServiceUUID(SERVICE_UUID);
  BLEDevice::startAdvertising();

  // Starting BLE can change the channel, so I set it again
  esp_wifi_set_channel(WIFI_CHANNEL, WIFI_SECOND_CHAN_NONE);
}

void loop() {
  if (pendingSymbol != 0) {
    char c = toupper(pendingSymbol);
    pendingSymbol = 0;

    char s = 0;
    if (c == 'A') s = '.';
    else if (c == 'B') s = '-';

    if (s != 0 && symCount < MAX_SYMBOLS) {
      symbols[symCount++] = s;
      symbols[symCount] = '\0';
      lastSymbolTime = millis();
    }
  }

  // If no new symbol arrives for 2 seconds, the letter is finished and sends it
  if (symCount > 0 && millis() - lastSymbolTime > LETTER_GAP_MS) {
    esp_now_send(peerAddress, (const uint8_t *)symbols,
                 strlen(symbols) + 1);
    symCount = 0;
    symbols[0] = '\0';
  }
}

Testing the App

To test the app, I connected the phone to the ESP and sent dots and lines with the Morse code button.

Connection to My Final Project

My final project, Ancora, needs a mobile app that talks to the wristband through BLE.

This week I learned how to build the app, connect it to an ESP board and send data from the phone.

BLE

The same connection method can link the app to the ESP32-C6.

Mobile App

This app is my base for the Ancora app.

Downloadable Files