Alexis Santiago Allende
Published © GPL3+

BLE Clock with Arduino and App Inventor

An alarm clock that can be controlled by means of a smartphone application over BLE.

IntermediateProtip4 hours6,761
BLE Clock with Arduino and App Inventor

Things used in this project

Story

Read more

Schematics

My circuit

Code

My circuit

Arduino
Alarm clock code version 0.9
#include <CurieBLE.h>
#include <LiquidCrystal.h>
#include <CurieTime.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int ledPin = 2; // set ledPin to use on-board LED
BLEPeripheral blePeripheral; // create peripheral instance
BLEService HolaMundo("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service

// create switch characteristic and allow remote device to read and write
BLEUnsignedIntCharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEDescriptor descriptor1("2901", "HolaMundo!");

boolean a=LOW,b=LOW,c=LOW,d=LOW,e=LOW,f=LOW,g=LOW,mostrarH=LOW,mostarN=LOW,alarma=LOW;
int valor,anio=2000;
byte hora=0,minu=0,segu=0,dia=1,mes=1,hoku=0,miku=0,horalarma,minualarma;
char clockTime[8],lista[3],lista2[3],ajuste1[8],ajuste2[10];


void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
  pinMode(10, OUTPUT);
  //setTime(16, 56, 24,31, 12, 2017);
  // set the local name peripheral advertises
  blePeripheral.setLocalName("RelojBLE");
  // set the UUID for the service this peripheral advertises
  blePeripheral.setAdvertisedServiceUuid(HolaMundo.uuid());

  // add service and characteristic
  blePeripheral.addAttribute(HolaMundo);
  blePeripheral.addAttribute(switchChar);
  blePeripheral.addAttribute(descriptor1);

  // assign event handlers for connected, disconnected to peripheral
  blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
  switchChar.setValue(0);

  // advertise the service
  blePeripheral.begin();
  Serial.println(("Bluetooth device active, waiting for connections..."));
  digitalWrite(10, HIGH);
}

void loop() {
  if(e==LOW)
  {
   lcd.clear();
   lcd.print("No connected");
   e=HIGH; 
  }
  if(f==HIGH)
  {
   lcd.clear();
   lcd.print("Connected");
   f=LOW; 
  }
  if(g==HIGH)
  {
   lcd.clear();
   g=LOW; 
  }
  if(horalarma==hour()&&minualarma==minute()&& alarma==HIGH)
  {
   digitalWrite(ledPin, HIGH);
   delay(500);
   digitalWrite(ledPin, LOW);  
   delay(500);
  }else if(horalarma!=hour()&&minualarma!=minute()&&alarma==HIGH){digitalWrite(ledPin, LOW); }
  
  // poll peripheral
  blePeripheral.poll();
  //use sprintf to create a time string of the hour, minte and seconds
  if(mostrarH==LOW)
  {
  sprintf(clockTime, "%02d:%02d:%02d", hour(), minute(), second());
  lcd.setCursor(3, 1);
  lcd.print(clockTime);
  if(hour()>=0&&hour()<=11)
  {
   lcd.setCursor(11, 1);
    lcd.print("AM");
  }
  else
  {
    lcd.setCursor(11, 1);
    lcd.print("PM");
    }
  lcd.setCursor(0, 0);
  
  }
  else if(mostrarH==HIGH)
  {
    sprintf(ajuste1, "%02d:%02d:%02d", hora, minu, segu);
    lcd.setCursor(4, 0);
    lcd.print(ajuste1);
    sprintf(ajuste2, "%02d:%02d:%04d", dia, mes, anio);
    lcd.setCursor(3, 1);
    lcd.print(ajuste2);
  
  }
   
}

void blePeripheralConnectHandler(BLECentral& central) {
  // central connected event handler
  f=HIGH;
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
}

void blePeripheralDisconnectHandler(BLECentral& central) {
  // central disconnected event handler
  e=LOW;
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
  
}

void switchCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
  // central wrote new value to characteristic, update LED
  Serial.print("Characteristic event, written: ");
  valor=switchChar.value();
  Serial.println(valor);
  if (switchChar.value() == 1 && a == LOW) { // 1 in ASCII
          
          Serial.print("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
          a = HIGH;
          alarma=LOW;
        } else if (switchChar.value() == 1 && a == HIGH)  {  //when 1 was read again (second time)          

          Serial.println("LED off");
          digitalWrite(ledPin, LOW);          // will turn the LED off
          a = LOW;
          alarma=LOW;
        }
        else if (switchChar.value() == 199&& b==LOW)  {  //when 1 was read again (second time)          

          mostrarH=HIGH;
          g=HIGH;
          b=HIGH;
          
        } else if (switchChar.value() == 199&& b==HIGH)  {  //when 1 was read again (second time)          

          f=HIGH;
          mostrarH=LOW;
          b=LOW;
          
        }
        else if (switchChar.value() == 255 && c==LOW)  {  //when 1 was read again (second time)          

          digitalWrite(10,LOW);
          c=HIGH;
        } else if (switchChar.value() == 255 && c == HIGH)  {  //when 1 was read again (second time)          

          
          digitalWrite(10, HIGH);          // will turn the LED off
          c = LOW;
        }
        //Cambio de horas
        else if (switchChar.value() == 200)  {  //when 1 was read again (second time)          

          hora++;
          if(hora>23)
          {
            hora=0;
          }
          
        } else if (switchChar.value() == 201)  {  //when 1 was read again (second time)          

          hora--;
          if(hora==255)
          {
            hora=23;
          }
        }
        else if (switchChar.value() == 202)  {  //when 1 was read again (second time)          

          minu++;
          if(minu>59)
          {
            minu=0;
          }
          
        } else if (switchChar.value() == 203)  {  //when 1 was read again (second time)          

          minu--;
          if(minu==255)
          {
            minu=59;
          }
        }
        else if (switchChar.value() == 204)  {  //when 1 was read again (second time)          

          segu++;
          if(segu>59)
          {
            segu=0;
          }
          
        } else if (switchChar.value() == 205)  {  //when 1 was read again (second time)          

          segu--;
          if(segu==255)
          {
            segu=59;
          }
        }
        else if (switchChar.value() == 206)  {  //when 1 was read again (second time)          

          dia++;
          if(dia>31)
          {
            dia=1;
          }
          
        } else if (switchChar.value() == 207)  {  //when 1 was read again (second time)          

          dia--;
          if(dia==0)
          {
            dia=31;
          }
        }
        else if (switchChar.value() == 208)  {  //when 1 was read again (second time)          

          mes++;
          if(mes>12)
          {
            mes=1;
          }
          
        } else if (switchChar.value() == 209)  {  //when 1 was read again (second time)          

          mes--;
          if(mes==0)
          {
            mes=12;
          }
        }
        else if (switchChar.value() == 210)  {  //when 1 was read again (second time)          

          anio++;
          if(anio>2020)
          {
            anio=2000;
          }
          
        } else if (switchChar.value() == 211)  {  //when 1 was read again (second time)          

          anio--;
          if(anio<2000)
          {
            anio=2020;
          }
        }
        else if (switchChar.value() == 212)  {  //when 1 was read again (second time)          

          setTime(hora,minu,segu,dia,mes,anio);
          
        } 
        else if (switchChar.value() == 213)  {  //when 1 was read again (second time)          

          hoku++;
          if(hoku>23)
          {
            hoku=0;
          }
          
        } else if (switchChar.value() == 214)  {  //when 1 was read again (second time)          

          hoku--;
          if(hoku==255)
          {
            hoku=23;
          }
        }
        else if (switchChar.value() == 215)  {  //when 1 was read again (second time)          

          miku++;
          if(miku>59)
          {
            miku=0;
          }
          
        } else if (switchChar.value() == 216)  {  //when 1 was read again (second time)          

          miku--;
          if(miku==255)
          {
            miku=59;
          }
        }
        else if (switchChar.value() == 217)  {  //when 1 was read again (second time)    

                hora=0;minu=0;segu=0;dia=1;mes=1;anio=2000;hoku=0;miku=0;
                
          
          
        }
        else if (switchChar.value() == 218)  {  //when 1 was read again (second time)    

                horalarma=hoku;
                minualarma=miku;
                alarma=HIGH;
          
          
        }  
        
}

Reloj_beta1_finish.aia

ADA
Appp inventor file
No preview (download only).

Credits

Alexis Santiago Allende

Alexis Santiago Allende

0 projects • 59 followers
Im a person who since young feel a passion for electronics, I also like to cook pizza and travel. Now Im working on the internet of things

Comments