15.- Two ESP32 with BLE send random temperature and humidity by Notification. Common Slider.
ESP32 with BLE generates (at random times) two random numbers temperatureBLE1 and humidityBLE2, and notifies them to the application.
Another ESP32 with BLE generates (at random times) two random numbers temperatureBLE2 and humidityBLE2, and notifies them to the application.
App sends a value to the two ESP32s through a Slider.
- Code for ESP32 with BLE1:
// Juan A. Villalpando
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
String valor;
long previousMillis_BLE1 = 0;
int interval_BLE1 = 1000;
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
valor = "";
for (int i = 0; i < value.length(); i++){
// Serial.print(value[i]); // Presenta value.
valor = valor + value[i];
}
Serial.println("*********");
Serial.print("valor = ");
Serial.println(valor); // Presenta valor.
}
}
};
///////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
// Create the BLE1 Device
BLEDevice::init("MyESP32_BLE1");
// Create the BLE Server
pServer = BLEDevice::createServer();
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());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis_BLE1 > interval_BLE1) {
float TemperatureBLE1 = random(10,60000)/1000.0; // 3 decimals
float HumidityBLE1 = random(5,99000)/1000.0; // 3 decimals
String temperatureBLE1 = String(TemperatureBLE1,3);
String humidityBLE1 = String(HumidityBLE1,3);
String tem_hum_BLE1 = temperatureBLE1 + "," + humidityBLE1;
std::string value = pCharacteristic->getValue();
pCharacteristic->setValue(tem_hum_BLE1.c_str()); // Notify.
pCharacteristic->notify();
previousMillis_BLE1 = currentMillis;
interval_BLE1 = random(500,1000);
}
}
- Code for ESP32 with BLE2:
// Juan A. Villalpando
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
String valor;
long previousMillis_BLE2 = 0;
int interval_BLE2 = 1000;
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
valor = "";
for (int i = 0; i < value.length(); i++){
// Serial.print(value[i]); // Presenta value.
valor = valor + value[i];
}
Serial.println("*********");
Serial.print("valor = ");
Serial.println(valor); // Presenta valor.
}
}
};
///////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
// Create the BLE2 Device
BLEDevice::init("MyESP32_BLE2");
// Create the BLE Server
pServer = BLEDevice::createServer();
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());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis_BLE2 > interval_BLE2) {
float TemperatureBLE2 = random(10,60000)/1000.0; // 3 decimals
float HumidityBLE2 = random(5,99000)/1000.0; // 3 decimals
String temperatureBLE2 = String(TemperatureBLE2,3);
String humidityBLE2 = String(HumidityBLE2,3);
String tem_hum_BLE2 = temperatureBLE2 + "," + humidityBLE2;
std::string value = pCharacteristic->getValue();
pCharacteristic->setValue(tem_hum_BLE2.c_str()); // Notify.
pCharacteristic->notify();
previousMillis_BLE2 = currentMillis;
interval_BLE2 = random(500,2000);
}
}