Rohan Barnwal
Published

Gesture and Voice-Controlled Home Automation System

Welcome to a project that redefines the way we interact with home automation! Using the power of Gesture and Voice Recognition Modules !!!!!

IntermediateFull instructions provided2 hours609
Gesture and Voice-Controlled Home Automation System

Things used in this project

Hardware components

DFRobot Gravity: GR10-30 Gesture Sensor
×1
DFRobot Gravity: Offline Language Learning Voice Recognition Sensor
×1
Arduino Mega 2560
Arduino Mega 2560
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connections

we have used 18 TX and 19 RX pin of Arduino Mega 2560

Code

Code

Arduino
#include "DFRobot_GR10_30.h"
#include "DFRobot_DF2301Q.h"

#if defined(ARDUINO_AVR_UNO) || defined(ESP8266)
#include <SoftwareSerial.h>
#endif


const int relayPins[] = {2, 3, 4, 5}; // Relay pins for relays 1, 2, 3, and 4
int relayStates[] = {0, 0, 0, 0};     // Array to track states of all relays (0 = OFF, 1 = ON)

// Gesture Sensor Initialization
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
SoftwareSerial softSerialGesture(/*rx =*/4, /*tx =*/5);
DFRobot_GR10_30 gr10_30(/*addr = */GR10_30_DEVICE_ADDR, /*pWire = */&Wire);
#else
DFRobot_GR10_30 gr10_30(/*addr = */GR10_30_DEVICE_ADDR, /*pWire = */&Wire);
#endif

// Voice Recognition Module Initialization
#if (defined(ARDUINO_AVR_UNO) || defined(ESP8266))
SoftwareSerial softSerialVoice(/*rx =*/6, /*tx =*/7);
DFRobot_DF2301Q_UART asr(/*softSerial =*/&softSerialVoice);
#elif defined(ESP32)
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1, /*rx =*/D3, /*tx =*/D2);
#else
DFRobot_DF2301Q_UART asr(/*hardSerial =*/&Serial1);
#endif

int S = 1; // Variable for relay selection, initial value 1
int W = 0; // Variable for ON/OFF control, initial value 0

void setup() {
  Serial.begin(115200);

  for (int i = 0; i < 4; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], HIGH); // Turn all relays off initially (default state)
  }

  

  // Gesture Sensor Initialization
  while (gr10_30.begin() != 0) {
    Serial.println("Gesture Sensor initialize failed!!");
    delay(1000);
  }
  Serial.println("Gesture Sensor initialize success!!");
  gr10_30.enGestures(GESTURE_UP | GESTURE_DOWN | GESTURE_LEFT | GESTURE_RIGHT);

  // Voice Recognition Module Initialization
  while (!(asr.begin())) {
    Serial.println("Voice Recognition Module initialize failed!!");
    delay(3000);
  }
  Serial.println("Voice Recognition Module initialize success!!");

  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_MUTE, 0);
  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_VOLUME, 7); // Set volume to 7
  asr.settingCMD(DF2301Q_UART_MSG_CMD_SET_WAKE_TIME, 20);
  
}

void loop() {
  // Gesture Handling
  if (gr10_30.getDataReady()) {
    uint16_t gestures = gr10_30.getGesturesState();

    if (gestures & GESTURE_RIGHT) {
      S++;
      if (S > 4) S = 1; // Reset S to 1 if it exceeds 4
      Serial.print("Relay selected: ");
      Serial.println(S);
    }

    if (gestures & GESTURE_LEFT) {
      S--;
      if (S < 1) S = 4; // Reset S to 4 if it goes below 1
      Serial.print("Relay selected: ");
      Serial.println(S);
    }

    if (gestures & GESTURE_UP) {
      W = 1; // Turn ON the relay corresponding to S
      relayStates[S - 1] = 1;
      Serial.print("Relay ");
      Serial.print(S);
      Serial.println(" ON");
    }

    if (gestures & GESTURE_DOWN) {
      W = 0; // Turn OFF the relay corresponding to S
      relayStates[S - 1] = 0;
      Serial.print("Relay ");
      Serial.print(S);
      Serial.println(" OFF");
    }

    // Update the states of all relays
    for (int i = 0; i < 4; i++) {
      digitalWrite(relayPins[i], relayStates[i] ? LOW : HIGH);
    }
  }

  // Voice Recognition Handling
  uint8_t CMDID = asr.getCMDID();
  
  switch (CMDID) {
    case 5:
      relayStates[0] = 1;
      Serial.println("Relay 1 ON");
      Serial.println(CMDID);
      break;
    case 6:
      relayStates[0] = 0;
      Serial.println("Relay 1 OFF");
      Serial.println(CMDID);
      break;
    case 7:
      relayStates[1] = 1;
      Serial.println("Relay 2 ON");
      Serial.println(CMDID);
      break;
    case 8:
      relayStates[1] = 0;
      Serial.println("Relay 2 OFF");
      Serial.println(CMDID);
      break;
    case 9:
      relayStates[2] = 1;
      Serial.println("Relay 3 ON");
      Serial.println(CMDID);
      break;
    case 10:
      relayStates[2] = 0;
      // Serial.println("Relay 3 OFF");
      Serial.println(CMDID);
      break;
    case 11:
      relayStates[3] = 1;
      Serial.println("Relay 4 ON");
      Serial.println(CMDID);
      break;
    case 12:
      relayStates[3] = 0;
      Serial.println("Relay 4 OFF");
      Serial.println(CMDID);
      break;
    default:
      if (CMDID != 0) {
        Serial.print("Unknown CMDID: ");
        Serial.println(CMDID);
      }
  }

  // Update relay states for voice commands
  for (int i = 0; i < 4; i++) {
    digitalWrite(relayPins[i], relayStates[i] ? LOW : HIGH);
  }

  delay(300);
}

Credits

Rohan Barnwal
23 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!
Contact

Comments

Please log in or sign up to comment.