BLE Clock with Arduino and App Inventor

BLE Clock with Arduino and App Inventor © GPL3+

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

  • 4,645 views
  • 2 comments
  • 19 respects

Components and supplies

Apps and online services

About this project

The idea

I am currently working and learning about App Inventor 2 and how to connect it with my Arduino 101 and as I have always wanted to make an alarm clock, I decided to try to combine a device so common with the internet of things, to control all aspects of the clock through a Application of cellular. A small yet functional IoT device.

Steps

The system is a clock that can be modified with the application and you can program a single alarm, maybe put a video of the system working.

What I did was not simple, it was to try and improve my code, I think at this point my work is acceptable, I can gladly see and try my code, and also review the app .aia .aia file, any comment or improvement Is well received, to this work I have a lot of work and I will continue improving it, all the improvements will update them.

Code

My circuitArduino
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.aiaADA
Appp inventor file
No preview (download only).

Schematics

My circuit
Regla1 nxaeqzwnjv

Comments

Similar projects you might like

Arduino 101 BLE App

Project in progress by Alexis Santiago Allende

  • 10,798 views
  • 26 comments
  • 51 respects

Control RGB LED by Dragging – Arduino 101 & App Inventor

Project tutorial by DFRobot and CAVEDU Education

  • 4,439 views
  • 0 comments
  • 12 respects

Arduino 101 Home BLE System

Project tutorial by Alexis Santiago Allende

  • 2,623 views
  • 0 comments
  • 12 respects

Open Data Home Automation - Arduino 101 & App Inventor

Project tutorial by 3 developers

  • 2,517 views
  • 1 comment
  • 11 respects

BLE Larson Scanner (Hackster Box Project)

Project showcase by Gustavo Reynaga

  • 3,677 views
  • 2 comments
  • 11 respects
Add projectSign up / Login