fbpx

Virtual Buttons using Unity to Control your Home Appliances | AR+IoT

In the last project, we tried to control our home appliances using AR application with the help of our smart phone using Touchscreen that means we need to physically touch the screen to control our appliances. You can know more about that project by clicking here.

But now we are taking it a step ahead and this time we will be controlling the appliances using AR application with the help of virtual buttons. Just hover your hands over that button to control them.

Demo Video

Isn’t this exciting? Let’s dive into this and start making this project.

Components Required

For Hardware part of the project you’ll need 

  • ESP32 / NodeMCU board
  • 5V Relay Module 

For Software part you’ll need 

  • Blynk App
  • Unity Hub

Hardware Connection Diagram

IoT part of the project 

For the IoT part, I have used Blynk IoT Platform. In which, I just made a simple blynk project of adding a button in the dashboard which will send data 0 & 1 to virtual pin V1.

After that, we have made a code in which, if we receive the data 1 from virtual pin V1, we just send the signal high to digital pin of ESP32 board which ultimately turns on Relay. And as soon as we receive 0 the relay gets turned off. You can copy and paste the code from below

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
//#define BLYNK_TEMPLATE_ID   "YourTemplateID"


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define relay 15  // Relay pin

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  digitalWrite(relay,pinValue);
  // process received value
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(relay,OUTPUT);
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

Just change these three parameters in the code, to make it work on your side as well.

Till this step, we will be easily able to control the relay using the Blynk App. But we want to control that using API so that later on we can integrate it with AR. And the good news is that, Blynk do provide us APIs. 

So to send the data to virtual pin of our blynk device, the format of API is something like this.

http://blynk-cloud.com/auth_token/update/pin?value=value

For example, If I want to send data ‘1’ to virtual pin ‘V1’ of my blynk project, the API will be like this,

http://blynk-cloud.com/MY_AUTH_TOKEN/update/V1?value=1

So with this, we have successfully covered the IoT part of our project. Now let’s jump on to the AR part. 

AR part of project 

For AR, we will be using Unity Hub software on our computer. Just goto this link and download unity hub

While Setting up unity, you’ll need three things, one is the target image, second is On/Off Button Image and third is C# Script. All the things are provided below, so you can easily download and use them

Target Image
ON Image
OFF Image

C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
using UnityEngine.Networking;

public class First : MonoBehaviour
{
    public VirtualButtonBehaviour Vb_on;
    public VirtualButtonBehaviour Vb_off;
    public string url_on;
    public string url_off;

    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

        }
    }

    void Start()
    {
        Vb_on.RegisterOnButtonPressed(OnButtonPressed_on);

        Vb_off.RegisterOnButtonPressed(OnButtonPressed_off);
       
    }


    public void OnButtonPressed_on(VirtualButtonBehaviour Vb_on)
    {
        StartCoroutine(GetRequest(url_on));
        Debug.Log("LED IS ON");
    }

    public void OnButtonPressed_off(VirtualButtonBehaviour Vb_off)
    {
        StartCoroutine(GetRequest(url_off));
        Debug.Log("LED IS OFF");
    }

}

You need to save this code with the name, First

Full Tutorial

We have prepared a full detailed tutorial video for AR+IOT which is uploaded on YouTube. So in case you want to understand it in more clear manner, I’ll suggest you to go through this video and do let me know your feedback in comment section.