How to send data to UBDITOS using AT Commands

How many variables do you want to send?

3 variables at a time

I just want to send data. Not Get

Ok. Wait a minute pls.

Waiting for your responseā€¦
Did you got that?

I am accommodating and commenting on the program so that you can understand it as well as possible. Iā€™m almost done, be patient.

Sorry for the delay but I was making sure that this 100% functional. Are you there ?

Yes iā€™m here iā€™m waiting for you. Its ok no problem. Test your work :slight_smile:

Perfect my friendā€¦ Iā€™m sure this information will not find you anywhere and I just hope it helps you.

#define ssid "xxxxxxxxxxxxx"                        // Put here your SSID              
#define password "xxxxxxxxxx"                       // Put here your PASSWORD 
                  
#define id1 "xxxxxxxxxxxxxxxxxxxxxxxx"              // Put here your Ubidots variable ID1
#define id2 "xxxxxxxxxxxxxxxxxxxxxxxx"              // Put here your Ubidots variable ID2
#define id3 "xxxxxxxxxxxxxxxxxxxxxxxx"              // Put here your Ubidots variable ID3

#define token "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  // Put here your Ubidots TOKEN
    
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 

  Serial.println("\nModule Settings WiFi ESP8266");

  sendCmd("AT+RESTORE", "R", 1000);                                                    // cmd AT+RESTORE: Restores the Factory Default Settings
  sendCmd("AT+RST", "WiFi Module Ready!", 1000);                                       // cmd AT+RST: Restarts the Module
  sendCmd("AT+CWMODE=3", "Ap+Station Mode", 1000);                                     // cmd AT+CWMODE=3: Sets the Wi-Fi Ap + Station Mode
  sendCmd("AT+CWJAP=\""ssid"\",\""password"\"", "Successful WiFi Connection!", 5000);  // cmd AT+CWJAP: Connects to an AP 
  sendCmd("AT+CIPMUX=1", "Enable Multiple Connections!", 500);                         // cmd AT+CIPMUX=1: Enable Multiple Connections
}

void loop() {
  // put your main code here, to run repeatedly:

  int dato1 = analogRead(0);
  //  float dato1 = analogRead(0);
  int dato2 = analogRead(1);
  //  float dato2 = analogRead(1);
  int dato3 = analogRead(2);
  //  float dato3 = analogRead(2);
  
  send_D(dato1, dato2, dato3, 3);  // The last parameter "3", is for choose the number of variables to send to Ubidots: (1-3) 
}

// Function to send data to Ubidots ************************************************************************************************************* 
void send_D(int d1, int d2, int d3, int n_var) {  // Send int data
//  void send_D(float d1, float d2, float d3, int n_var) {  // Send float data  
   
  int string_length = 0, long_json = 0, SEND = 0;
  String Inicio, Final, variable[10];

  // JSON 
  Inicio = "[";
  variable[1] = "{\"variable\": \"" + String(id1) + "\",\"value\":" + String(d1);
  variable[2] = "}, {\"variable\": \"" + String(id2) + "\",\"value\":" + String(d2);
  variable[3] = "}, {\"variable\": \"" + String(id3) + "\",\"value\":" + String(d3);
  Final = "}]";
   
  for(int i = 1; i <= n_var; i++) {
   
    string_length += variable[i].length(); 
  }
  long_json = string_length + Inicio.length() + Final.length();
  //  Serial.println(long_json);

  String send1, send2, send3, send4, send5;
  int long_post = 0;
  
  // POST
  send1 = "POST /api/v1.6/collections/values/ HTTP/1.1\r\n";
  long_post = send1.length();
  send2 = "Content-Type: application/json\r\nContent-Length: "; 
  long_post += send2.length();  
  send3 = String(long_json) + "\r\nX-Auth-Token: "; 
  long_post += send3.length();
  send4 = String(token);
  long_post += send4.length();
  send5 = "Host: things.ubidots.com\r\n";
  long_post += send5.length();
   
  SEND = long_json + long_post + 5;
  //  Serial.println(SEND);
   
  Serial.println(F("\n**********************************************************************************************************************************************************************\n"));
  Flush();
  Serial.println(F("AT+CIPSTART=4,\"TCP\",\"things.ubidots.com\",80"));  // cmd AT+CIPSTART: Establishes TCP Connection 
  delay(5000);
  Flush();
  Serial.println("AT+CIPSEND=4," + String(SEND));                     // cmd AT+CIPSEND: Sends Data
  delay(500);
  Serial.print(send1);
  Serial.print(send2);
  Serial.print(send3);
  Serial.println(send4);
  Serial.println(send5);
  delay(500);
   
  Serial.print(Inicio);
  for(int i = 1; i <= n_var; i++) {
     
    Serial.print(variable[i]); 
  }
  Serial.println(Final);
}

// Function for AT commands ********************************************************************************************************************* 
void sendCmd(char* cmd, char* ans, int t) {

  boolean Er = true;

  Flush();  
  Serial.println(cmd);
  delay(t);

  while(Serial.available()) {
    if(Serial.find("OK")) {
      if(ans == "R") {          
        delay(500);
        Serial.print(".");
        delay(500);
        Serial.print(".");
        delay(500);
        Serial.println(".\n");
        delay(1000);
      }
      else {          
        Serial.println(ans);
        delay(500);
      }        
      Er = false;
    }
  }  
  
  if(Er == true) {      
    Serial.println("Error");
    delay(500);
    if(ans == "Successful WiFi Connection!") {        
      if(Er == true) {          
        Serial.println("WiFi Disconnected!");
        delay(500);
      }
    }
  }  
}

// Function to clean Buffer Serial **************************************************************************************************************
void Flush() {

  byte w = 0;  
  for (int i = 0; i < 10; i++) {    
    while (Serial.available() > 0) {          
      char k = Serial.read();
      w++;
      delay(1);
    }    
    delay(1);
  }
}

Good luck !

If i want to send data with variable label then i should do changes in POST URL right? According to UBIDOTS api referenceā€¦

Iā€™ll try this and will let you know soon what was the response. Thanks a lot man

This is the answer in Serial Port

Device name OR Device label are not mentioned?

Your wealcome. Iā€™ll be waiting the good news. Regards !

Because here you only use the variable token and id.

They are different ways to do a POST but do not worry, they are the same. Also I am more comfortable doing it with the ID.

Ok thanks again. Soon iā€™ll let you know the response and results

1 Like

Good luck in your projects :smiley: !!

Where you have connected your ESP8266 RX and TX pins?

RX(Arduino) -> TX(ESP8266)
TX(Arduino) -> RX(ESP8266)