[SOLVED] How to implement 2 variables with the sim900

I use de sim900 script that use one variable , but i dont konw how to manage 2 of this variables , the code that I used is the next:
IT REALLY WORKS , BUT … I DONT KNOW HOW TO TRY WITH 2 VARIABLES , SOME ONE CAN HELP ME?
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(10,11); //your pins to serial communication
int valor;
//-------------------------------------------------------------
//---------------------Ubidots Configuration-------------------
//-------------------------------------------------------------
String token = “ip9y0rslhSsyWGni4UPKPuSn5tdR6prXf1ATLiVWbXGy4d1wj9eGrsgtERRv”; //your token to post value
String idvariable = “553160967625420f1a712899”; //ID of your variable
void setup()
{

mySerial.begin(19200); //the GPRS baud rate
Serial.begin(19200); //the serial communication baud rate
delay(10000);
}

void loop()
{
int value = analogRead(A0); //read pin A0 from your arduino
save_value(String(value)); //call the save_value function
if (mySerial.available())
Serial.write(mySerial.read());
}
//this function is to send the sensor data to Ubidots, you should see the new value in Ubidots after executing this function
void save_value(String value)
{
int num;
String le;
String var;
var="{“value”:"+ value + “}”;
num=var.length();
le=String(num);
for(int i = 0;i<7;i++)
{
mySerial.println(“AT+CGATT?”); //this is made repeatedly because it is unstable
delay(2000);
ShowSerialData();
}
mySerial.println(“AT+CSTT=“internet.claro.com.ec””); //replace with your providers’ APN
delay(1000);
ShowSerialData();
mySerial.println(“AT+CIICR”); //bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIFSR”); //get local IP adress
delay(2000);
ShowSerialData();
mySerial.println(“AT+CIPSPRT=0”);
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSTART=“tcp”,“things.ubidots.com”,“80"”); //start up the connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSEND”); //begin send data to remote server
delay(3000);
ShowSerialData();
mySerial.print(“POST /api/v1.6/variables/”+idvariable);
delay(100);
ShowSerialData();
mySerial.println(”/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println(“Content-Type: application/json”);
delay(100);
ShowSerialData();
mySerial.println("Content-Length: "+le);
delay(100);
ShowSerialData();
mySerial.print("X-Auth-Token: ");
delay(100);
ShowSerialData();
mySerial.println(token);
delay(100);
ShowSerialData();
mySerial.println(“Host: things.ubidots.com”);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println(var);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println((char)26);
delay(7000);
mySerial.println();
ShowSerialData();
mySerial.println(“AT+CIPCLOSE”); //close the communication
delay(1000);
ShowSerialData();
}

void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}

Hi @josjos45,

To update two variables in a single request, use this API endpoint.

In your program, it would look something like this:

In the line where you specify the URL:

mySerial.print("POST /api/v1.6/variables/"+idvariable);

Specify the new URL:

mySerial.print("POST /api/v1.6/collections/values/");

In the line where you send the body of the request, specify your variables IDs and values:

var="[{\"variable\":" + idvariable1 + ", \"value\":" + value1 + "}, {\"variable\":" + idvariable2 + ", \"value\":" + value2 + "}]";

I’m replying based on the theory, sorry if there’s some error in a character (that last line can be tricky), hope this helps!

kindly anyone check the code below as I am unable to post data for 2 variable. I am able to post single variable data with the same APN as the sample code has.

/*
Basic sketch for GPRS shield sim900

This is a basic example to post a value on Ubidots with a simple
function “save_value”.

You’ll need:

  • An Arduino Uno
  • A GPRS shield

created 20 Aug. 2014
by Mateo Velez - Metavix - for Ubidots Inc.

This example code is in the public domain.

*/

