amrmostaafaa
Published © CERN-OHL

How to Build a DIY Arduino-Based Smart Home Hub with 1Sheeld

Arduino-based smart home hub allows you to control your devices remotely from outside and monitor your home environment from your phone.

IntermediateFull instructions provided51,453
How to Build a DIY Arduino-Based Smart Home Hub with 1Sheeld

Things used in this project

Hardware components

1Sheeld
1Sheeld
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Grove - Gas Sensor(MQ2)
Seeed Studio Grove - Gas Sensor(MQ2)
×1
Relay Module (Generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Servo Module (Generic)
×1
Slide Switch
Slide Switch
×1
18650 battery
×2
18650 dual battery box
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Doom push button
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×19
Male/Male Jumper Wires
×10
IR transmitter diode
×1
IR receiver module
×1
iPhone
Apple iPhone
×1
Android device
Android device
×1
Arduino Mega 2560
Arduino Mega 2560
×1

Software apps and online services

1Sheeld iOS/Android App

Hand tools and fabrication machines

siccors
glue gun
tape
cutter knife
Ruller

Story

Read more

Custom parts and enclosures

3D printed 4 parts

Schematics

Schematic

Code

Project code

Arduino
/*
  Arduino IoT Quality Project using IoT Shield from 1Sheeld

  OPTIONAL:
  To reduce the library compiled size and limit its memory usage, you
  can specify which shields you want to include in your sketch by
  defining CUSTOM_SETTINGS and the shields respective INCLUDE_ define.
*/

/* Include all required shields */
#define CUSTOM_SETTINGS
#define INCLUDE_IOT_SHIELD
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_EMAIL_SHIELD
#define INCLUDE_CAMERA_SHIELD
#define INCLUDE_BUZZER_SHIELD
#define INCLUDE_FINGERPRINT_SCANNER_SHIELD
#define INCLUDE_TEXT_TO_SPEECH_SHIELD
#define INCLUDE_MUSIC_PLAYER_SHIELD
#define INCLUDE_VOICE_RECOGNIZER_SHIELD

/* Define the devices signal pin */
#define camServoPin             3
#define light_pin               8
#define gas_switch_pin          21
#define gas_switch_led_pin      11
#define test_led                7
#define trigPin                 4
#define echoPin                 5
#define ir_led                  9

/* Include 1Sheeld library. */
#include <OneSheeld.h>

/* Include IR library */
#include <IRremote.h>

/* Include servo motor library */
#include <Servo.h>

/* Include the dht11 sensor library */
#include "DHT.h"

/* Define the sensors signal pins */
#define TEMP_PIN  A0
#define GAS_PIN  A1

/* Initia servo objects */
Servo CamServo;

/* Initia IR object */
IRsend irsend;

/* Crea an instance of the dht11 */
dht DHT;

/* door status variables to prevent multiple publication */
bool published_door_status_closed = false;
bool published_door_status_opened = false;

/* Variable to save the measured distance from ultrasonic */
int distance;

/* 38kHz carrier frequency for the NEC protocol */
int khz = 38;

// AC on/off at mp 23
unsigned int power[] = {4712, 2580, 384, 376, 380, 952, 380, 976, 356, 396, 360, 976, 356, 376, 380, 396, 356, 396, 360, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 396, 360, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 976, 356, 376, 380, 396, 356, 400, 356, 396, 356, 976, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 396, 360, 976, 356, 396, 356, 400, 356, 396, 360, 972, 360, 396, 360, 396, 356, 396, 356, 976, 360, 972, 360, 396, 356, 400, 356, 396, 356, 976, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 976, 360, 972, 360, 976, 356, 396, 360, 956, 356, 20116, 4676, 6568, 9276, 5012, 384, 396, 356, 396, 360, 396, 356, 400, 356, 396, 360, 396, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 976, 356, 976, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 976, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 356, 400, 356, 396, 360, 396, 356, 400, 356, 396, 356, 396, 360, 396, 360, 396, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 976, 356, 976, 356, 976, 356, 380, 360, 20116, 4672};

// AC mp+ from 23 to 24
unsigned int temp24[] = {4716, 2604, 360, 396, 356, 976, 356, 976, 360, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 396, 356, 976, 360, 396, 356, 396, 360, 396, 356, 976, 360, 396, 356, 396, 360, 972, 360, 396, 356, 980, 352, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 976, 356, 400, 356, 396, 356, 976, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 396, 360, 976, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 356, 976, 360, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 976, 356, 396, 356, 380, 360, 20104, 4684, 6596, 9276, 5012, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 400, 356, 396, 356, 396, 356, 400, 356, 396, 360, 976, 356, 976, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 400, 356, 396, 356, 400, 352, 400, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 400, 352, 400, 356, 400, 352, 400, 356, 400, 356, 396, 356, 400, 352, 400, 356, 980, 352, 980, 352, 980, 356, 380, 356, 20104, 4684};

// AC mp+ from 24 to 25
unsigned int temp25[] = {4716, 2604, 356, 400, 356, 976, 356, 976, 360, 396, 356, 976, 356, 396, 360, 396, 356, 400, 356, 396, 356, 976, 360, 396, 356, 396, 360, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 396, 356, 976, 360, 396, 356, 396, 360, 396, 356, 976, 360, 396, 356, 396, 360, 396, 356, 396, 360, 972, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 396, 356, 976, 356, 400, 356, 396, 356, 400, 356, 976, 356, 396, 360, 396, 356, 396, 360, 976, 356, 396, 360, 396, 356, 396, 356, 976, 360, 396, 356, 976, 356, 400, 356, 396, 360, 972, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 976, 356, 396, 360, 976, 356, 380, 356, 20108, 4684, 6592, 9276, 5012, 356, 396, 360, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 396, 360, 976, 356, 976, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 976, 356, 396, 356, 396, 360, 396, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 356, 396, 360, 396, 360, 396, 356, 396, 356, 400, 356, 396, 356, 400, 356, 396, 360, 972, 360, 976, 356, 976, 356, 380, 356, 20080, 4712};

/* auto optimization variable */
bool isAutoOptimize = false;

/* temp24 and temp25 checking variables */
bool temp24_isSent = false;
bool temp25_isSent = false;

/* sensor variabes */
int temp;
int gas_degree;
bool gas_HighLevelWarning = false;
bool gas_emailSent = false;
bool alarm_isPressed = false;
bool interruptPin_isPressed = false;
/* required variables for the publishing function */
char tempChar[4];
char gas_degree_char[4];

/* time relad variables */
bool isAuthorized = true;
unsigned long start_time;
unsigned long time_passed;

unsigned long ac_start_time;
unsigned long ac_time_passed;

/* broker instance security details */
const char * host_name = "m12.cloudmqtt.com";
const char * user = "azlckshz";
const char * password = "aoRcZqKQV0Kf";
const int port = 11911;

/* The topics*/
const char * ultrasonic = "ultrasonic";
const char * cam_dir = "cam/direction";
const char * cam_cap = "cam/capture";
const char * light = "light";
const char * temperature = "temp";
const char * gas = "gas";
const char * speak = "speak";
const char * ac = "ac";
const char * ac_auto = "auto";

byte interruptPin = gas_switch_pin;
void setup()
{
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), interruptPin_isr, FALLING);
  // pinMode(gas_switch_pin , INPUT_PULLUP);
  pinMode(gas_switch_led_pin , OUTPUT);
  pinMode(light_pin , OUTPUT);
  pinMode(test_led , OUTPUT);
  pinMode(trigPin, OUTPUT);     // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);      // Sets the echoPin as an Input
  /* Configure Arduino to use this pin as the servo signal pin */
  CamServo.attach(camServoPin);

  /* Start communication. */
  OneSheeld.begin();

  /* Disconnect from broker. */
  IOT.disconnect();

  /* Reset all connection variables to default */
  IOT.resetConnectionParametersToDefaults();

  /* Connect to mosquitto's public broker. */
  IOT.connect(host_name, user, password, port);

  /* Subscribe to new messages. */
  IOT.setOnNewMessage(&newMessage);

  /* Subscribe to connnection status callback. */
  IOT.setOnConnectionStatusChange(&connectionStatus);

  /* Subscribe to error callback. */
  IOT.setOnError(&error);

  /* Some time for app to connect. */
  delay(3000);

  /* Subscribions to topics */
  IOT.subscribe(ultrasonic);
  IOT.subscribe(cam_dir);
  IOT.subscribe(cam_cap);
  IOT.subscribe(light);
  IOT.subscribe(speak);
  IOT.subscribe(ac);
  IOT.subscribe(ac_auto);

  /* publish ready once light is connecd */
  IOT.publish("status", "ready", QOS_0);
  
  /* Start servo at the 90 degree angle */
  CamServo.write(90);

}

