I'm having trouble doing my arduino to receive and read commands from mi iphone i combined 2 codes for that propose one is from electronic clinic and the other is an example this is the code:
#include <ArduinoBLE.h>
int relay1 = 1; // car switch ON
long int password1 = 98421615;// to on switch
long int password2 = 96951628;// to off switch
int relay2 = 2; // engin ON " this will turn on the switch for few seconds to turn on the engine, after the engine starts the switch is released again.
long int password3 = 74151525; // engine self on
long int password31 = 45614787; // engine self 0ff
int relay3 = 3; // doors open
long int password4 = 84515822;
int relay4 = 4; // doors close
long int password5 = 81426337;
int relay6 = 5; // for horn
long int password8 = 34156469; // to turn the horn button
int relay7 = 6; // anti theft
long int password9 = 24127161; // to turn on anti theft
long int password10 = 48615369; // to turn off anti theft
BLEService value("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
void setup() {
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW);
pinMode(relay3, OUTPUT);
digitalWrite(relay3, LOW);
pinMode(relay4, OUTPUT);
digitalWrite(relay4, LOW);
pinMode(relay6, OUTPUT);
digitalWrite(relay6, LOW);
pinMode(relay7, OUTPUT);
//digitalWrite(relay7, LOW);
digitalWrite(relay7, HIGH);
delay(500);
// incase if the power from battery is disconnected and then connected again then the horn will on and off few times
digitalWrite( relay6, HIGH);
delay(1000);
digitalWrite(relay6, LOW);
delay(500);
digitalWrite( relay6, HIGH);
delay(1000);
digitalWrite(relay6, LOW);
delay(500);
digitalWrite( relay6, HIGH);
delay(1000);
digitalWrite(relay6, LOW);
delay(500);
Serial.begin(9600);
while (!Serial);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("C20");
BLE.setAdvertisedService(value);
// add the characteristic to the service
value.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(value);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("C20");
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
if (value == password1)
{
digitalWrite(relay1,HIGH);
Serial.println("Switch On");
delay(1000);
BLE.value=0;
}
if( value == password2)
{
digitalWrite(relay1,LOW);
Serial.println("Switch OFF");
BLE.value=0;
}
if( value == password3) // for self on
{
digitalWrite(relay2,HIGH);
Serial.println("Self ON");
delay(2000);
digitalWrite(relay2,LOW);
BLE.value=0;
}
if( value == password31) // for self off
{
digitalWrite(relay2,LOW);
Serial.println("Self OFF");
delay(1000);
BLE.value=0;
}
if( value == password4) // for doors opening
{
digitalWrite(relay3,HIGH); // press button
Serial.println("Doors Opened");
delay(2000); // keep the switch pressed for 2 seconds
digitalWrite(relay3,LOW); // release button
BLE.value=0;
}
if( value == password5) // for doors opening
{
digitalWrite(relay4,HIGH); // press button
Serial.println("Doors Closed");
delay(2000); // keep the switch pressed for 2 seconds
digitalWrite(relay4,LOW); // release button
BLE.value=0;
}
if( value == password8) //to turn on and off horn switch
{
digitalWrite(relay6,HIGH); // press button
Serial.println("Horn ON");
delay(2000); // keep the switch pressed for 2 seconds
digitalWrite(relay6,LOW);
Serial.print("Horn OFF");
BLE.value=0;
}
if( value == password9) //to turn on anti theft
{
digitalWrite(relay7,HIGH);
Serial.println("Anti theft ON");
delay(2000);
BLE.value=0;
}
if( value == password10) //to turn off anti theft
{
digitalWrite(relay7,LOW);
Serial.println("Anti theft OFF");
delay(2000);
BLE.value=0;
}
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}