Ron Dagdag
Published © GPL3+

Cloud-Controlled Augmented Reality LightSaber

Control your LightSaber with Littlebits! Mixing real world gadgets with virtual reality tools.

IntermediateFull instructions provided3,838

Things used in this project

Hardware components

Gizmos & Gadgets
littleBits Gizmos & Gadgets
Littlebits Cloudbit
×1
Android device
Android device
×1
Littlebits Cloudbit
×1

Software apps and online services

littleBits Cloudbit
Unity
Unity

Story

Read more

Code

LittlebitsWebSocket.cs

C#
This is the code I used to connect Unity3d to Littlebits cloud via websockets
using UnityEngine;
using System.Collections;
using System;
using System.Security.Policy;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WebSocketSharp;
using WebSocketSharp.Net;

public class LittlebitsWebSocket : MonoBehaviour {

	// receiving Thread
	Thread receiveThread;

	public string access_token = "<Access Token Here>";
	public string device_id = "<Device ID Here>";
	private string uri = "wss://api-stream.littlebitscloud.cc/primus/?access_token=";
	public int value;
	WebSocket ws;
	GameObject energyLight;
	EnergyScript energyScript;
	// Use this for initialization
	void Start () {
		uri = uri + access_token; 

		energyLight = GameObject.FindGameObjectWithTag ("Light");
		energyScript = energyLight.GetComponent<EnergyScript> ();
		init ();
	}
	
	// Update is called once per frame
	void Update () {

	}

	private void init()
	{
		receiveThread = new Thread(
			new ThreadStart(ReceiveData));
		receiveThread.IsBackground = true;
		receiveThread.Start();
	}

	private  void ReceiveData()
	{
		ws = new WebSocket (uri);
		{
			// Set the WebSocket events. 

//connect to littlebits and subscribe to a device
			ws.OnOpen += (sender, e) =>
			{
				var data = new JObject();
				data.Add("name","subscribe");
				var args1 = new JObject();
				args1.Add("device_id",device_id);
				data.Add("args",args1);
				
				ws.Send(data.ToString());
			};
			
// when we receive a message from littlebits, we need to parse the data
			ws.OnMessage += (sender, e) =>
			{
				var data = e.Data.Substring(1,e.Data.Length -2);
				var newData = data.Replace("\\", "");
				JObject json = JObject.Parse(newData);
				var payload = json["payload"];
				if (payload != null)
				{
					var newValue = payload["percent"].Value<int>();
					//if (value != newValue)
					{
						energyScript.LocalVelocity_Y = value / 10;
					}
					value = newValue;
					Debug.Log ("Cloudbit:" + value);
				}
			};
			
			ws.OnError += (sender, e) =>
				Debug.Log ("Error >>" + e.Message);

			ws.OnClose += (sender, e) =>
				Debug.Log(String.Format("WebSocket Close ({0})", e.Code));
			
			#if DEBUG
			// To change the logging level.
			ws.Log.Level = LogLevel.Trace;
			
			// To change the wait time for the response to the Ping or Close.
			ws.WaitTime = TimeSpan.FromSeconds(10);
			
			// To emit a WebSocket.OnMessage event when receives a ping.
			ws.EmitOnPing = true;
#endif
		}
		ws.ConnectAsync ();
	}
}

Credits

Ron Dagdag

Ron Dagdag

47 projects • 438 followers
Microsoft MVP award / Lead Software Engineer / Augmented Reality. Developer Passionate to learn about Robotics, VR, AR, ML, IOT

Comments