[SOLVED] How to create new Device/Data source on new Esp8266 module

I want to know how to create a new Device from each new esp8266 module using unique id or mac address as a Device ID and custom Device Name?

Greetings,

If you are using the MicroESP library, you should call the method setDataSourceLabel("Your-MAC") before of calling the add method, something like this:

setDataSourceLabel("YOUR-MAC");
add("Var-1", value1);
add("Var-2", value2);
sendAll(false);

If you are using the MQTT library, you should send it at the publish method, something like this:

client.add("stuff", 10.2); //Insert your variable Labels and the value to be sent
client.ubidotsPublish("YOUR-MAC");

Regards.

Ok. it means i have to add this line
client.setDataSourceLabel(“my-mac”);
before add(“Var-1”, value1);
in void setup()

And i thing more what should i place in Variable ID as i’m going to create a new datasource
I’m using espMicro Library

Grettings sir, you have to use the HTTP send method and that is why I set a false flag as parameter to the sendAll() function in the example below, if you use HTTP to send data you have to insert as parameters to the add() method the variable label and the value and you don’t need to use the variable ID. In some words, just try the example code posted before and you will understand how it works.

Regads

i’m not getting you. i’m using espMicro library could you please show me the example? i’m getting confuse because i want to use esp library so what should i place in variable id as i cannot leave it blank

Please insert the snippet code above in your routine.

Regards

Here is my Code… it is not creating a new data source. if i’m wrong please guide me

#include "UbidotsMicroESP8266.h"

#define TOKEN  "9XWFrsh83kQWtNvBB6oU1F6zufzS8B"  // Put here your Ubidots TOKEN
#define ID_1 "58d52de17625421a84b7f177" // Put your variable ID here
//#define ID_2 "Your_variable_ID_here" // Put your variable ID here
#define WIFISSID "ZJ" // Put here your Wi-Fi SSID
#define PASSWORD "3ngin33r99" // Put here your Wi-Fi password

Ubidots client(TOKEN);

void setup(){
    Serial.begin(115200);
    client.wifiConnection(WIFISSID, PASSWORD);
    client.setDataSourceLabel("espID");
    client.setDataSourceName("_dsName");
    client.setDebug(true); // Uncomment this line to set DEBUG on
}

void loop(){
    float value1 = analogRead(4);
    //float value2 = analogRead(2)
    client.add(ID_1, value1);
    //client.add(ID_2, value2);
    client.sendAll(false);
    delay(5000);
}

the vriable ID mentioned above is the ID of my previous Datasource Variable ID, if i put it blank it gives error and if i put random ID it shows “variable doesn’t exist”

Greetings sir, I made a mistake, you should set a true bool flag as parameter to the method sendAll() to use HTTP, just adapt this example to your needs:


#include "UbidotsMicroESP8266.h"

#define TOKEN  "..."  // Put here your Ubidots TOKEN
#define WIFISSID "..." // Put here your Wi-Fi SSID
#define PASSWORD "..." // Put here your Wi-Fi password

Ubidots client(TOKEN);

void setup(){
    Serial.begin(115200);
    delay(10);
    client.wifiConnection(WIFISSID, PASSWORD);
    client.setDataSourceLabel("device-test"); 
}
void loop(){
    float value1 = analogRead(0);
    float value2 = analogRead(2);
    client.add("temperature", value1);
    client.add("switch", value2);
    client.sendAll(true);
    delay(5000);
}

In the add method you have to use the variable label instead of its ID.

Regards

thanks it is working now… but why there is a delay of 6 seconds in posting values to ubidots in this method. when i was using the old method by sending value with variable id, data show up after every second

Sir, at the end of the example script there is a delay of 5000 milliseconds, or 5 seconds. Simply play with the delay if you need a higher sample rate.

All the best.

thank you for your help… :slight_smile:

1 last question…
am i calling sendTLATE function now instead of sendHTTP?

Sir this is the code for the sendAll() method:

bool Ubidots::sendAll(bool type) {
    if (type) {
        return sendTLATE();
    } else {
        return sendHTTP();
    }
}

So with the script that I sent you previously you are calling the sendTLATE() private method.

Regards