Get data from Particle Photon to Unity - Vuforia

Hello!

I am working on an AR project, visualising data from a sensor to Unity.
Particle photon is the microprocessor that receives data and publishes them online. Then this data is linked to a text mesh, whose values change accordingly.
The problem is that although data is sent to the cloud, unity does not receive any values. Nothing inside yield return is read, according to Debug.Log

Any ideas how this could be implemented would be great :slight_smile:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class talktoCyberPlant : MonoBehaviour
{
    string JSON_Rate;
    string path;
    string Url;
    float temp;
    public float MoistureInput;
   

    string Zero;
    WWW myRequest;
    string url = "https://api.particle.io/v1/devices/3a001d001847393035313137/MoistureInput?access_token=1d905201546329bf6eb90a4265a4c57f796cc16f";
  
    void Start() // Use this for initialization
    {
   
       
        myRequest = new WWW(url);
        StartCoroutine(WaitForRequest(myRequest));

    }

    IEnumerator WaitForRequest(WWW myRequest)
        {
            Debug.Log("inside WaitForRequest");
           
            yield return myRequest;

            // check for errors   
            if (myRequest.error == null)
            {
                Debug.Log("Inside if ");
                string work = myRequest.text; //text was data on other code?

                _Particle fields = JsonUtility.FromJson<_Particle>(work);
                JSON_Rate = fields.result;
                temp = float.Parse (JSON_Rate);
                //MoistureInput = Mathf.FloorToInt(temp);
                MoistureInput = temp;
                //Debug.Log (MoistureInput);

               
            } else {
                Debug.Log("Inside else ");
            } 
        }

   

    // Update is called once per frame
    void Update()
    {
       
        Debug.Log("Inside component ");
        //This is to text connectivity with Unity, text should appear on TextMesh when code is loaded
        GetComponent<TextMesh>().text = MoistureInput.ToString()+"degrees";
        Debug.Log(" State Moisture Data ");

    }

    [System.Serializable]
    public class _Particle{
        public string name;
        public string result;
    }


}

WWW is deprecated, you should migrate your code over to UnityWebRequest: Unity - Scripting API: Networking.UnityWebRequest.Get

1 Like

Thank’s for your message! I’m going through the documentation.
Since I am a complete beginner at this any help would be great :slight_smile:

M

Anytime you’re hitting a web API, it’s always helpful to get it working first with curl or Postman, then once you are 100% sure what is necessary, start implementing it in Unity.

1 Like