Ubidots Community

[SOLVED] C# getting a value from variable

I am using the C# library: https://github.com/ubidots/ubidots-c-sharp in Unity 3D.

I am not able to get the current value within a variable. All it returns is the object Ubidots.Variable.
I am able to get names data so I am confident I am connected to Ubidots and that the issue is probably in my code. :slight_smile:

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Ubidots;
using Newtonsoft;
using UnityEngine.UI;

public class UbidotsManager : MonoBehaviour {

    static ApiClient Api = new ApiClient("XXXXXXXXXXXXXXXXXXXX"); // create Ubidots client
    // set up variabels
    public Variable exampleVariable;
    public Value exampleValues;
    public string exampleName;
    public Text text;

    void Update()

        exampleVariable = Api.GetVariable("XXXXXXXXXXXX"); // set to the temperature variable
        exampleValues = exampleVariable.GetValues()[0]; // get the last value
        exampleName = exampleVariable.GetName(); //  get the name
        Debug.Log(exampleValues); // show me the value
        Debug.Log(exampleName); // show me the variable's name


Here is my resulting log:

UbidotsManager:Update() (at Assets/PID/UbidotsManager.cs:26)

UbidotsManager:Update() (at Assets/PID/UbidotsManager.cs:27)

p.s. have hidden my API keys with “XXXXXXXXXXXX”

Greetings, the C# SDK is actually outdated and some of its methods may fail, we do not plan to update it in the short time so I advise you to create your own HTTP requests to retrieve values using the native C# HTTP library. You can find below an example that fits our REST API to retrieve the last value of a variable in your account:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
   class Program
       static void Main(string[] args)
           String token = "BBFF-";  // Replace here by your TOKEN
           String device = "weather-station";  //Replace here by the device label
           String variable = "temperature";  //Replace here by the variable label
           // Create a request for the URL.         
           WebRequest request = WebRequest.Create("http://industrial.api.ubidots.com/api/v1.6/devices/" + device + "/" + variable + "/lv");
           // If required by the server, set the credentials.
           request.Headers.Add("X-Auth-Token", token);
           request.Credentials = CredentialCache.DefaultCredentials;
           // Get the response.
           HttpWebResponse response = (HttpWebResponse)request.GetResponse();
           // Display the status.
           // Get the stream containing content returned by the server.
           Stream dataStream = response.GetResponseStream();
           // Open the stream using a StreamReader for easy access.
           StreamReader reader = new StreamReader(dataStream);
           // Read the content.
           string responseFromServer = reader.ReadToEnd();
           // Display the content.
           // Cleanup the streams and the response.
           response.Close(); ;


All the best

Could you also show an example of posting a value with WebRequest in C#?