Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get data from Particle Photon to Unity - Vuforia

Discussion in 'Scripting' started by mariapaneta, Dec 10, 2020.

  1. mariapaneta

    mariapaneta

    Joined:
    Nov 30, 2020
    Posts:
    2
    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 :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Linq;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class talktoCyberPlant : MonoBehaviour
    9. {
    10.     string JSON_Rate;
    11.     string path;
    12.     string Url;
    13.     float temp;
    14.     public float MoistureInput;
    15.    
    16.  
    17.     string Zero;
    18.     WWW myRequest;
    19.     string url = "https://api.particle.io/v1/devices/3a001d001847393035313137/MoistureInput?access_token=1d905201546329bf6eb90a4265a4c57f796cc16f";
    20.  
    21.     void Start() // Use this for initialization
    22.     {
    23.    
    24.        
    25.         myRequest = new WWW(url);
    26.         StartCoroutine(WaitForRequest(myRequest));
    27.  
    28.     }
    29.  
    30.     IEnumerator WaitForRequest(WWW myRequest)
    31.         {
    32.             Debug.Log("inside WaitForRequest");
    33.            
    34.             yield return myRequest;
    35.  
    36.             // check for errors  
    37.             if (myRequest.error == null)
    38.             {
    39.                 Debug.Log("Inside if ");
    40.                 string work = myRequest.text; //text was data on other code?
    41.  
    42.                 _Particle fields = JsonUtility.FromJson<_Particle>(work);
    43.                 JSON_Rate = fields.result;
    44.                 temp = float.Parse (JSON_Rate);
    45.                 //MoistureInput = Mathf.FloorToInt(temp);
    46.                 MoistureInput = temp;
    47.                 //Debug.Log (MoistureInput);
    48.  
    49.                
    50.             } else {
    51.                 Debug.Log("Inside else ");
    52.             }
    53.         }
    54.  
    55.    
    56.  
    57.     // Update is called once per frame
    58.     void Update()
    59.     {
    60.        
    61.         Debug.Log("Inside component ");
    62.         //This is to text connectivity with Unity, text should appear on TextMesh when code is loaded
    63.         GetComponent<TextMesh>().text = MoistureInput.ToString()+"degrees";
    64.         Debug.Log(" State Moisture Data ");
    65.  
    66.     }
    67.  
    68.     [System.Serializable]
    69.     public class _Particle{
    70.         public string name;
    71.         public string result;
    72.     }
    73.  
    74.  
    75. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    mariapaneta likes this.
  3. mariapaneta

    mariapaneta

    Joined:
    Nov 30, 2020
    Posts:
    2
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.
     
    mariapaneta likes this.