diff --git a/platformio.ini b/platformio.ini index ef6b071..4d47f4b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,3 +16,5 @@ framework = arduino lib_deps = adafruit/Adafruit SSD1306@^2.5.13 adafruit/Adafruit GFX Library@^1.11.11 + mbed-seeed/BluetoothSerial@0.0.0+sha.f56002898ee8 + adafruit/Adafruit BusIO@^1.16.2 diff --git a/src/save_mit_app b/src/save_mit_app new file mode 100644 index 0000000..ff5a1e5 --- /dev/null +++ b/src/save_mit_app @@ -0,0 +1,60 @@ +#include "Arduino.h" +#include "BluetoothSerial.h" +#include +#include + +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"); + } +}