Augmented Reality based IoT Switch using Unity and Vuforia.

by Jun 13, 2020Articles

Introduction:

In this project, we will use unity along with Vuforia to Make an AR-based IoT Switch which can control any device connected to a NodeMCU through an HTTP request.

NOTE:This project will make use of the Arduino Webserver project which I build in one of my earlier posts.(Refer here)

Demo of the project:

The buttons On  and OFF are being added to the bulb image  in the android app.

Structure/Work Flow:

  • STEP1: Setup Your NodeMCU along with a LED indicating any device.
  • STEP2: Download and install Unity Hub from Here. Also, install unity latest version from unity hub.
  • STEP3: Download Vuforia Engine for unity from Here.
  • STEP4: Setup Target Image which will be used to Augment buttons.
  • STEP5: Add buttons over the target image in unity.
  • STEP6: Write a script for the buttons to create an HTTP request.
  • STEP7: Add script to Autofocus AR Camera.
  • STEP8: Build a .apk file and test it on any Android Device.

Hardware and Software Requirement:

  • Nodemcu
  • LED with Resistor
  • Unity
  • Vuforia

STEP1: Setup Your NodeMCU along with a LED indicating any device.

First we need to setup our target device to which we will connect our devices. Follow this post to make your system using a NodeMCU.

In this post we define HTTP request Urls to turn the LED ON and OFF. We will use the same urls in this project with Our Buttons. to turn Device ON and OFF.

AT the end of this step we have Two URLS to turn on and Off Our device.
1.To Turn Device ON:  http://192.168.31.88/led1on
2.To Turn Device OFF: http://192.168.31.88/led1off

STEP2: Download and install Unity Hub.Also, install unity latest version from unity hub.

  • Once Unity Hub is installed open it and install unity latest version with android build support to build apk files.
  • Create a new project :
  • Give a project name ,select 3D template option and Click on CREATE
  • You will Get a screen like this.
  • Goto file and save scene with desired name (say “IoT Switch”).

STEP3: Download Vuforia Engine for unity from Here.

  • Login and agree the terms.
  • Double click the downloaded file to open in unity
  • Click on Import .

Now your unity Software is ready along with Vuforia Engine .

STEP4: Setup Target Image

Next, we will need a target image . Whenever camera will detect this image it will Augment our ON/OFF buton over this image to control any Device.

We Will use the following image as our target image. You can use any image of your choice.(make sure to fill the entire image area)

  • Give a name .Select Type: Device and click on Create:
  • Click on Add target and Browse Your target image as shown.
  • Give a Width of your choice (say 4) and a name (say bulb).Finally Click on Add.
  • The Better the Star Rating You get the better will be the results.(In my case i got 4 stars for the above image).
  • Click on download Database(All). Select Unity Editor and Download.
  • Double click the Package Downloaded and import in unity Just like we did Previously.

STEP5: Add buttons over the target image in unity.

  • First Open the scene and Delete the main camera.(simply select it and press delete key.)
  • Add AR Camera from Vuforia Engine .
  • Next we will add licence key. Goto Licence Manager Tab and click on Get Development Key.
  • Give a name ,Tick the checkbox and Confirm
  • Next click on the New Licence.
  • Copy the licence
  • open unity ,select AR camera and in the inspector panel click on Open Vuforia Engine Config.
  • Paste the licence key you copied in earlier step.
  • Next add image target from Vuforia Engine.
  • Next , Select Image target and in the inspector panel on the right side of the screen load the image from database.
  • once this is done Our screen will look Something like this
  • Next Add a button from UI . Right click on ImageTarget and select button from UI.
  • Next click on canvas and Change Render Mode to World Space.
  • Also set Event Camera as AR camera.(Simply drag and drop)
  • Also Reset the canvas position to (0,0,0). so that the buttons will be at same position as the target image.
  • Next Adjust the button on top of the Target image. (Rotate and resize to fit like this)
  • Right click and duplicate the button
  • Rename the Buttons and Also its text as shown to ON and OFF.
  • Now if you run and bring the target image in front of the Computer camera You will see the buttons on top of it.

STEP6: Write a script for the buttons to create an HTTP request.

So Now finally we need some code which will create a http request to our Webserver whenever we press the On /OFF buttons.

  • First inside assets Folder we create a new folder “Scripts” and inside this folder we create a new C# file by name “ClickUrl”.

Add the following code to the C# file.

  • First  add following namespaces 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
  • Next inside the ClickUrl class, we create a public string URL to hold the URL for each button.
  • Also, create a Function named Open () to Create the HTTP Get Request. We will Call this open() function on click action of the button.
  • We create a Coroutine and create Get Request and wait until it returns.
public class ClickUrl : MonoBehaviour
{
    public string url;
    public void open()
    {
        StartCoroutine(GetRequest(url));
    }

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

        }
    }
}
  • Save the code and Come back to unity screen.
  • Now attach this script to both the buttons(On,Off).(Simply drag and drop.)

