Skip to content

Instantly share code, notes, and snippets.

@dyadica

dyadica/About Secret

Last active March 8, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyadica/f0555197e300f5f3b888 to your computer and use it in GitHub Desktop.
Save dyadica/f0555197e300f5f3b888 to your computer and use it in GitHub Desktop.
MonoBehaviour & external javascript function used to obtain data from particle (photon) cloud and return it to Unity
Photon and Unity Integration
The following gist contains a selection of code elements that can be used to provide a bridge between a Unity App and the Particle cloud. Full instructions can be found via: http://www.dyadica.co.uk/unity-and-the-particle-cloud/
GetPhotonData.js - This file contains a function that should be added to the unity webplayer html file. The function is called from within unity via PhotonCall.cs. The passed arguments include both the device id and access token needed to point at and get data from the particle.io cloud. If PhotonCall.cs is attached to a gameobject other than the Main Camera then you will need to update the gameobject reference in each SendMessage command to reflect.
PhotonCall.cs - This file is a MonoBehaviour which contains functions to both send requests and receive data from the particle.io cloud. You need to update both the DeviceID and AccessToken variables to reflect those of your Photon and account. The file also contains several Text references which you will need to assign to UI Text objects for the data to display.
function GetPhotonData( arg1, arg2, arg3)
{
var deviceID = arg1;
var accessToken = arg2;
var sensor = arg3;
switch(sensor)
{
case 0:
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "FullData" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
// var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdateDataArray", js.result);
});
break;
case 1:
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "Humidity" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdateHumidity", js.result);
});
break;
case 2:
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "Fahrenheit" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdateFahrenheit", js.result);
});
break;
case 3:
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "Celcius" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdateCelcius", js.result);
});
break;
case 4:
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "Pressure" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdatePressure", js.result);
});
break;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class PhotonCall : MonoBehaviour
{
public string DeviceID = "<Your Device ID>";
public string AccessToken = "<Your Access Token>";
public Text DataDisplayAll;
public Text DataDisplayF;
public Text DataDisplayC;
public Text DataDisplayP;
public Text DataDisplayH;
public float timeGap = 2f;
private float humidity;
private float fahrenheit;
private float celcius;
private float pressure;
public enum Sensors
{
All, Humidity, Fahrenheit, Celcius, Pressure
}
public Sensors Sensor = Sensors.All;
void Start()
{
}
void UpdateHumidity(string json)
{
if (DataDisplayH != null)
DataDisplayH.text = json;
humidity = float.Parse(json);
}
void UpdateFahrenheit(string json)
{
if (DataDisplayF != null)
DataDisplayF.text = json;
fahrenheit = float.Parse(json);
}
void UpdateCelcius(string json)
{
if (DataDisplayC != null)
DataDisplayC.text = json;
celcius = float.Parse(json);
}
void UpdatePressure(string json)
{
if (DataDisplayP != null)
DataDisplayP.text = json;
pressure = float.Parse(json);
}
void UpdateDataArray(string data)
{
if (DataDisplayAll != null)
DataDisplayAll.text = "Raw: " + data;
string[] sData = data.Split(',');
if (sData.Length == 4)
{
humidity = float.Parse(sData[0]);
fahrenheit = float.Parse(sData[1]);
celcius = float.Parse(sData[2]);
pressure = float.Parse(sData[3]);
if (DataDisplayH != null)
DataDisplayH.text = "Humidity: " + humidity.ToString();
if (DataDisplayF != null)
DataDisplayF.text = "Fahrenheit: " + fahrenheit.ToString();
if (DataDisplayC != null)
DataDisplayC.text = "Celcius: " + celcius.ToString();
if (DataDisplayP != null)
DataDisplayP.text = "Pressure: " + pressure.ToString();
}
}
void Update()
{
timeGap -= Time.deltaTime;
if (timeGap < 0)
{
timeGap = 2;
Application.ExternalCall("GetPhotonData", DeviceID, AccessToken, (int)Sensor);
}
}
public void GetSensorData(Sensors sensor)
{
Application.ExternalCall("GetPhotonData", DeviceID, AccessToken, (int)sensor);
}
}
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "FullData" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
u.getUnity().SendMessage("Main Camera", "UpdateDataArray", js.result);
});
requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + "Humidity" + "/?access_token=" + accessToken;
$.getJSON(requestURL, function(js) {
var fullData = JSON.stringify(js.result);
u.getUnity().SendMessage("Main Camera", "UpdateHumidity", js.result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment