Connecting XR to a Power Relay

varenyaraj23 edited this page Jul 2, 2018 · 2 revisions

This is a tutorial on how to connect mixed reality world with the physical world and how to turn a device on and off when there is certain interaction or action happening in the Mixed Reality world. In this example, we are trying to recreate something that is generally seen in all horror movies, that when a ghost comes to the place, the lights flicker on and off.

Hardware Needed

1x Rasberry Pi

1x PowerSwitch Tail II

1x Lamp

1x Small Screwdriver (for the PowerSwitch Tail screw terminals)

Part 1 - Physical World

Connecting Raspberry to Spacebrew

For that, you will need to first download PySpacebrew library into your project folder in Raspberry Pi. The code for the Raspberry Pi to initialize the objects (Raspberry Pi and Spacebrew) will be as follows.

import sys
import time
from pySpacebrew.spacebrew import Spacebrew
import RPi.GPIO as GPIO


#Spacebrew
# listen for light changes - bool
brew = Spacebrew("MRSwitch", description="Control light using mixed reality with PowerSwitch Tail 2",  server="192.168.1.165", port=9000) #Spacebrew Server IP Address
brew.addSubscriber("flipLight", "boolean") #boolean to turn switch on and off
# brew.addPublisher("buttonPress", "boolean")

#Raspberry Pi
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

#connect to server
try:
    brew.start()
    
finally:
    GPIO.cleanup()
    brew.stop()

4-relays-figure-16

Connect PowerSwitch Tail II to Pi

We will be connecting PowerSwitch Tail II to the Raspberry Pi on the Pin 24.

You can read more about how it works and its Datasheet at Adafruit Website

And the code to set the pin 24 as output is as below

Paste this in #Raspberry Pi Section:

powerSwitchPin = 24
GPIO.setup(powerSwitchPin, GPIO.OUT)
GPIO.output(powerSwitchPin, GPIO.LOW) #To set output as low initially

Switch On off using Spacebrew Data

Now we need to connect the spacebrew data to turn the switch on and off.

We subscribe to 'flipLight' Event in spacebrew, and this will receive true or false as an argument.

So we will need to create a function to store that value in some global value from which we can turn the light on or off.

img_0450

#Raspberry Pi
isLightOn = False #A global variable to call later for switching the lamp on or off
CHECK_FREQ = 0.1 #Sleep time for the loop

#function to handle the spacebrew data
def handleBoolean(value):
    print("Received: "+str(value))
    global isLightOn
    if (value == 'true' or str(value) == 'True'):
        isLightOn = True
    if (value == 'false' or str(value)== 'False'):
        isLightOn = False    

#Suscribing to FlipLight and calling handleBoolean when we receive any data.
brew.subscribe("flipLight", handleBoolean)


#connect to server code
try:
    brew.start()
    print("Press Ctrl-C to quit.")
    while True:
        GPIO.output(powerSwitchPin, isLightOn)
        time.sleep(CHECK_FREQ)
    
finally:
    GPIO.cleanup()
    brew.stop()

Now whenever we will receive flipLight as true, handleboolean will set isLightOn as true.

Then the GPIO.output will be set true for powerSwitchPin which will turn on the lamp.

Your final code should look something like this:

import sys
import time
from pySpacebrew.spacebrew import Spacebrew
import RPi.GPIO as GPIO


#Spacebrew
# listen for light changes - bool
brew = Spacebrew("MRSwitch", description="Control light using mixed reality with PowerSwitch Tail 2",  server="192.168.1.165", port=9000) #Spacebrew Server IP Address
brew.addSubscriber("flipLight", "boolean") #boolean to turn switch on and off


#Raspberry Pi
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
powerSwitchPin = 24
GPIO.setup(powerSwitchPin, GPIO.OUT)
GPIO.output(powerSwitchPin, GPIO.LOW)
isLightOn = False


CHECK_FREQ = 0.1 #Sleep time for the loop


def handleBoolean(value):
    print("Received: "+str(value))
    global isLightOn
    if (value == 'true' or str(value) == 'True'):
        isLightOn = True
    if (value == 'false' or str(value)== 'False'):
        isLightOn = False    

brew.subscribe("flipLight", handleBoolean)

try:
    brew.start()
    print("Press Ctrl-C to quit.")
    while True:
        GPIO.output(powerSwitchPin, isLightOn)
        time.sleep(CHECK_FREQ)
    
finally:
    GPIO.cleanup()
    brew.stop()

Part 2 - Mixed Reality World

Using Unity 3D

We used Google Poly to import 3D models into Unity. Once that was done, we added a script to receive the input from Spacebrew which would detect the specific Gameobject layer inside the 3D model and that same layer turns off/on based on the randomised data sent from Spacebrew. The code fro that is written below:

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

public class lightONOFF : MonoBehaviour {

	// Use this for initialization
	SpacebrewEvents sbEvents;
	GameObject light;

	void Start () {
		GameObject go = GameObject.Find ("SpacebrewObject");
		sbEvents = go.GetComponent <SpacebrewEvents> ();
		light = GameObject.Find("ScaryImage/InnerDetails/node_Hypercolor_e8ef32b1-baa8-460a-9c2c-9cf8506794f5_0_0");
		StartCoroutine(timeFunctionforLightOnOff());
	}

    private IEnumerator timeFunctionforLightOnOff()
    {
        while(true){
                        sbEvents.sendMessageOverSpaceBrew("buttonPress","boolean","true");
			yield return new WaitForSecondsRealtime(0.2f);
			light.GetComponent<Renderer>().enabled = true;
            System.Random r = new System.Random();
			float rand = r.Next(5,7);
			yield return new WaitForSecondsRealtime(rand);

			sbEvents.sendMessageOverSpaceBrew("buttonPress","boolean","false");
			yield return new WaitForSecondsRealtime(0.2f);
			light.GetComponent<Renderer>().enabled = false;
			float rand1 = r.Next(5,7);
			yield return new WaitForSecondsRealtime(rand1);
			// yield return null;
		}
		
    }

}

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.