Jose Romaní
Published © GPL3+

Presence Detector Alarm 2.0 for "Dinoseto Robot" with App

This is the continuation of my other proyect 1.0 but with an HM-10 module. It can be used as an intruder detector. It has a downloadable apk

AdvancedFull instructions provided358
Presence Detector Alarm 2.0 for "Dinoseto Robot" with App

Things used in this project

Hardware components

Adafruit USB to TTL Serial Cable
×1
DSD TECH HM-10 Bluetooth 4.0 BLE iBeacon UART
×1
ELEGOO 120pcs Multicolored Dupont Wire
ELEGOO 120pcs Multicolored Dupont Wire
×1

Software apps and online services

Arduino IDE
Arduino IDE
MIT App Inventor
MIT App Inventor
fritzing

Story

Read more

Schematics

Dinosaur Alarm BLE Breadboard

This image can be used to reproduced the project conexions.

App "Dinoseto" Presence Detector with HM-10

Code

shortBeep

Arduino
This part of the code loaded on Arduino UNO, permits that the passive buzzer emits very short beeps a number of times. It serves as a warning that you have change of mode each time you press a button of the app. To test this code only the buzzer must be connected to the Arduino UNO on pin 11.
#define BUZZER 11
void setup() {
  Serial.begin(115200);  // Set Bluetooth baud rate. Must match the one of the module
  pinMode(BUZZER, OUTPUT);//Buzzer
  digitalWrite(BUZZER, LOW);
  shortBeep(80); 
}

void shortBeep(byte number)
{
  for(byte i=0; i<= number; i++)
  {
    analogWrite(BUZZER, 127);
    delay(2);
    analogWrite(BUZZER, 0);
    delay(2);
  }
}

Dinoseto BLE

Arduino
After connect all the modules of the project load this code to Arduino UNO and manage later with the mobile app.
/* Adjust potentiometers of PIR. Turn all to the left:
 * Sensitiveness: 3m
 * Duration HIGH: 3s minimum
*/


#define LED 13
#define INPUT_PIR 12
#define BUZZER 11
#define INTERVAL 500
//==============================
//Bluetooth protocol related
//==============================
int incomingByte = 0;       // Store Received Data(byte)
String inputString = "";         // Store Received Data(String)
boolean newLineReceived = false; // Previous data end flag
boolean startBit  = false;  // Protocol start flag

//-----------------------------------
//LED
bool ledState = 0;
//-----------------------------------
//Alarm
bool firstAlarm = 0;
unsigned long previous_millis = 0;
//-----------------------------------
//general state
byte gState = 1;
//------------------------------

void setup() {
  Serial.begin(115200);  // Set Bluetooth baud rate. Must match the one of the module
  pinMode(LED, INPUT); //PIR
  digitalWrite(INPUT_PIR,LOW);
  pinMode(LED,OUTPUT); //LED
  digitalWrite(LED,LOW);
  pinMode(BUZZER, OUTPUT);//Buzzer
  digitalWrite(BUZZER, LOW);
}


void loop() {
  Bluetooth();
  runElection();
}


void PIR()
  {
    analogWrite(BUZZER,0); 
    digitalWrite(LED,LOW);
    if(digitalRead(INPUT_PIR))  //input to Arduino from PIR's output
    {
      analogWrite(BUZZER,127); //most audible sound
      digitalWrite(LED, HIGH);
      delay(200);
      analogWrite(BUZZER,0);  
      digitalWrite(LED,LOW);
      delay(200);
    }
  }


void Bluetooth(void)
{
  if (newLineReceived)
  {
    if (inputString[1] == 'S') //Stop
    {
      if (gState != 0) //if it is not stopped, 
      {
        gState = 0;
        shortBeep(80);
      } 
    }
       
    else if (inputString[1] == 'A') //Alarm
    {
      gState = 2; 
      firstAlarm = 1;
    }
    else if (inputString[1] == 'P') //PIR
    {
      shortBeep(20); 
      gState = 1;
    } 
    inputString = "";   // clear the string
    newLineReceived = false; 
  }
}

void Stop()
{
  digitalWrite(LED, LOW);
  digitalWrite(BUZZER, LOW);
}

void proveAlarm(){
  if (firstAlarm)
  {
    firstAlarm = 0;
    digitalWrite(LED, LOW); //LED off
    digitalWrite(BUZZER, LOW); //buzzer off
    delay(100); //in case alarm was sounding
    ledState = 0;
    previous_millis = 0;
  }
  unsigned long current_millis = millis();
  if(current_millis - previous_millis >= INTERVAL)
  {
    digitalWrite(LED, !ledState);
    analogWrite(BUZZER, 127 *(int)!ledState);
    previous_millis = current_millis;
    ledState = !ledState;
  }
}

void runElection()
{
  switch (gState)
  {
    analogWrite(BUZZER, 0); delay(1);
    case 1 : PIR(); break;
    case 0 : Stop(); break;
    case 2 : proveAlarm(); break;
    default: break;
  }
}

void shortBeep(byte number)
{
  for(byte i=0; i<= number; i++)
  {
    analogWrite(BUZZER, 127);
    delay(2);
    analogWrite(BUZZER, 0);
    delay(2);
  }
}



//Serial read data
void serialEvent()
{
  while (Serial.available())
  {
    incomingByte = Serial.read();   //One byte by one byte reads 
    if (incomingByte == '$')  // '$' means the start of packet.$#
    {
      startBit = true;
    }
    if (startBit == true)
    {
      inputString += (char) incomingByte;     // The received data constitutes a completed packet.
    }
    if (incomingByte == '#')    // '#' means the end of packet
    {
      newLineReceived = true;
      startBit = false;
    }
  }
}

Credits

Jose Romaní

Jose Romaní

5 projects • 0 followers
Industrial Technical Engineer in Electronics and Automation

Comments