New library for Ubidots!

Hey guys!
Hello for all, i have good news for you, Ubidots create a new library for you, with this new library you will be able to connect to Ubidots with TCP method or if you prefer with UDP method. Here some basic examples:

// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"



#define TOKEN "CCN8FrVulRYGulPTkbaiR9Myx8qN2o"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
}
void loop() {
    float value = analogRead(A0);
    ubidots.add("Variable_Name", value, "context1=value_context1$context2=value_context2");  // Change for your variable name
    ubidots.sendAll();
    delay(5000);
}

for UDP method use this:

// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"



#define TOKEN "CCN8FrVulRYGulPTkbaiR9Myx8qN2o"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setMethod(TYPE_UDP);
}
void loop() {
    float value = analogRead(A0);
    ubidots.add("Variable_Name", value, "context1=value_context1$context2=value_context2");  // Change for your variable name
    ubidots.sendAll();
    delay(5000);
}

if you want to change the the data source name where the particle will save the variable use the next code:


// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"



#define TOKEN "CCN8FrVulRYGulPTkbaiR9Myx8qN2o"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_NAME "Your_Data_Source_Name"
Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setDatasourceName(DATA_SOURCE_NAME);
}
void loop() {
    float value = analogRead(A0);
    ubidots.add("Variable_Name", value, "context1=value_context1$context2=value_context2");  // Change for your variable name
    ubidots.sendAll();
    delay(5000);
}

by the way, we save the variables in a data source with a specific TAG, for default the data source TAG of the particle is the particle ID, but if you want to change it please use the next code:


// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"



#define TOKEN "CCN8FrVulRYGulPTkbaiR9Myx8qN2o"  // Put here your Ubidots TOKEN
#define DATA_SOURCE_TAG "Your_Data_Source_Tag"
Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    ubidots.setDatasourceTag(DATA_SOURCE_TAG);
}
void loop() {
    float value = analogRead(A0);
    ubidots.add("Variable_Name", value, "context1=value_context1$context2=value_context2");  // Change for your variable name
    ubidots.sendAll();
    delay(5000);
}
6 Likes

Does the udp method result in an appreciable data usage savings and if so how much?

1 Like

According to this link

TCP has bigger header than UDP. Usual header size of a TCP packet is 20 bytes which is more than double of 8 bytes, header size of UDP datagram packet. TCP header contains Sequence Number, Ack number, Data offset, Reserved, Control bit, Window, Urgent Pointer, Options, Padding, Check Sum, Source port, and Destination port. While UDP header only contains Length, Source port, Destination port, and Check Sum

1 Like

Hey there, I am getting the following compile errors. Any thoughts, ideas?

 simple_temp.cpp:31:22: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 unsigned int DHTnextSampleTime;     // Next time we want to start sample
                      ^
simple_temp.cpp: In function 'void loop()':
simple_temp.cpp:41:33: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 }
                                 ^
simple_temp.cpp:42:28: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 
                            ^
HttpClient/HttpClient.cpp: In member function 'void HttpClient::request(http_request_t&, http_response_t&, http_header_t*, const char*)':
HttpClient/HttpClient.cpp:176:19: warning: unused variable 'firstRead' [-Wunused-variable]
     unsigned long firstRead = millis();
                   ^
Ubidots/Ubidots.cpp: In constructor 'Ubidots::Ubidots(char*)':
Ubidots/Ubidots.cpp:36:13: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
     _dsName = "Particle";
             ^
Ubidots/Ubidots.cpp:37:15: warning: converting to non-pointer type 'float' from NULL [-Wconversion-null]
     lastValue = NULL;
               ^
Ubidots/Ubidots.cpp: In member function 'float Ubidots::getValue(char*)':
Ubidots/Ubidots.cpp:110:19: warning: unused variable 'firstRead' [-Wunused-variable]
     unsigned long firstRead = millis();
                   ^
Ubidots/Ubidots.cpp: In member function 'float Ubidots::getValueWithDatasource(char*, char*)':
Ubidots/Ubidots.cpp:224:19: warning: unused variable 'firstRead' [-Wunused-variable]
     unsigned long firstRead = millis();
                   ^
Ubidots/Ubidots.cpp: In member function 'bool Ubidots::sendAll()':
Ubidots/Ubidots.cpp:333:20: warning: comparison with string literal results in unspecified behaviour [-Waddress]
     if (_dsName == "Particle") {
                    ^
Ubidots/Ubidots.cpp: In member function 'bool Ubidots::sendAllUDP(char*)':
Ubidots/Ubidots.cpp:370:9: warning: unused variable 'size' [-Wunused-variable]
     int size;
         ^
Ubidots/Ubidots.cpp: In member function 'bool Ubidots::sendAll()':
Ubidots/Ubidots.cpp:361:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
../../../build/target/user/platform-6/libuser.a(simple_temp.o): In function `__static_initialization_and_destruction_0':
simple_temp.cpp:31: undefined reference to `dht_wrapper()'
collect2: error: ld returned 1 exit status
make: *** [43fd4b1f676c0cc1417f5c16d390c09f8f514e5f9cb1dcb13b8f548ae1f5.elf] Error 1
Error: Could not compile. Please review your code.

Have you got an implementation for that in your code?

This has nothing to do with Ubidots but the DHT library you're using.


BTW: The amount of warnings and the reason for some of them are not nice or good practice, but they don't make the build fail - only the error mentioned above does.

Yeah, I am using the PietteTech_DHT. Here is my code, please feel free to shred it to pieces :smile:

// This #include statement was automatically added by the Particle IDE.
#include "Ubidots/Ubidots.h"

#include "PietteTech_DHT/PietteTech_DHT.h"


// system defines
#define DHTTYPE  DHT22              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   6         	    // Digital pin for communications
#define DHT_SAMPLE_INTERVAL   2000  // Sample every two seconds

//declaration
void dht_wrapper(); // must be declared before the lib initialization


// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

#define TOKEN "my_token_which_I_removed_to_post_on_this_forum"
int lightLevel = 0;
unsigned int DHTnextSampleTime;	    // Next time we want to start sample
bool bDHTstarted;		    // flag to indicate we started acquisition
int n;


Ubidots ubidots(TOKEN);

void setup() {
    Serial.begin(115200);
    DHT.acquire();
}

void loop() {
    float h = DHT.getHumidity();
    float t = DHT.getCelsius();
    ubidots.add("temperature", t);  // Change for your variable name
    ubidots.add("humidy", h);
    ubidots.sendAll();
    delay(5000);
}

You do have a function prototype

//declaration
void dht_wrapper(); // must be declared before the lib initialization

but you are missing the implementation for it

void dht_wrapper() 
{
  DHT.isrCallback();
}

that worked! I definitely missed that, cool!

1 Like

1 Like

any ideas on how to check the sensor is actually working? this test produced no change in temp or humidity

You’d need to trigger a new measurement by DHT.acquire() or DHT.acquireAndWait() each time anew

1 Like

@squeakywheel you could try with air conditioner sensor. If your sensor has the same vale of air conditioner it is ok (don’t forget to put the DHT sensor close to the air conditioner).
Best regards,
Metavix

I actually got it to work with DHT.acquireAndWait in the void loop section of the code.

Yes, I meant that this should be called repeatedly (in loop() or some other way) to get new readings.
It’s always good to look at the samples that come with a lib to see how it’s supposed to be used.

1 Like