Back to Parent

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class PhotonController : MonoBehaviour
{
    bool ledIsOn = false;
    public GameObject uiImages;
    void Start()
    {
        uiImages.SetActive(false);
        // A correct website page.
        //StartCoroutine(GetRequest("https://api.particle.io/v1/devices/3d003a000447363339343638/value?access_token=5c8b7dfdeeda7ff3f10ea3090b6546d4a471dac3"));

        // A non-existing page.
       
    }

    IEnumerator GetRequest(string uri)
    {
        while (true)
        {
            using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
            {
                // Request and wait for the desired page.
                yield return webRequest.SendWebRequest();

                string[] pages = uri.Split('/');
                int page = pages.Length - 1;

                if (webRequest.isNetworkError)
                {
                    Debug.Log(pages[page] + ": Error: " + webRequest.error);
                }
                else
                {
                    //Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                    JSONObject lightData = new JSONObject(webRequest.downloadHandler.text);

                    // Grab the "result" value and store it
                    lightData = lightData["result"];
                    Debug.Log(lightData);
                }
            }
            yield return new WaitForSeconds(1);
        }
    }

    public IEnumerator Upload(string state)
    {
        WWWForm form = new WWWForm();
        form.AddField("arg", state);

        using (UnityWebRequest www = UnityWebRequest.Post("https://api.particle.io/v1/devices/3d003a000447363339343638/led?access_token=5c8b7dfdeeda7ff3f10ea3090b6546d4a471dac3", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
            }
        }
    }
    private void Update()
    {
        if (DefaultTrackableEventHandler.isTracking)
        {

            if (!ledIsOn)
            {
                StartCoroutine(Upload("on"));
                Debug.Log("uploadind on");
                ledIsOn = true;
                uiImages.SetActive(true);
            }
           
        }
        else
        {
           
            if (ledIsOn)
            {
                StartCoroutine(Upload("off"));
                Debug.Log("uploading off");
                ledIsOn = false;
                uiImages.SetActive(false);

            }

        }
    }
}
Shengzhi Wu (2019) Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0