#include <String.h>
int valor;

String token = "6ZImU622GhPWecutlSnMd8ayOpjRps";      //your token to post value
String idvariable = "547791ad7625425a8fae9fc6";                                     //ID of your variable
void setup()
{
  Serial.begin(19200);                                                              //the serial communication baud rate
  delay(1000);
}

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

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