//--------------------------------------------------------------
//------------------------------Libraries-----------------------
//--------------------------------------------------------------
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(3, 4); //your pins to serial communication
int valor;
//-------------------------------------------------------------
//---------------------Ubidots Configuration-------------------
//-------------------------------------------------------------
String token = “ftooXtqkuPr7woUugDAsit8rFJOWbPiBq2oLko5MgYBWnzXP65V1YaPWb8Pz”; //your token to post value
String idvariable1 = “563c27ea7625426b1005aea4”; //ID of your variable
String idvariable2 = “563c280f7625426bb0a5390e”;
void setup()
{

mySerial.begin(9600); //the GPRS baud rate
Serial.begin(9600); //the serial communication baud rate
delay(10000);
}

void loop()
{
int value1 = analogRead(A0); //read pin A0 from your arduino
int value2 = analogRead(A1);
save_value(String (value1), String (value2)); //call the save_value function
if (mySerial.available())
Serial.write(mySerial.read());
}
//this function is to send the sensor data to Ubidots, you should see the new value in Ubidots after executing this function
void save_value(String value1, String value2)
{
int num;
String le;
String var;
var="[{“variable”:" + idvariable1 + “, “value”:” + value1 + “}, {“variable”:” + idvariable2 + “, “value”:” + value2 + “}]”;
num=var.length();
le=String(num);
for(int i = 0;i<7;i++)
{
mySerial.println(“AT+CGATT?”); //this is made repeatedly because it is unstable
delay(2000);
ShowSerialData();
}
mySerial.println(“AT+CSTT=“web.vmc.net.co””); //replace with your providers’ APN
delay(1000);
ShowSerialData();
mySerial.println(“AT+CIICR”); //bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIFSR”); //get local IP adress
delay(2000);
ShowSerialData();
mySerial.println(“AT+CIPSPRT=0”);
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSTART=“tcp”,“things.ubidots.com”,“80"”); //start up the connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSEND”); //begin send data to remote server
delay(3000);
ShowSerialData();
mySerial.print(“POST /api/v1.6/collections/values/”);
delay(100);
ShowSerialData();
mySerial.println(”/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println(“Content-Type: application/json”);
delay(100);
ShowSerialData();
mySerial.println("Content-Length: "+le);
delay(100);
ShowSerialData();
mySerial.print("X-Auth-Token: ");
delay(100);
ShowSerialData();
mySerial.println(token);
delay(100);
ShowSerialData();
mySerial.println(“Host: things.ubidots.com”);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println(var);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println((char)26);
delay(7000);
mySerial.println();
ShowSerialData();
mySerial.println(“AT+CIPCLOSE”); //close the communication
delay(1000);
ShowSerialData();
}

void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}

serial monitor in Arduino is showing:

AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CSTT=“web.vmc.net.co

ERROR
AT+CIICR

ERROR
AT+CIFSR

100.104.7.190
AT+CIPSPRT=0

OK
AT+CIPSTART=“tcp”,“things.ubidots.com”,“80”

OK

CONNECT OKAT+CIPSEND
POST /api/v1.6/collections/values//values HTTP/1.1
Content-Typ
AT+CIPCLOSE

CLOSE OK
AT+CGATT?

+CGATT: 1

Hi,

You have an error, you must replace mySerial.print("POST /api/v1.6/collections/values/"); by mySerial.print("POST /api/v1.6/collections"); then the code would look something like:

