-
-
Save dyadica/b89b4da2db4e8f3cdce79f0ab787df87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Photon and Unity Integration via the Variable Method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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