Help me to retrive the context my-key to arduino, java or anroid program


#1

I have input data from arduino to ubidots context with name my-key, i try too much to retrieve context data but i can’t get the context, please help me, this is for my project, help me as soon as possible.


#2

Hi @karunthomas,

Please show us how are you sending data (your code).

All the best,
Catalina M.


#3

this is the code while i sending context to ubidots.

String var="{\"Data\": {\"value\":"+String(a)+", \"context\":{\"Status\":\"Driving""}}}";
int num=0;
num = var.length();
client.connect("things.ubidots.com", 80);
Serial.println("connected ubidots");
delay(100);
client.println("POST /api/v1.6/devices/"+data_source+"/ HTTP/1.1");
client.println("Content-Type: application/json");
client.println("Content-Length: "+String(num));
client.println("X-Auth-Token: "+token);
client.println("Host: things.ubidots.com\n");
client.print(var);
 if (client.available()) 
 {
char c = client.read();
//Serial.println(c);
 }

Now help me with any code to retrieve value or context from ubidots, i try with json parsing for values but its returns 0.00, if any change in bellow code for geting context,and values any one help me.

 float getValue(char* id){
 float num;
 String response;
 uint8_t bodyPosinit;
 uint8_t bodyPosend;
 if (client.connect("things.ubidots.com", 80)){
 Serial.println(F("Geting your variable"));
 // Make a HTTP request:
 client.print(F("GET /api/v1.6/variables/"));
 client.print(id);
 client.println(F("/values?page_size=1 HTTP/1.1"));
 client.println(F("Host: things.ubidots.com"));
 client.println(F("User-Agent: NodeMCU/1.0")); 
 client.print(F("X-Auth-Token: "));
 client.println(TOKEN);
 client.println(F("Connection: close"));
 client.println();
 }
 int timeout = 0;
 while (!client.available() && timeout < 5000) {
  timeout++;
 }
 while (client.available()){
 response = client.readString();
  }
 Serial.println(response);
 bodyPosinit = 4 + response.indexOf("\r\n\r\n");
 response = response.substring(bodyPosinit);
 Serial.println(response);
 bodyPosinit = 9 + response.indexOf("\"value\":");
 bodyPosend = response.indexOf(", \"timestamp\"");
 num = response.substring(bodyPosinit,bodyPosend).toFloat();
 client.flush();
 client.stop();
 return num;
 }

please help as soon as possible


#4

Hi,

There’s a library for Java to interact in an easier way with the Ubidots API.

You can find it here: https://github.com/ubidots/ubidots-java

You should do something like:

import com.ubidots.*

public class UbidotsProject {
    final String API_KEY = "YOUR API KEY";
    final String VARIABLE_ID = "YOUR VARIABLE ID";
    ApiClient apiClient = new ApiClient(API_KEY);

    public static void main(String... args) {
        Variable variable = apiClient.getVariable(VARIABLE_ID);
        Value[] values = variable.getValues();

        for (Value value : values) {
            Map<String, Object> context = value.getContext();
            // Do whatever you need to do with the context of that value like:
            // int someInformation = (int) context.get("my-key");
        }
    }
}

Hope this helps!


#5

i try this to my android program. i hope you w’ll again help me if i have any query.


#6

If you’re going to use the library in Android, I recommend you to read the documentation and this thread.

It works a bit different in Android because you need to do the HTTP Request in a separate thread from the Main Thread.