mySerial.println("AT+CIPSEND"); //begin send data to remote server
delay(3000);
ShowSerialData();
mySerial.print("POST /api/v1.6/collections");
delay(100);
ShowSerialData();
mySerial.println("/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println("Content-Type: application/json");

Regards,

Hi Woakas,

Thanks for replying. I did as you mentioned above but still data is not posting. Kindly check I am attaching the code and serial monitor copy:

code:
/*
Basic sketch for GPRS shield sim900

This is a basic example to post a value on Ubidots with a simple
function “save_value”.

You’ll need:

  • An Arduino Uno
  • A GPRS shield

created 20 Aug. 2014
by Mateo Velez - Metavix - for Ubidots Inc.

This example code is in the public domain.

*/

//--------------------------------------------------------------
//------------------------------Libraries-----------------------
//--------------------------------------------------------------
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial mySerial(3, 4); //your pins to serial communication
int valor;
//-------------------------------------------------------------
//---------------------Ubidots Configuration-------------------
//-------------------------------------------------------------
String token = “ftooXtqkuPr7woUugDAsit8rFJOWbPiBq2oLko5MgYBWnzXP65V1YaPWb8Pz”; //your token to post value
String idvariable1 = “563c27ea7625426b1005aea4”; //ID of your variable
String idvariable2 = “563c280f7625426bb0a5390e”;
void setup()
{

mySerial.begin(9600); //the GPRS baud rate
Serial.begin(9600); //the serial communication baud rate
delay(10000);
}

void loop()
{
int value1 = analogRead(A0); //read pin A0 from your arduino
int value2 = analogRead(A1);
save_value(String (value1), String (value2)); //call the save_value function
if (mySerial.available())
Serial.write(mySerial.read());
}
//this function is to send the sensor data to Ubidots, you should see the new value in Ubidots after executing this function
void save_value(String value1, String value2)
{
int num;
String le;
String var;
var="[{“variable”:" + idvariable1 + “, “value”:” + value1 + “}, {“variable”:” + idvariable2 + “, “value”:” + value2 + “}]”;
num=var.length();
le=String(num);
for(int i = 0;i<7;i++)
{
mySerial.println(“AT+CGATT?”); //this is made repeatedly because it is unstable
delay(2000);
ShowSerialData();
}
mySerial.println(“AT+CSTT=“web.vmc.net.co””); //replace with your providers’ APN
delay(1000);
ShowSerialData();
mySerial.println(“AT+CIICR”); //bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIFSR”); //get local IP adress
delay(2000);
ShowSerialData();
mySerial.println(“AT+CIPSPRT=0”);
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSTART=“tcp”,“things.ubidots.com”,“80"”); //start up the connection
delay(3000);
ShowSerialData();
mySerial.println(“AT+CIPSEND”); //begin send data to remote server
delay(3000);
ShowSerialData();
mySerial.print(“POST /api/v1.6/collections”);
delay(100);
ShowSerialData();
mySerial.println(”/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println(“Content-Type: application/json”);
delay(100);
ShowSerialData();
mySerial.println("Content-Length: "+le);
delay(100);
ShowSerialData();
mySerial.print("X-Auth-Token: ");
delay(100);
ShowSerialData();
mySerial.println(token);
delay(100);
ShowSerialData();
mySerial.println(“Host: things.ubidots.com”);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println(var);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println((char)26);
delay(7000);
mySerial.println();
ShowSerialData();
mySerial.println(“AT+CIPCLOSE”); //close the communication
delay(1000);
ShowSerialData();
}

void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}

Serial Monitor:
+CGATT: 1

OK

Call Ready
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CGATT?

+CGATT: 1

OK
AT+CSTT=“web.vmc.net.co
OK
AT+CIICR

OK
AT+CIFSR

10.84.80.229
AT+CIPSPRT=0

OK
AT+CIPSTART=“tcp”,“things.ubidots.com”,“80”

OK

CONNECT OKAT+CIPSEND
POST /api/v1.6/collections/values HTTP/1.1
Content-Type: appli
AT+CIPCLOSE

CLOSE OK
AT+CGATT?

+CGATT: 1

OK

Regards

can anyone help me…kindly check the above code…as I am unable to post more one variable to Ubidots till now.

Hi @suraj please use the new SIM900 library http://ubidots.com/docs/devices/gprs.html the older code will be deprecated soon.

hola woakas como estas, sabes hablar español?, me podrias asesorar con un proyecto que estoy haciendo?

@ivan82 claro, lo mejor es que puedas abrir otro hilo para poder conversar lo nuevo.

Saludos
Gustavo.