#include #include #include #include Ticker tkSec; uint32_t timer1 = 8; // Set to true to define Relay as Normally Open (NO) #define RELAY_NO true // Set number of relays #define NUM_RELAYS 2 // Assign each GPIO to a relay int relayGPIOs[NUM_RELAYS] = {26, 27}; String relayState(int numRelay){ if(RELAY_NO){ if(digitalRead(relayGPIOs[numRelay-1])){ return ""; } else { return "checked"; } } else { if(digitalRead(relayGPIOs[numRelay-1])){ return "checked"; } else { return ""; } } return ""; } void everySecond() { if (timer1 && --timer1 == 0){ Serial.println("timer done"); if(RELAY_NO){ //Serial.print("Relay0 OFF NO "); digitalWrite(relayGPIOs[0], HIGH); }else{ //Serial.print("Relay0 OFF NC "); digitalWrite(relayGPIOs[0], LOW); } } } #define PERIPHERAL_NAME "Sesam_open" //Rock generate UUID's // uuidgen // Check with iphone lightblue #define SERVICE_UUID "186eaa96-ced3-43dc-bf61-3925b32308c9" #define CHARACTERISTIC_INPUT_UUID "8fbc5b21-61ed-4244-8cde-7287b4aa35ec" #define CHARACTERISTIC_OUTPUT_UUID "f1921465-7ce5-4994-a201-8494e068cbb7" // Curren t value of output characteristic persisted here static uint8_t outputData[1]; // Output characteristic is used to send the response back to the connected phone BLECharacteristic *pOutputChar; // Class defines methods called when a device connects and disconnects from the service class ServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) { Serial.println("BLE Client Connected"); } void onDisconnect(BLEServer* pServer) { BLEDevice::startAdvertising(); Serial.println("BLE Client Disconnected"); } }; class InputReceivedCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharWriteState) { uint8_t *inputValues = pCharWriteState->getData(); switch(inputValues[0]) { case 0x00: // Dont' now Serial.printf("Dont' now: %02x \r\n", inputValues[0]); outputData[0] = inputValues[0]; break; case 0x01: // Open door Serial.printf("Open door: %02x \r\n", inputValues[0]); outputData[0] = inputValues[0]; if(RELAY_NO){ if( relayGPIOs[0] ){ Serial.print("Relay0 ON NO "); digitalWrite(relayGPIOs[0], LOW); timer1 = 8; tkSec.attach(1, everySecond); } }else{ Serial.print("Relay0 ON NC "); digitalWrite(relayGPIOs[0], HIGH); } break; default: Serial.printf("default: %02x \r\n", inputValues[0]); outputData[0] = inputValues[0]; } Serial.printf("Sending response: %02x\r\n", outputData[0]); pOutputChar->setValue((uint8_t *)outputData, 1); pOutputChar->notify(); /* switch(inputValues[2]) { case 0x00: // add Serial.printf("Adding: %02x %02x\r\n", inputValues[0], inputValues[1]); outputData[0] = inputValues[0] + inputValues[1]; break; case 0x01: // subtract Serial.printf("Subtracting: %02x %02x\r\n", inputValues[0], inputValues[1]); outputData[0] = inputValues[0] - inputValues[1]; break; default: // multiply Serial.printf("Multiplying: %02x %02x\r\n", inputValues[0], inputValues[1]); outputData[0] = inputValues[0] * inputValues[1]; } Serial.printf("Sending response: %02x\r\n", outputData[0]); pOutputChar->setValue((uint8_t *)outputData, 1); pOutputChar->notify();*/ } }; void setup() { // Use the Arduino serial monitor set to this baud rate to view BLE peripheral logs Serial.begin(115200); Serial.println("Begin Setup BLE Service and Characteristics"); // Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH for(int i=1; i<=NUM_RELAYS; i++){ pinMode(relayGPIOs[i-1], OUTPUT); if(RELAY_NO){ digitalWrite(relayGPIOs[i-1], HIGH); } else{ digitalWrite(relayGPIOs[i-1], LOW); } } // Configure thes server BLEDevice::init(PERIPHERAL_NAME); BLEServer *pServer = BLEDevice::createServer(); // Create the service BLEService *pService = pServer->createService(SERVICE_UUID); // Create a characteristic for the service BLECharacteristic *pInputChar = pService->createCharacteristic( CHARACTERISTIC_INPUT_UUID, BLECharacteristic::PROPERTY_WRITE_NR | BLECharacteristic::PROPERTY_WRITE); pOutputChar = pService->createCharacteristic( CHARACTERISTIC_OUTPUT_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); // Hook callback to report server events pServer->setCallbacks(new ServerCallbacks()); pInputChar->setCallbacks(new InputReceivedCallbacks()); // Initial characteristic value outputData[0] = 0x00; pOutputChar->setValue((uint8_t *)outputData, 1); // Start the service pService->start(); // Advertise the service BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); Serial.println("BLE Service is advertising"); } void loop() { // put your main code here, to run repeatedly: delay(2000); }