tp BLE
This commit is contained in:
130
tp3/BLE/BLE.ino
Normal file
130
tp3/BLE/BLE.ino
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
|
||||
Ported to Arduino ESP32 by Evandro Copercini
|
||||
updates by chegewara
|
||||
*/
|
||||
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEServer.h>
|
||||
#include <BLEAdvertisedDevice.h>
|
||||
|
||||
int lux;
|
||||
int mini;
|
||||
// See the following for generating UUIDs:
|
||||
// https://www.uuidgenerator.net/
|
||||
|
||||
#define SERVICE_UUID "f608bae5-6eec-4be7-aafb-2bd43ce942bf"
|
||||
#define POWER_READ_UUID "06c79a69-54b1-46e9-9b1d-afb77e6fad15"
|
||||
#define POWER_CHECK_UUID "f54a0bdf-afa3-4d18-a96c-770ba5bc3577"
|
||||
#define SEUILS_UUID "e7588746-cde4-44c4-aec6-8ef838a71480"
|
||||
|
||||
bool estConnecte = false;
|
||||
char msgTOsend[30];
|
||||
//char rxValue [255];
|
||||
|
||||
class MyCallback : public BLECharacteristicCallbacks {
|
||||
void onRead(BLECharacteristic* pCharacteristic) {
|
||||
// Do something before the read completes
|
||||
|
||||
int lux=random(0, 9999);
|
||||
|
||||
sprintf(msgTOsend,"Puissance : %d",lux);
|
||||
pCharacteristic->setValue(msgTOsend);
|
||||
Serial.printf("Hello : %d \n",lux);
|
||||
//Serial.print('\n'):
|
||||
}
|
||||
void onWrite(BLECharacteristic* pCharacteristic) {
|
||||
// Do something because a new value was written.
|
||||
std::string rxValue=pCharacteristic->getValue(); //ex tel envoi 5000
|
||||
mini = atoi(rxValue.c_str());
|
||||
Serial.print("message received :");
|
||||
Serial.println(rxValue.c_str());
|
||||
// si valeur numérique :
|
||||
//uint8_t* received_data = pCharacteristic->getData();
|
||||
// Serial.println(*received_data,HEX);
|
||||
}
|
||||
};
|
||||
|
||||
class EtatServeur : public BLEServerCallbacks {
|
||||
void onConnect(BLEServer* pServer) {
|
||||
estConnecte = true;
|
||||
Serial.println("Connecté");
|
||||
}
|
||||
|
||||
void onDisconnect(BLEServer* pServer) {
|
||||
estConnecte = false;
|
||||
BLEDevice::startAdvertising();
|
||||
Serial.println("Déconnecté");
|
||||
}
|
||||
};
|
||||
|
||||
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||
Serial.printf("Advertised Device: %s", advertisedDevice.toString().c_str());
|
||||
if (advertisedDevice.haveRSSI()){
|
||||
Serial.printf("Rssi: %d \n", (int)advertisedDevice.getRSSI());
|
||||
}
|
||||
else {
|
||||
Serial.printf("\n");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
BLECharacteristic *pCharacteristic;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting BLE ");
|
||||
|
||||
BLEDevice::init("PomPom");
|
||||
BLEServer *pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(new EtatServeur());
|
||||
BLEService *pService = pServer->createService(SERVICE_UUID);
|
||||
//BLECharacteristic *pCharacteristic = pService->createCharacteristic(
|
||||
pCharacteristic = pService->createCharacteristic(
|
||||
POWER_READ_UUID,
|
||||
BLECharacteristic::PROPERTY_READ |
|
||||
BLECharacteristic::PROPERTY_NOTIFY |
|
||||
BLECharacteristic::PROPERTY_WRITE
|
||||
);
|
||||
|
||||
pCharacteristic->setValue("Hello ESP32 ");
|
||||
|
||||
//BLECharacteristic::setCallbacks(new MyCallback())
|
||||
pCharacteristic->setCallbacks(new MyCallback());
|
||||
|
||||
|
||||
pService->start();
|
||||
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
|
||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
||||
/*pAdvertising->setScanResponse(true);
|
||||
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
|
||||
pAdvertising->setMinPreferred(0x12);*/
|
||||
//pAdvertising->setCallbacks(new MyAdvertisedDeviceCallbacks());
|
||||
|
||||
BLEDevice::startAdvertising();
|
||||
Serial.println("Characteristic defined! Now you can read it in your phone!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//float h = getRssi();
|
||||
// put your main code here, to run repeatedly:
|
||||
|
||||
if (estConnecte) {
|
||||
|
||||
lux=random(0, 9999);
|
||||
|
||||
if(lux<mini) {
|
||||
Serial.println("Pas assez de lumière");
|
||||
sprintf(msgTOsend,"Pas assez de lumière. Lux = %d",lux);
|
||||
pCharacteristic->setValue(msgTOsend);
|
||||
pCharacteristic->notify();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
//BLEDevice::startAdvertising();
|
||||
}
|
14
tp3/BLE/debug.cfg
Normal file
14
tp3/BLE/debug.cfg
Normal file
@@ -0,0 +1,14 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
#
|
||||
# Example OpenOCD configuration file for ESP32-WROVER-KIT board.
|
||||
#
|
||||
# For example, OpenOCD can be started for ESP32 debugging on
|
||||
#
|
||||
# openocd -f board/esp32-wrover-kit-3.3v.cfg
|
||||
#
|
||||
|
||||
# Source the JTAG interface configuration file
|
||||
source [find interface/ftdi/esp32_devkitj_v1.cfg]
|
||||
set ESP32_FLASH_VOLTAGE 3.3
|
||||
# Source the ESP32 configuration file
|
||||
source [find target/esp32.cfg]
|
19
tp3/BLE/debug_custom.json
Normal file
19
tp3/BLE/debug_custom.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name":"Arduino on ESP32",
|
||||
"toolchainPrefix":"xtensa-esp32-elf",
|
||||
"svdFile":"esp32.svd",
|
||||
"request":"attach",
|
||||
"postAttachCommands":[
|
||||
"set remote hardware-watchpoint-limit 2",
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
],
|
||||
"overrideRestartCommands":[
|
||||
"monitor reset halt",
|
||||
"monitor gdb_sync",
|
||||
"thb setup",
|
||||
"c"
|
||||
]
|
||||
}
|
46087
tp3/BLE/esp32.svd
Normal file
46087
tp3/BLE/esp32.svd
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user