void loop()
{
  /************************************************* Ultrasonic Sensor *****************************************************/
  /* Get the current distance from the ultrasonic */
  distance = getUltrasonicDistance();
  //terrminal.println("");
  //Terminal.print("distance:");
  //Terminal.println(distance);
  if ( (distance < 30) && (distance > 0) && (isAuthorized == true) )
  {
    isAuthorized = false;
    start_time = millis();
    TextToSpeech.say("please enter your fingerprint");
    OneSheeld.delay(1400);

    if(published_door_status_opened == false)
    {
      /* Publish door status as opened */
      IOT.publish(ultrasonic, "your door is opened", QOS_0);
      
      /* ensure publisjing one time only */
      published_door_status_opened = true;
    }
  }
  else
  {
    if(published_door_status_closed == false)
    {
      /* Publish door status as opened */
      IOT.publish(ultrasonic, "your door is closed", QOS_0);
      
      /* ensure publisjing one time only */
      published_door_status_closed = true;
    }
  }
  /************************************************* fingerprint security timer *****************************************************/
  if ( isAuthorized == false )
  {
    time_passed = (unsigned long) (millis() - start_time);
    if ( FingerprintScanner.isNewFingerScanned() && FingerprintScanner.isVerified())
    {
      isAuthorized = true;
      TextToSpeech.say("fingerprint matched");
      OneSheeld.delay(1100);
      TextToSpeech.say("welcome home");
      OneSheeld.delay(1200);

      /* allow publishing again */
      published_door_status_closed = false;
      published_door_status_opened = false;
    }

    if ( time_passed >= 30000 )
    {
      isAuthorized = true;
      TextToSpeech.say("you are not authorized");
      OneSheeld.delay(1500);

      /* Take a photo for that stranger using the phone's front camera */
      Camera.frontCapture();

      /* delay for the camera */
      OneSheeld.delay(8000);

      /* Send an email with the last picture in OneSheeld folder. */
      Email.attachLastPicture("amr.mostaafaa@gmail.com", "Security Alert!", "Hi, A stranger has entered your home and I got a photo of him. Check it out", 1);

      /* delay for the email */
      OneSheeld.delay(3000);
    }
  }

  /************************************************* air conditioner timer *****************************************************/
    ac_time_passed = (unsigned long) (millis() - ac_start_time);
    if( isAutoOptimize )
    {
        /* increase the AC temperature from 23 to 24C if the time passed since on is >= 2sec */
        if ( ac_time_passed >= (60000 * 2) && (temp24_isSent == false) )
        {
          /* increase temp to 24 */
          irsend.sendRaw(temp24, sizeof(power) / sizeof(temp24[0]), khz); //No the approach used to automatically calculate the size of the array.
          temp24_isSent = true;
        }

        /* increase the AC temperature from 24 to 25C if the time passed since on is >= 4sec */
        if ( ac_time_passed >= (60000 * 4) && (temp25_isSent == false))
        {
          /* increase temp to 25 */
          irsend.sendRaw(temp25, sizeof(power) / sizeof(temp25[0]), khz); //Note the approach used to automatically calculate the size of the array.
          temp25_isSent = true;

          /* turn off auto-optimization since it's completed */
          isAutoOptimize = false;
          
          /* publish that auto-optimization has completed and is off now */
          IOT.publish(ac_auto, "off", QOS_0);
        }
    }  
  /************************************************* voice recognition *************************************************************/
  //check if 1Sheeld's Arduino Voice Recognition Shield received a new command
  if (VoiceRecognition.isNewCommandReceived())
  {
    //Compare the last command received by the Arduino Voice Recognition Shield with the command "on"
    if (!strcmp("light on", VoiceRecognition.getLastCommand()))
    {
      //Then turn the light on
      digitalWrite(light_pin, HIGH);
    }

    //Compare the last command received by the Arduino Voice Recognition Shield with the command "off"
    else if (!strcmp("light off", VoiceRecognition.getLastCommand()))
    {
      //Then turn the light off
      digitalWrite(light_pin, LOW);
    }

    //Compare the last command received by the Arduino Voice Recognition Shield with the command "play music"
    else if (!strcmp("play music", VoiceRecognition.getLastCommand()))
    {
      // play the music.
      MusicPlayer.play(0);
      // Terminal.println("play music now");
    }

    //Compare the last command received by the Arduino Voice Recognition Shield with the command "pause music"
    else if (!strcmp("pause music", VoiceRecognition.getLastCommand()))
    {
      // pause the music.
      MusicPlayer.pause();
      // Terminal.println("pause music now");
    }
  }

  /********************************* Temperature & Gas sensors *************************************/
  /* Read DHT all data */
  DHT.read11(TEMP_PIN);

  /* Read temperature */
  temp = (int)DHT.temperature;

  /* read gas sensor */
  gas_degree = analogRead(GAS_PIN);

  /* Convert sensors integers to char arrays */
  itoa(temp, tempChar, 10);
  itoa(gas_degree, gas_degree_char, 10);

  /* Publish sensors' data */
  IOT.publish(temperature, tempChar, QOS_0);
  IOT.publish(gas, gas_degree_char, QOS_0);
  
  /************************************************ Gas alarm ****************************************************/
  /* check for gas dangerous level */
  if ( gas_degree > 150 )
  {
     /* blink the button led*/
     digitalWrite(gas_switch_led_pin , HIGH);
     OneSheeld.delay(500);
     digitalWrite(gas_switch_led_pin , LOW);
     gas_HighLevelWarning = true;
  }
  else
  {
    /* turn off button led*/
    digitalWrite(gas_switch_led_pin , LOW);
    
    gas_HighLevelWarning = false;
    alarm_isPressed = false;
    gas_emailSent = false;
  }

  
  if( (gas_HighLevelWarning == true) && ( alarm_isPressed == false ) )
  {
    /* Turn on the buzzer. */
    Buzzer.buzzOn();
    
    if(gas_emailSent == false)
    {
        /* Send an email with the last picture in OneSheeld folder. */
        Email.send("amr.mostaafaa@gmail.com", "Dangerous Gas Alert!", "Hi, There is a dangerous level of gas in your home! Please check your dashboard.");
    
        /* delay for the email */
        OneSheeld.delay(3000);
    
        /* prevent multiple sending*/
        gas_emailSent = true;
    }
  }

  if( (interruptPin_isPressed == true) && (alarm_isPressed == false) )
  {
    /* Turn off the buzzer. */
    Buzzer.buzzOff();
    alarm_isPressed = true;
    interruptPin_isPressed = false;
  }
}

