Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE_comunication #63

Open
Pedro-Gui opened this issue Sep 5, 2023 · 0 comments
Open

BLE_comunication #63

Pedro-Gui opened this issue Sep 5, 2023 · 0 comments
Labels
Hardware:Dev Board Coral Dev Board issues subtype:ubuntu/linux type:docs-feature Doc issues for new feature, or clarifications about functionality

Comments

@Pedro-Gui
Copy link

Pedro-Gui commented Sep 5, 2023

Description

I want to utilize the wireless addon to establish a connection between my phone and the Coral Dev Board Micro. However, the Bluetooth examples provided are insufficient for comprehending its functionality. Upon inspecting the library folder, it appears that there is no function available for sending and receiving data within the library, edgefast_bluetooth.h. I've got a code for the ESP32, which illustrates the functionality I intend to implement:

`#include <Wire.h> // Library for I2C
#include <SparkFunMLX90614.h> // SparkFunMLX90614 Library

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLECharacteristic *characteristicTX; // This object is used to send data to the client

// Object responsible for communication with the infrared temperature sensor
IRTherm sensor;

bool deviceConnected = false; // Device connected control

const int LED = 2; // ESP32 internal LED (this pin may vary from board to board)
const int BUZZER = 23; // Buzzer pin

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "ab0828b1-198e-4351-b779-901fa0e0371e" // UART service UUID
#define CHARACTERISTIC_UUID_RX "4ac8a682-9736-4e5d-932b-e9b31405049c"
#define CHARACTERISTIC_UUID_TX "0972EF8C-7613-4075-AD52-756F33D4DA91"

// Callback to receive device connection events
class ServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};

void onDisconnect(BLEServer* pServer) {
  deviceConnected = false;
}

};

// Callback for handling characteristics writes
class CharacteristicCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic characteristic) {
// Returns a pointer to the register containing the current characteristic value
std::string rxValue = characteristic->getValue();
// Check if there is data (length greater than zero)
if (rxValue.length() > 0) {
Serial.println("
********");
Serial.print("Received Value: ");

    for (int i = 0; i < rxValue.length(); i++) {
      Serial.print(rxValue[i]);
    }

    Serial.println();

    // Do stuff based on the command received
    if (rxValue.find("L1") != -1) { 
      Serial.print("Turning LED ON!");
      digitalWrite(LED, HIGH);
    }
    else if (rxValue.find("L0") != -1) {
      Serial.print("Turning LED OFF!");
      digitalWrite(LED, LOW);
    }
    // Do stuff based on the command received from the app
    else if (rxValue.find("B1") != -1) { 
      Serial.print("Turning Buzzer ON!");
      digitalWrite(BUZZER, HIGH);
    }
    else if (rxValue.find("B0") != -1) {
      Serial.print("Turning Buzzer OFF!");
      digitalWrite(BUZZER, LOW);
    }

    Serial.println();
    Serial.println("*********");
  }
}

};

void setup() {
Serial.begin(115200);

pinMode(LED, OUTPUT);
pinMode(BUZZER, OUTPUT);

// Create the BLE Device
BLEDevice::init("ESP32-BLE"); // Bluetooth device name
// Create the BLE Server
BLEServer *server = BLEDevice::createServer(); // Create a BLE server
server->setCallbacks(new ServerCallbacks()); // Set the server callback
// Create the BLE Service
BLEService *service = server->createService(SERVICE_UUID);
// Create a BLE Characteristic for data transmission
characteristicTX = service->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);

characteristicTX->addDescriptor(new BLE2902());

// Create a BLE Characteristic for data reception
BLECharacteristic *characteristic = service->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);

characteristic->setCallbacks(new CharacteristicCallbacks());
// Start the service
service->start();
// Start advertising (ESP32 discovery)
server->getAdvertising()->start();

// Initialize the infrared temperature sensor
sensor.begin();
// Select temperature in Celsius
sensor.setUnit(TEMP_C);// You can also use TEMP_F for Fahrenheit or TEMP_K for Kelvin

Serial.println("Waiting for a client connection to notify...");
}

void loop() {
// If a device is connected
if (deviceConnected) {
// Call the "read" method of the sensor to perform the temperature reading
// read will return 1 if it can perform the reading, or 0 otherwise
if (sensor.read())
{
// Retrieve the ambient temperature reading
float tempAmbiente = sensor.ambient();
// Retrieve the temperature reading of the object pointed to by the sensor
float tempObjeto = sensor.object();

      // Let's convert the value to a char array:
      char txString[8]; // make sure this is big enough
      dtostrf(tempAmbiente, 1, 2, txString); // float_val, min_width, digits_after_decimal, char_buffer

      characteristicTX->setValue(txString); // Set the value that the characteristic will notify (send)       
      characteristicTX->notify(); // Send the value to the smartphone

      Serial.print("*** Sent Value: ");
      Serial.print(txString);
      Serial.println(" ***");
  }

/*
delay(1000);

characteristicTX2->setValue("Hello!"); // Sending a test message
characteristicTX2->notify(); // Send the value to the app!*/

}
delay(1000);

}`

Click to expand!

Issue Type

Feature Request, Documentation Feature Request

Operating System

Ubuntu

Coral Device

Dev Board Micro

Other Devices

No response

Programming Language

C++

Relevant Log Output

No response

@google-coral-bot google-coral-bot bot added Hardware:Dev Board Coral Dev Board issues subtype:ubuntu/linux type:docs-feature Doc issues for new feature, or clarifications about functionality labels Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hardware:Dev Board Coral Dev Board issues subtype:ubuntu/linux type:docs-feature Doc issues for new feature, or clarifications about functionality
Projects
None yet
Development

No branches or pull requests

1 participant