Add Bluetooth functionality to control LED via serial commands for app inventor

This commit is contained in:
Lemonochrme 2024-12-11 11:43:16 +01:00
parent 88e635bfb7
commit 23e703d123
2 changed files with 62 additions and 0 deletions

View file

@ -16,3 +16,5 @@ framework = arduino
lib_deps = lib_deps =
adafruit/Adafruit SSD1306@^2.5.13 adafruit/Adafruit SSD1306@^2.5.13
adafruit/Adafruit GFX Library@^1.11.11 adafruit/Adafruit GFX Library@^1.11.11
mbed-seeed/BluetoothSerial@0.0.0+sha.f56002898ee8
adafruit/Adafruit BusIO@^1.16.2

60
src/save_mit_app Normal file
View file

@ -0,0 +1,60 @@
#include "Arduino.h"
#include "BluetoothSerial.h"
#include <Wire.h>
#include <SPI.h>
BluetoothSerial SerialBT;
const int LED_BUILTIN = 2;
String receivedMessage = "";
void processBluetoothMessage(String message);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
Serial.println("Starting Bluetooth...");
if (!SerialBT.begin("ESP32 Yohan et Robin")) {
Serial.println("Error on starting Bluetooth");
while (1);
}
Serial.println("Bluetooth started, waitint for connections...");
}
void loop() {
if (SerialBT.available()) {
char receivedChar = SerialBT.read();
receivedMessage += receivedChar;
Serial.print("Current message: ");
Serial.println(receivedMessage);
if (receivedMessage.endsWith("o")) {
processBluetoothMessage("ON");
receivedMessage = "";
} else if (receivedMessage.endsWith("f")) {
processBluetoothMessage("OFF");
receivedMessage = "";
}
}
}
void processBluetoothMessage(String message) {
message.trim();
Serial.println("Received: " + message);
if (message.equalsIgnoreCase("ON")) {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Turning LED ON");
} else if (message.equalsIgnoreCase("OFF")) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Turning LED OFF");
} else {
Serial.println("Invalid command received");
}
}