Skip to content

Instantly share code, notes, and snippets.

@dyadica

dyadica/About Secret

Last active March 8, 2017 20:16
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/b89b4da2db4e8f3cdce79f0ab787df87 to your computer and use it in GitHub Desktop.
Save dyadica/b89b4da2db4e8f3cdce79f0ab787df87 to your computer and use it in GitHub Desktop.
Photon and Unity Integration via the Variable Method
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JSONWebCall : MonoBehaviour
{
public string DeviceID = "<Your Device ID>";
public string AccessToken = "<Your Access Token>";
public float timeGap = 2;
public AccData AccXData = new AccData();
public AccData AccYData = new AccData();
public AccData AccZData = new AccData();
public Text AccXText;
public Text AccYText;
public Text AccZText;
void Start ()
{
}
void Update ()
{
timeGap -= Time.deltaTime;
if (timeGap < 0)
{
timeGap = 2;
StartCoroutine(GetJSONFromWeb("X"));
StartCoroutine(GetJSONFromWeb("Y"));
StartCoroutine(GetJSONFromWeb("Z"));
}
if (AccXText != null)
AccXText.text = AccXData.result.ToString();
if (AccYText != null)
AccYText.text = AccYData.result.ToString();
if (AccZText != null)
AccZText.text = AccZData.result.ToString();
}
IEnumerator GetJSONFromWeb(string id)
{
string name = string.Empty;
AccData data;
switch(id)
{
case "X": name = "AccX";
break;
case "Y": name = "AccY";
break;
case "Z": name = "AccZ";
break;
}
string url = @"https://api.spark.io/v1/devices/" + DeviceID + @"/" + name + @"/?access_token=" + AccessToken;
WWW www = new WWW(url);
yield return www;
if (www.error == null)
{
data = JsonUtility.FromJson<AccData>(www.text);
if (data.name == "AccX")
AccXData = data;
if (data.name == "AccY")
AccYData = data;
if (data.name == "AccZ")
AccZData = data;
}
else
{
Debug.Log("ERROR: " + www.error);
}
}
}
[System.Serializable]
public class AccData
{
public string cmd;
public string name;
public float result;
public CoreInfo coreInfo;
}
[System.Serializable]
public class CoreInfo
{
public string last_heard;
public bool connected;
public string last_handshake_at;
public string deviceID;
public int product_id;
}
// The on-board LED
int led = D7;
// The accelerometer Pins
int ax = A0;
int ay = A1;
int az = A2;
// The random limits
int maxRand = 90;
int minRand = -90;
char jsonDataString[64];
// Reference the floats
double accX, accY, accZ;
// Reference the string
String FullData = "";
// Send data delay
int dataDelay = 1000;
void setup()
{
pinMode(led, OUTPUT);
}
void loop()
{
// Create dummy accelerometer data
// int x = random(minRand, maxRand);
// int y = random(minRand, maxRand);
// int z = random(minRand, maxRand);
// Or read values from a real accelerometer
// such as the ADXL335
int x = analogRead(ax);
int y = analogRead(ay);
int z = analogRead(az);
// Update the vars for sending
accX = x;
accY = y;
accZ = z;
// convert this to a string to be
// accesable via variable
sprintf(jsonDataString,"{\"AccX\": %u, \"AccY\": %u, \"AccZ\": %u}", x, y, z);
// Assign it to variable
FullData = jsonDataString;
Particle.variable("FullData", &FullData, STRING);
// Assing values to variables
Particle.variable("AccX", &accX, DOUBLE);
Particle.variable("AccY", &accY, DOUBLE);
Particle.variable("AccZ", &accZ, DOUBLE);
// Publish the data via an event
Particle.publish("AccDataCall", FullData);
// Turn the LED on
digitalWrite(led, HIGH);
// Wait for 1 seconds to both allow
// the data to be sent and limit the
// volume of data.
delay(dataDelay);
// Turn the LED off
digitalWrite(led, LOW);
// Wait for 1 seconds to both allow
// the data to be sent and limit the
// volume of data.
delay(dataDelay * 2);
// Continue the loop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment