Skip to content

Commit

Permalink
#37 Started setting up BLE on the M5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebedel committed Apr 9, 2024
1 parent b0e4735 commit a1d51f8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 49 deletions.
6 changes: 3 additions & 3 deletions iot/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions iot/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
102 changes: 56 additions & 46 deletions iot/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
#include <M5Stack.h>
#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 <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

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
}
58 changes: 58 additions & 0 deletions iot/src/main.cpp.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <M5Stack.h>
#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;
}
}

0 comments on commit a1d51f8

Please sign in to comment.