/* ######################################################################################################### */
void newMessage(char * incomingTopic, char * payload, byte qos, bool retained)
{
/*************************************************************************************************************/
  /* check for the speak topic */
  if (!strcmp(speak, incomingTopic))
  {
    /* If message is speak */
    if (!strcmp("speak", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);
      
      /* start listening to commands */
      VoiceRecognition.start();
      
      // this is for testing
      digitalWrite(test_led , LOW);
    }
  }
/*************************************************************************************************************/
  /* check for the light topic */
  if (!strcmp(light, incomingTopic))
  {
    /* If message is on */
    if (!strcmp("on", payload))
    {

      // this is for testing
      digitalWrite(test_led , HIGH);
      
      /* turn on light */
      digitalWrite(light_pin , HIGH);

      // delay to notice the test led light
      OneSheeld.delay(200);
      
      // this is for testing
      digitalWrite(test_led , LOW);
    }

    /* If message is off */
    else if (!strcmp("off", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);
      
      /* turn off light */
      digitalWrite(light_pin , LOW);

      // delay to notice the test led light
      OneSheeld.delay(200);
      
      // this is for testing
      digitalWrite(test_led , LOW);
    }
  }
/*************************************************************************************************************/
  /* check for the cam_cap topic */
  if (!strcmp(cam_cap, incomingTopic))
  {
    /* If message is capture */
    if (!strcmp("capture", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);
      
      /* Take a photo for that stranger using the phone's front camera */
      Camera.frontCapture();

      /* delay for the camera */
      OneSheeld.delay(8000);

      /* Send an email with the last picture in OneSheeld folder. */
      Email.attachLastPicture("amr.mostaafaa@gmail.com", "Home Camera", "Hi, here is a photo of your home", 1);

      /* delay for the email */
      OneSheeld.delay(3000);

      // this is for testing
      digitalWrite(test_led , LOW);
    }
  }
/*************************************************************************************************************/
  /* check for the cam_dir topic */
  if (!strcmp(cam_dir, incomingTopic))
  {
    /* If message is 0 */
    if (!strcmp("0", payload))
    {
      /* Start servo at the 180 degree angle */
      servo_for_loop(CamServo.read() , 180);
    }

    /* If message is 1 */
    else if (!strcmp("1", payload))
    {

      /* Start servo at the 135 degree angle */
      servo_for_loop(CamServo.read() , 135);
    }

    /* If message is 1 */
    else if (!strcmp("2", payload))
    {
      /* Start servo at the 90 degree angle */
      servo_for_loop(CamServo.read() , 90);
    }

    /* If message is 3 */
    else if (!strcmp("3", payload))
    {
      /* Start servo at the 45 degree angle */
      servo_for_loop(CamServo.read() , 45);
    }

    /* If message is 4 */
    else if (!strcmp("4", payload))
    {
      /* Start servo at the 0 degree angle */
      servo_for_loop(CamServo.read() , 0);
    }
  }
/*************************************************************************************************************/
  /* check for the ac topic */
  if (!strcmp(ac, incomingTopic))
  {
    /* If message is on */
    if (!strcmp("on", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);

      /* turn on ac */
      irsend.sendRaw(power, sizeof(power) / sizeof(power[0]), khz); //Note the approach used to automatically calculate the size of the array.

      // this is for testing
      digitalWrite(test_led , LOW);
    }

    /* If message is off */
    else if (!strcmp("off", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);

      /* turn on ac */
      irsend.sendRaw(power, sizeof(power) / sizeof(power[0]), khz); //Note the approach used to automatically calculate the size of the array.

      // this is for testing
      digitalWrite(test_led , LOW);
    }
  }
/*************************************************************************************************************/
  /* check for the auto-optimization ac topic */
  if (!strcmp(ac_auto, incomingTopic))
  {
    /* If message is on */
    if (!strcmp("on", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);

      /* turn on ac */
      ac_start_time = millis();
      
      // turn on auto optimization
      isAutoOptimize = true;
      
      // this is for testing
      digitalWrite(test_led , LOW);
    }

    /* If message is off */
    else if (!strcmp("off", payload))
    {
      // this is for testing
      digitalWrite(test_led , HIGH);

      // turn off auto optimization
      isAutoOptimize = false;

      // this is for testing
      digitalWrite(test_led , LOW);
    }
  }
}
/*************************************************************************************************************/
void connectionStatus(byte statusCode)
{
  /* Check connection code and display. */
  switch (statusCode)
  {
    case CONNECTION_SUCCESSFUL: Terminal.println("CONNECTION_SUCCESSFUL"); break;
    case CONNECTION_FAILED: Terminal.println("CONNECTION_FAILED"); break;
    case CONNECTION_LOST: Terminal.println("CONNECTION_LOST"); break;
    case CONNECTION_LOST_RECONNECTING: Terminal.println("CONNECTION_LOST_RECONNECTING"); break;
    case NOT_CONNECTED_YET:  Terminal.println("NOT_CONNECTED_YET"); break;
    case MISSING_HOST: Terminal.println("MISSING_HOST"); break;
  }
}

