Hi.. I'm trying to send data of 3 variables to ubidots. The data is not showing up on Ubidots.. Here is my code please check it where is the actual problem.
#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);
}
}
I'm using Arduino UNO and ESP8266-01.
Please check the screenshot below to get the better understanding and the output.
Here is the screenshot of my serial monitor too..
