Ubidots Community

[SOLVED] Cant send context data with Arduino GSM Shield

hey everyone, am new to using the api’s, am planning to send data from the arduino GSM shield as instructed in the website with context info, but whenevr i try to include the context in the variable field i get bad request response, here is my code

// libraries
#include <GSM.h>

    // PIN Number
    #define PINNUMBER ""

    // APN data
    #define GPRS_APN       "internet" // replace with your GPRS APN
    #define GPRS_LOGIN     "guest"    // replace with your GPRS login
    #define GPRS_PASSWORD  "guest" // replace with your GPRS password

    // initialize the library instance
    GSMClient client;
    GPRS gprs;
    GSM gsmAccess;
    uint16_t reset = 0;


    String idvariable = "56730183762542244ad83305";   // replace with your Ubidots Variable ID
    String token = "Xb3ahJbNM8vPXMDGZ7oNBmnnvLIHMfd7hOvKt0ftiORBVLl5ok56OBlNEp85";  // replace with your Ubidots token

//Location where this logger is placed!!
String loclat=“19.21833”;
String loclng=“72.978088”;
String Location;
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

      Serial.println("Starting Arduino web client.");
      Location="{\"lat\":";

Location=Location+loclat;
Location += " ,“lng”:";
Location=Location+loclng+ “}”;
// connection state
boolean notConnected = true;

      // After starting the modem with GSM.begin()
      // attach the shield to the GPRS network with the APN, login and password
      while (notConnected)
      {
        if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
            (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
          notConnected = false;
        else
        {
          Serial.println("Not connected");
          delay(1000);
        }
      }


    }

    void loop()
    {
      int value = 21;
      if(save_value(value,Location))
      {
      }
      else
      {
        reset++;
        if (reset == 10)
        {
          asm volatile ("  jmp 0");    // reset the Arduino board if the data transmission fail
        }
      }

      // if the server's disconnected, stop the client:
      if (!client.available() && !client.connected())
      {
        Serial.println();
        Serial.println("disconnecting.");
        client.stop();

        // do nothing forevermore:

      }
    }
    boolean save_value(int value,String Location)
    {
      Serial.println("connecting...");

      int num=0;
      String var = "{\"value\":"+ String(value) + ",\"context\":"+ Location +"}";
                Serial.println(var);

      num = var.length();
      // if you get a connection, report back via serial:
      if (client.connect("things.ubidots.com", 80))
      {
        Serial.println("connected");
        // Make a HTTP request:
        client.print("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1\nContent-Type: application/json\nContent-Length: "+String(num)+"\nX-Auth-Token: "+token+"\nHost: things.ubidots.com\n\n");
        

        client.println(var);
        client.println();

delay(5000);
}
else
{
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
return false;
}
while (client.available())
{

        char c = client.read();
        Serial.print(c);
      }
    }

so please how can i include the context with the variables am letterly hopeless

Hi

Your code is not really as in example.

Your code is

String var = "{\"value\":"+ String(value) + ",\"context\":"+ Location +"}";

But in example context use a JSON format in {}
like here from example

{“value”:22, “context”:{“color”:“blue”,“status”:“active”}}

Hi all we have a new library for the GPRS Arduino Shield which makes it simpler to send a context: https://github.com/ubidots/ubidots-arduino-gprs