Hi All,
New to the forums, was programming before in Python and I must say the change feels big.
Currently I am doing a project with an ESP32 with BLE to switch some relays.
Now I am having the following problem and just can figure out how to do it, maybe one of you guys can help me on the way.
I created several relays characteristics and want to be able to switch the relay on and off on the moment the ESP receives a write on BLE.
// Relay1
char Relay1NameID[37] = "8469a6de-861a-11ea-bc55-0242ac130002";
char Relay1StateID[37] = "b3364172-8721-11ea-bc55-0242ac130003";
char Relay1Name[8] = "Relay1";
BLECharacteristic RelayNameCharacteristic1(BLEUUID(Relay1NameID), BLECharacteristic::PROPERTY_READ);
BLECharacteristic RelayStateCharacteristic1(BLEUUID(Relay1StateID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE);
//Relay2
char Relay2NameID[37] = "384f81cc-8720-11ea-bc55-0242ac130003";
char Relay2StateID[37] = "04603914-87b2-11ea-bc55-0242ac130003";
char Relay2Name[8] = "Relay2";
BLECharacteristic RelayNameCharacteristic2(BLEUUID(Relay2NameID), BLECharacteristic::PROPERTY_READ );
BLECharacteristic RelayStateCharacteristic2(BLEUUID(Relay2StateID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE);
//Relay3
char Relay3NameID[37] = "81b10b1a-87b1-11ea-bc55-0242ac130003";
char Relay3StateID[37] = "7b3debd6-87b1-11ea-bc55-0242ac130003";
char Relay3Name[8] = "Relay3";
BLECharacteristic RelayNameCharacteristic3(BLEUUID(Relay3NameID), BLECharacteristic::PROPERTY_READ );
BLECharacteristic RelayStateCharacteristic3(BLEUUID(Relay3StateID), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE);
// Relay Callback
class RelayCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *customCharacteristic) {
std::string rcvString = customCharacteristic->getValue();
if (rcvString.length() > 0) {
Serial.println("Value Received for BLEUUID: ");
Serial.print(customCharacteristic->getUUID().toString().c_str());
Serial.println("Currentvalue");
Serial.println((char*)&value);
for (int i = 0; i < rcvString.length(); ++i)
{
Serial.print(rcvString[i]);
value[i] = rcvString[i];
}
for (int i = rcvString.length(); i < 50; ++i)
{
value[i] = NULL;
}
customCharacteristic->setValue((char*)&value);
}
else {
Serial.println("Empty Value Received!");
}
}
};
the questions that I have is how I can get the class to switch all relays instead of having to write a class for each one seperate.
I am guessing with a multidimensional array where I do a lookup on the BLEUUID, but I just cannot get this to work.
Some pointers would be appreciated.
Thanks