Skip to content

Commit

Permalink
#37 Still trying to setup bluetooth
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebedel committed Apr 9, 2024
1 parent d966f16 commit 95915e2
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 163 deletions.
3 changes: 2 additions & 1 deletion iot/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp"
"cinttypes": "cpp",
"m5stack.h": "c"
}
}
92 changes: 53 additions & 39 deletions iot/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,79 @@
#include <BLEUtils.h>
#include <BLE2902.h>

#define SERVICE_UUID "0693C92E-8A68-41AA-83E2-AA0B17F70168"
#define CHARACTERISTIC_UUID "1F5FF96C-AA4A-4159-BCE4-6C25350CB78B"

BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;

class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
Serial.println("Client connected");
}
void onConnect(BLEServer *pServer)
{
Serial.println("Connected");
deviceConnected = true;
}

void onDisconnect(BLEServer *pServer)
{
Serial.println("Client disconnected");
}
void onDisconnect(BLEServer *pServer)
{
Serial.println("Disconnected");
deviceConnected = false;
}
};

class MyCallbacks : public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();
Serial.print("Received data: ");
Serial.println(value.c_str());
}
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string value = pCharacteristic->getValue();

// Handle received data
Serial.print("Received data: ");
Serial.println(value.c_str());
}
};

void setup()
{
M5.begin();
Serial.begin(9600);
Serial.begin(9600);

// Set the advertising name
BLEDevice::init("M5-Stack");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Initialize BLE
BLEDevice::init("M5-Stack");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());

// Create a service
BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// Create service and characteristic
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->addDescriptor(new BLE2902());

// Create a characteristic
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
"beb5483e-36e1-4688-b7f5-ea07361b26a8",
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE);
// Start the service
pService->start();

// Add a descriptor
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->addDescriptor(new BLE2902());
// Start advertising
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->start();

// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();

Serial.println("BLE server started");
Serial.println("BLE server started");
}

void loop()
{
// Handle BLE events
// Check if device is connected
if (deviceConnected)
{
M5.Lcd.println("Device connected");
// Do something when device is connected
}

delay(1000);
}
97 changes: 97 additions & 0 deletions iot/src/main.cpp.bak2
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <M5Stack.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <set>

#define SERVICEUUID "0693C92E-8A68-41AA-83E2-AA0B17F70168"
#define CHARACTERISTICUUID "1F5FF96C-AA4A-4159-BCE4-6C25350CB78B"

BLEServer *pServer = NULL;
BLEService *pService = NULL;
BLECharacteristic *pCharacteristic = NULL;
std::set<uint16_t> connectedClients;

class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
// Get connId of the connected client
uint16_t connId = pServer->getConnId();
connectedClients.insert(connId);
Serial.print("Client connected with connId: ");
Serial.println(connId);

String data = "Hello from Arduino!";
pCharacteristic->setValue(data.c_str());
pCharacteristic->notify();
}

void onDisconnect(BLEServer *pServer)
{
// Get connId of the disconnected client
uint16_t connId = pServer->getConnId();
connectedClients.erase(connId);
Serial.print("Client disconnected with connId: ");
Serial.println(connId);
}
};

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);

// Set the advertising name
BLEDevice::init("M5-Stack");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());

pService = pServer->createService(SERVICEUUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTICUUID,
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();

M5.Lcd.println("BLE server started");
Serial.println("BLE server started");
}

void loop()
{
if (M5.BtnA.wasPressed())
{
M5.Lcd.println("Cleaning clients");
for (auto &id : connectedClients)
{
M5.Lcd.println("Cleaning client: " + id);
pServer->disconnect(id);
}
connectedClients.clear();
pServer->getAdvertising()->start();
}
M5.update();
// Handle BLE events
}
4 changes: 2 additions & 2 deletions mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ android {
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion

namespace 'com.smartbin.app'
namespace 'app.smartbin.epi'
defaultConfig {
applicationId 'com.smartbin.app'
applicationId 'app.smartbin.epi'
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down
1 change: 1 addition & 0 deletions mobile/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myapp"/>
<data android:scheme="com.smartbin.app"/>
<data android:scheme="app.smartbin.epi"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" android:exported="false"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smartbin.app
package app.smartbin.epi

import android.os.Build
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smartbin.app
package app.smartbin.epi

import android.app.Application
import android.content.res.Configuration
Expand Down
4 changes: 2 additions & 2 deletions mobile/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default ({ config }: ConfigContext): ExpoConfig => {
extra: {
env: "production",
production: true,
apiUrl: undefined,
apiUrl: "https://smart-bin-jade.vercel.app/api",
},
};
break;
Expand All @@ -21,7 +21,7 @@ export default ({ config }: ConfigContext): ExpoConfig => {
extra: {
env: "development",
production: false,
apiUrl: "http://172.20.10.2:3000/api",
apiUrl: "https://smart-bin-jade.vercel.app/api",
},
};
break;
Expand Down
14 changes: 5 additions & 9 deletions mobile/ios/smartbinesp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
13B07FB51A68108700A75B9A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = smartbinesp/Images.xcassets; sourceTree = "<group>"; };
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = smartbinesp/Info.plist; sourceTree = "<group>"; };
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = smartbinesp/main.m; sourceTree = "<group>"; };
4B01632B2BC5981A00794DDE /* smartbinespRelease.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; name = smartbinespRelease.entitlements; path = smartbinesp/smartbinespRelease.entitlements; sourceTree = "<group>"; };
58EEBF8E8E6FB1BC6CAF49B5 /* libPods-smartbinesp.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-smartbinesp.a"; sourceTree = BUILT_PRODUCTS_DIR; };
6C2E3173556A471DD304B334 /* Pods-smartbinesp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-smartbinesp.debug.xcconfig"; path = "Target Support Files/Pods-smartbinesp/Pods-smartbinesp.debug.xcconfig"; sourceTree = "<group>"; };
7A4D352CD337FB3A3BF06240 /* Pods-smartbinesp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-smartbinesp.release.xcconfig"; path = "Target Support Files/Pods-smartbinesp/Pods-smartbinesp.release.xcconfig"; sourceTree = "<group>"; };
Expand All @@ -50,6 +51,7 @@
13B07FAE1A68108700A75B9A /* smartbinesp */ = {
isa = PBXGroup;
children = (
4B01632B2BC5981A00794DDE /* smartbinespRelease.entitlements */,
BB2F792B24A3F905000567C9 /* Supporting */,
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
Expand Down Expand Up @@ -365,7 +367,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = smartbinesp/smartbinesp.entitlements;
CODE_SIGN_ENTITLEMENTS = smartbinesp/smartbinespRelease.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
Expand Down Expand Up @@ -450,10 +452,7 @@
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down Expand Up @@ -510,10 +509,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_CFLAGS = "$(inherited)";
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
OTHER_LDFLAGS = "$(inherited) ";
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down
Loading

0 comments on commit 95915e2

Please sign in to comment.