/*************************************************************************************************************/
void error(byte errorCode)
{
  /* Check error code and display. */
  switch (errorCode)
  {
    case CONNECTION_REFUSED                    : Terminal.println("CONNECTION_REFUSED"); break;
    case ILLEGAL_MESSAGE_RECEIVED              : Terminal.println("ILLEGAL_MESSAGE_RECEIVED"); break;
    case DROPPING_OUT_GOING_MESSAGE            : Terminal.println("DROPPING_OUT_GOING_MESSAGE"); break;
    case ENCODER_NOT_READY                     : Terminal.println("ENCODER_NOT_READY"); break;
    case INVALID_CONNACK_RECEIVED              : Terminal.println("INVALID_CONNACK_RECEIVED"); break;
    case NO_CONNACK_RECEIVED                   : Terminal.println("NO_CONNACK_RECEIVED"); break;
    case CONNACK_UNACCEPTABLEP_ROTOCOLVERSION  : Terminal.println("CONNACK_UNACCEPTABLEP_ROTOCOLVERSION"); break;
    case CONNACK_IDENTIFIER_REJECTED           : Terminal.println("CONNACK_IDENTIFIER_REJECTED"); break;
    case CONNACK_SERVER_UNAVAILABLE            : Terminal.println("CONNACK_SERVER_UNAVAILABLE"); break;
    case CONNACK_AUTHENTICATION_FAILED         : Terminal.println("CONNACK_AUTHENTICATION_FAILED"); break;
    case CONNACK_NOT_AUTHORIZED                : Terminal.println("CONNACK_NOT_AUTHORIZED"); break;
    case CONNACK_RESERVED                      : Terminal.println("CONNACK_RESERVED"); break;
  }
}

