Esp32 notification app help

hello guys i am a beginner in esp 32 and mit app inventor. I am currently working for a project involving esp 32s and mit app inventor. I want to send the data from my esp 32 to the mit app for it to display. Here is my code for the esp 32

#include <esp_now.h>
#include <WiFi.h>
#include <BluetoothSerial.h>

// Initialize Bluetooth Serial
BluetoothSerial SerialBT;

// Structure to hold received data
struct DataPacket {
  float distance;
  char warning[20];
  char senderID[10]; // Optional sender ID for additional identification
};

DataPacket receivedData;

// Known MAC addresses of the senders
const char sender1MAC[] = "08:A6:F7:6F:E4:E4"; // Replace with Sender 1's MAC address
const char sender2MAC[] = "08:A6:F7:6F:D6:20"; // Replace with Sender 2's MAC address

// Variable to track the last sender
int lastSender = 2; // Initialize to 2 so the first expected sender is Sender 1

// Function to initialize ESP-NOW
void initESPNow() {
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
}

// Callback function to handle received data
void onDataRecv(const esp_now_recv_info_t *recvInfo, const uint8_t *incomingData, int len) {
  if (len == sizeof(receivedData)) {
    memcpy(&receivedData, incomingData, sizeof(receivedData));

    // Get the sender's MAC address from recvInfo
    char senderMAC[18];
    snprintf(senderMAC, sizeof(senderMAC), "%02X:%02X:%02X:%02X:%02X:%02X",
             recvInfo->src_addr[0], recvInfo->src_addr[1], recvInfo->src_addr[2],
             recvInfo->src_addr[3], recvInfo->src_addr[4], recvInfo->src_addr[5]);

    // Determine the sender
    int currentSender = 0;
    if (strcmp(senderMAC, "08:A6:F7:6F:E4:E4") == 0) {
      currentSender = 1;
    } else if (strcmp(senderMAC, "08:A6:F7:6F:D6:20") == 0) {
      currentSender = 2;
    } else {
      Serial.println("Data from Unknown Sender");
      return; // Ignore data from unknown senders
    }

    // Ensure alternation between senders
    if (currentSender == lastSender) {
      Serial.println("Waiting for the next sender...");
      return; // Ignore data if it's not from the expected sender
    }

    // Update the last sender
    lastSender = currentSender;

    // Log the received data
    Serial.print("Sender: ");
    Serial.print(currentSender == 1 ? "Sender 1" : "Sender 2");
    Serial.print(" (");
    Serial.print(senderMAC);
    Serial.print(") - Distance: ");
    Serial.print(receivedData.distance);
    Serial.print(" cm - ");
    Serial.println(receivedData.warning);

    // Send the data to the MIT App Inventor app via Bluetooth
    if (SerialBT.connected()) {
      SerialBT.print("Sender: ");
      SerialBT.print(currentSender == 1 ? "Sender 1" : "Sender 2");
      SerialBT.print(" - Distance: ");
      SerialBT.print(receivedData.distance);
      SerialBT.print(" cm - ");
      SerialBT.println(receivedData.warning);
    }
  } else {
    Serial.println("Received data size mismatch");
  }
}

void setup() {
  // Start serial communication for debugging
  Serial.begin(9600);

  // Configure WiFi to station mode
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  // Initialize Bluetooth Serial
  if (!SerialBT.begin("ESP32_FloodMonitor")) {
    Serial.println("Error starting Bluetooth");
  } else {
    Serial.println("Bluetooth started");
  }

  // Initialize ESP-NOW and register the receive callback
  initESPNow();
  esp_now_register_recv_cb(onDataRecv);
}

void loop() {
  // The data reception and Bluetooth transmission is handled by the callback function
  delay(1000); // Delay to prevent unnecessary loop executions
}

Thank you in advance for your help everyone!

Look in the Play store for a Bluetooth terminal app for testing the sketch independently of AI2.

Once you know that the sketch is sending and receiving data, proceed to the AI2 app.

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.

1 Like

Thank you so much!!! :star_struck: