From a1d51f8a86d76513961929826164f00390299632 Mon Sep 17 00:00:00 2001 From: Alexandre Bedel Date: Tue, 9 Apr 2024 13:11:33 +0200 Subject: [PATCH] #37 Started setting up BLE on the M5 --- iot/.vscode/c_cpp_properties.json | 6 +- iot/platformio.ini | 3 + iot/src/main.cpp | 102 ++++++++++++++++-------------- iot/src/main.cpp.bak | 58 +++++++++++++++++ 4 files changed, 120 insertions(+), 49 deletions(-) create mode 100644 iot/src/main.cpp.bak diff --git a/iot/.vscode/c_cpp_properties.json b/iot/.vscode/c_cpp_properties.json index df6ae8f4f..6346a7189 100644 --- a/iot/.vscode/c_cpp_properties.json +++ b/iot/.vscode/c_cpp_properties.json @@ -11,6 +11,7 @@ "/Users/alexandrebedel/Desktop/Msc/esp/iot/include", "/Users/alexandrebedel/Desktop/Msc/esp/iot/src", "/Users/alexandrebedel/Desktop/Msc/esp/iot/lib/uart_frame", + "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", "/Users/alexandrebedel/Desktop/Msc/esp/iot/lib/GoPLUS2", "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/UUID", "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/Adafruit NeoPixel", @@ -222,7 +223,6 @@ "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/Unity/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", - "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", @@ -252,6 +252,7 @@ "/Users/alexandrebedel/Desktop/Msc/esp/iot/include", "/Users/alexandrebedel/Desktop/Msc/esp/iot/src", "/Users/alexandrebedel/Desktop/Msc/esp/iot/lib/uart_frame", + "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", "/Users/alexandrebedel/Desktop/Msc/esp/iot/lib/GoPLUS2", "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/UUID", "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/Adafruit NeoPixel", @@ -463,7 +464,6 @@ "/Users/alexandrebedel/Desktop/Msc/esp/iot/.pio/libdeps/m5stack-core-esp32/Unity/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/ArduinoOTA/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/AsyncUDP/src", - "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BLE/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/BluetoothSerial/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/DNSServer/src", "/Users/alexandrebedel/.platformio/packages/framework-arduinoespressif32/libraries/EEPROM/src", @@ -505,7 +505,7 @@ "ARDUINO=10812", "ARDUINO_VARIANT=\"m5stack_core_esp32\"", "ARDUINO_BOARD=\"M5Stack Core ESP32\"", - "ARDUINO_PARTITION_default", + "ARDUINO_PARTITION_default_8MB", "" ], "cStandard": "gnu99", diff --git a/iot/platformio.ini b/iot/platformio.ini index c88bf914f..eb5468031 100644 --- a/iot/platformio.ini +++ b/iot/platformio.ini @@ -25,3 +25,6 @@ lib_deps = lbussy/LCBUrl@^1.1.9 adafruit/Adafruit NeoPixel@^1.12.0 robtillaart/UUID@^0.1.6 +board_upload.flash_size = 8MB +board_upload.maximum_size = 8388608 +board_build.partitions = default_8MB.csv \ No newline at end of file diff --git a/iot/src/main.cpp b/iot/src/main.cpp index 3713ea083..d47384840 100644 --- a/iot/src/main.cpp +++ b/iot/src/main.cpp @@ -1,58 +1,68 @@ #include -#include "core.h" -#include "camera.h" -#include "filesystem.h" -#include "network.h" -#include "servo.h" -#include "motion.h" -#include "led.h" - -bool closeTimeout = false; -String binId = ""; -unsigned long lastPictureTime = millis(); -unsigned long currentTime = millis(); -auto expoAddr = [](String binId, IPAddress ip = IPAddress(172, 20, 10, 2)) -> String +#include +#include +#include +#include + +BLEServer *pServer = NULL; + +class MyServerCallbacks : public BLEServerCallbacks { - return String("exp://") + ip.toString() + ":8081/--/" + binId; + void onConnect(BLEServer *pServer) + { + Serial.println("Client connected"); + } + + void onDisconnect(BLEServer *pServer) + { + Serial.println("Client disconnected"); + } +}; + +class MyCallbacks : public BLECharacteristicCallbacks +{ + void onWrite(BLECharacteristic *pCharacteristic) + { + std::string value = pCharacteristic->getValue(); + Serial.print("Received data: "); + Serial.println(value.c_str()); + } }; void setup() { M5.begin(); Serial.begin(9600); - Network::init(); - Filesystem::init(); - ServoMotor::init(); - Camera::init(); - Motion::init(); - Led::init(); - lastPictureTime = millis(); - xTaskCreatePinnedToCore(ServoMotor::buttonsTask, "buttonsTask", 4096, NULL, 1, NULL, 0); + + // Set the advertising name + BLEDevice::init("M5-Stack"); + pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks()); + + // Create a service + BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); + + // Create a characteristic + BLECharacteristic *pCharacteristic = pService->createCharacteristic( + "beb5483e-36e1-4688-b7f5-ea07361b26a8", + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE | + BLECharacteristic::PROPERTY_NOTIFY | + BLECharacteristic::PROPERTY_INDICATE); + + // Add a descriptor + pCharacteristic->setCallbacks(new MyCallbacks()); + pCharacteristic->addDescriptor(new BLE2902()); + + // Start the service + pService->start(); + // Start advertising + pServer->getAdvertising()->start(); + + Serial.println("BLE server started"); } void loop() { - String type = ""; - - if (binId.isEmpty()) - { - binId = Filesystem::getBinId(); - M5.Lcd.qrcode(expoAddr(binId)); - } - if (Motion::isDetected()) - { - Led::on(); - Serial.println("Taking a picture"); - type = Camera::detectTrashType(); - lastPictureTime = millis(); - closeTimeout = true; - } - currentTime = millis(); - if (currentTime - lastPictureTime >= 10 * 1000 && closeTimeout) - { - Serial.println("Closing the servo motor after 10 seconds"); - Led::off(); - ServoMotor::close(SERVO_BOXES[type]); - closeTimeout = false; - } -} + // Handle BLE events +} \ No newline at end of file diff --git a/iot/src/main.cpp.bak b/iot/src/main.cpp.bak new file mode 100644 index 000000000..3713ea083 --- /dev/null +++ b/iot/src/main.cpp.bak @@ -0,0 +1,58 @@ +#include +#include "core.h" +#include "camera.h" +#include "filesystem.h" +#include "network.h" +#include "servo.h" +#include "motion.h" +#include "led.h" + +bool closeTimeout = false; +String binId = ""; +unsigned long lastPictureTime = millis(); +unsigned long currentTime = millis(); +auto expoAddr = [](String binId, IPAddress ip = IPAddress(172, 20, 10, 2)) -> String +{ + return String("exp://") + ip.toString() + ":8081/--/" + binId; +}; + +void setup() +{ + M5.begin(); + Serial.begin(9600); + Network::init(); + Filesystem::init(); + ServoMotor::init(); + Camera::init(); + Motion::init(); + Led::init(); + lastPictureTime = millis(); + xTaskCreatePinnedToCore(ServoMotor::buttonsTask, "buttonsTask", 4096, NULL, 1, NULL, 0); +} + +void loop() +{ + String type = ""; + + if (binId.isEmpty()) + { + binId = Filesystem::getBinId(); + M5.Lcd.qrcode(expoAddr(binId)); + } + if (Motion::isDetected()) + { + Led::on(); + Serial.println("Taking a picture"); + type = Camera::detectTrashType(); + lastPictureTime = millis(); + closeTimeout = true; + } + currentTime = millis(); + if (currentTime - lastPictureTime >= 10 * 1000 && closeTimeout) + { + Serial.println("Closing the servo motor after 10 seconds"); + Led::off(); + ServoMotor::close(SERVO_BOXES[type]); + closeTimeout = false; + } +}