/*************************************************************************************************************/
/* A function that makes the whole operation of the ultrasonic and returning the detected distance */
int getUltrasonicDistance(void)
{
  /* Variable to save the sound wave travel time in microseconds */
  long duration;

  /* Variable to save the detected distance in cm */
  int distanceReturned;

  /* Clears the trigPin */
  digitalWrite(trigPin, LOW);

  /* delay 2 micro seconds */
  delayMicroseconds(2);

  /* Sets the trigPin on HIGH state for 10 micro seconds */
  digitalWrite(trigPin, HIGH);

  /* delay 10 micro seconds */
  delayMicroseconds(10);

  /* Sets the trigPin on LOW state */
  digitalWrite(trigPin, LOW);

  /* Reads the echoPin, returns the sound wave travel time in microseconds */
  duration = pulseIn(echoPin, HIGH);


  /* Calculating the distance */
  distanceReturned = duration * 0.034 / 2;

  /* Returning the detected distance in cm */
  return distanceReturned;
}

/*************************************************************************************************************/
void servo_for_loop(int start_angle , int stop_angle)
{
  if( start_angle < stop_angle )
  {
    for( ; start_angle <= stop_angle ; start_angle++ )
    {
      CamServo.write(start_angle);
      delay(25);
    }
  }
  else if ( start_angle > stop_angle )
  {
    for( ; start_angle >= stop_angle ; start_angle-- )
    {
      CamServo.write(start_angle);
      delay(25);
    }
  }
}
/*************************************************************************************************************/
void interruptPin_isr()
{
  interruptPin_isPressed = true;
}

Credits

amrmostaafaa

amrmostaafaa

7 projects • 44 followers
Maker, Engineer, dreamer and passionate about technology.

Comments