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!