Inside the Inspector Panel you can see that the Script component is added to each button element. .

  • Also a URL field is present(which we made public in our script). Type in the Required URL in here.
1.To Turn Device ON:  http://192.168.31.88/led1on
2.To Turn Device OFF: http://192.168.31.88/led1off
  • Next we need to define the click action.
  • Select any button and click on the + button under On Click():
  • Next drag and drop the Button_off Game Object from Hierarchy to the On Click ().
  • Finally Select the Open() function we defined in our ClickUrl script as shown
Note:Repeat the above steps for the other button (Button_ON) as well to call Open() function.

With this our Buttons are ready and functional to trigger the HTTP request.

STEP7: Add script to Autofocus AR Camera.

The progress up to STEP 6 works properly with the computer camera inside the Unity environment. But when we build apk file and run on android the camera is unable to focus and therefore fails to recognize the features on our target image.

To solve this problem we add a new script to the AR camera element so that it is able to autofocus.

Create a new script inside Assets>Scripts named “FocusCamera”.

Add the following code to the above script.

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

public class FocusCamera : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        bool focusModeSet = CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        if (!focusModeSet)
        {
            Debug.Log("Failed to set the focus mode to continusauto (unsupported mode).");
        }
    }

}

  • Attach this FocusCamera Script to the AR Camera Element (Simply Drag and drop)

STEP8: Build a .apk file and test it on any Android Device.

Finally It is time to build our android app

  • Goto File > Build Settings
  • Switch to Android. [ Click on Switch Platform]
  • Click on Player settings and add the details you want. App icon can also be changed from here.
  • Finally click on build.
  • Give a name to your Apk and save.
  • It will start compiling and finish after some time.
  • After some time you will have your apk file.

Transfer and install this apk file on any android device to see it in action.

That’s All We need to build an AR based ioT switch.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

5 Booming Technologies in IoT to watch out for in 2022

5 Booming Technologies in IoT to watch out for in 2022

Introduction Internet of Things - IoT is one of the industries that has experienced an exponential rise in the past few years. With technology on the rise, we expect this field to grow even further in the coming years. It is one of the most important technologies...

Furtherance to SIM Technology: eSIM and embedded SIM

Furtherance to SIM Technology: eSIM and embedded SIM

eSIM (electronic SIM) and embedded SIM are two different terms. While both are under development and can be incorporated in IoT. They will result in more efficient SIM technology combined with the fast-growing and in-demand 5G network. Before going into the details...

The Internet of Nano Things (IoNT): Evolution of a new era

The Internet of Nano Things (IoNT): Evolution of a new era

Internet of Nano Things The internet of nano-things (IoNT) is a network that connects a collection of very small devices to transport data. The internet of nano-things is similar to the internet of things. The only difference is that the devices present inside it are...

10 Innovations in IoT Using 5G

10 Innovations in IoT Using 5G

5G usage cases typically depend on the improved speed and stability of 5G, as well as the reduced latency it provides, and they have the potential to disrupt both conventional and digital industries. And, in the coming months, years, and decades, 5G technology will...

What is IoRT(Internet of Robotic Things)

What is IoRT(Internet of Robotic Things)

The IoT and robotics, two different fields, are coming together to create IoRT (Internet of Robotic Things). The IoRT is a concept in which intelligent devices can monitor the events happening around them, fuse their sensor data, use local and distributed intelligence...

Discover the Top 5 proven Use cases of IoT data analytics

Discover the Top 5 proven Use cases of IoT data analytics

Billions of connected IoT devices are generating a massive amount of data every second. Meanwhile, as the IoT is booming this data generation has exponential growth. This data needs to be analyzed in order to retrieve insights out of this data. Further, these insights...

Data Analysis role in IoT

Data Analysis role in IoT

Before diving into Data analysis role in IoT, let us first understand what data analysis exactly mean What is Data Analysis? According to Wikipedia, Data analysis is a process of...

What is the future of IoT?

What is the future of IoT?

IoT or the Internet of Things describes the network of physical objects—“things”—that are embedded with sensors, software, and other technologies for the purpose of connecting and exchanging data with other devices and systems over the Internet. The definition of...

IoT Security Solutions

IoT Security Solutions

Introduction IoT is one of the emerging technology. Moreover, this has its own risks and rewards. IoT devices sure make our lives simpler and automate a lot of processes. By now there are billions of IoT devices which include Smart TVs, Smart Refrigerators, Smart...

The relation between Embedded Systems and IoT

The relation between Embedded Systems and IoT

Embedded systems are the major part of our technological advances, found in everyday items such as – microwave oven, washing machine, remote control, RFID tags, routers, modems, PDAs, mobile phones etc. However, we’re in a new era of internet-based...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....