Complier errors when using Ubidots Library and Particle Dev (Atom)

Hello everyone,

When I attempt to run a simple program using the Ubidots library on the Particle Dev, I continually get the error shown below. However, when I use the Web IDE, I do not get any errors.

Has anyone else had similar issues with libraries on the Particle Dev?

The following is the code I’ve been attempting to compile:

// This example is to save multiple variables to the Ubidots API

#include "Ubidots.h"

#define TOKEN "yyy"
#define Data_Source_Name "zzz"

Ubidots ubidots(TOKEN);

float value1 = 1;
void setup(){
    Serial.begin(9600);
    ubidots.setDatasourceName(Data_Source_Name);
}

void loop(){

    ubidots.add("Variable_Name_One", value1);
    ubidots.sendAll();

    delay(5000);

    value1++;
}

Currently when I attempt to use the Ubidots library with Particle Dev I get the following output from Terminal:

Jeremys-MacBook-Pro:~ Jeremy$ particle compile electron /Users/Jeremy/xxx/Ubidots_practice/

Compiling code for electron

Including:
    /Users/Jeremy/xxx/Ubidots_practice/Ubidots.h
    /Users/Jeremy/xxx/Ubidots_practice/Ubidots_practice.ino
    /Users/Jeremy/xxx/Ubidots_practice/Ubidots.cpp
attempting to compile firmware 
Compile failed. Exiting.
In file included from Ubidots.cpp:27:0:
Ubidots.h:34:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 #define SERVER "translate.ubidots.com"
                ^
Ubidots.h:56:50: note: in expansion of macro 'SERVER'
     explicit Ubidots(char* token, char* server = SERVER);
                                                  ^
Ubidots.cpp: In constructor 'Ubidots::Ubidots(char*, char*)':
Ubidots.cpp:37:13: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
     _dsName = "Particle";
             ^
Ubidots.cpp:38:15: warning: converting to non-pointer type 'float' from NULL [-Wconversion-null]
     lastValue = NULL;
               ^
Ubidots.cpp: In member function 'float Ubidots::getValue(char*)':
Ubidots.cpp:109:19: warning: unused variable 'firstRead' [-Wunused-variable]
     unsigned long firstRead = millis();
                   ^
Ubidots.cpp: In member function 'float Ubidots::getValueWithDatasource(char*, char*)':
Ubidots.cpp:203:74: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
   snprintf(allData, "Particle/1.1|LV|%s|%s:%s|end", _token, dsTag, idName);
                                                                          ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:219:19: warning: unused variable 'firstRead' [-Wunused-variable]
     unsigned long firstRead = millis();
                   ^
Ubidots.cpp: In member function 'bool Ubidots::sendAll()':
Ubidots.cpp:323:20: warning: comparison with string literal results in unspecified behaviour [-Waddress]
     if (_dsName == "Particle") {
                    ^
Ubidots.cpp:324:70: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
         snprintf(allData, "%s|POST|%s|%s=>", USER_AGENT, _token, _pId);
                                                                      ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:324:70: warning: too many arguments for format [-Wformat-extra-args]
         snprintf(allData, "%s|POST|%s|%s=>", USER_AGENT, _token, _pId);
                                                                      ^
Ubidots.cpp:324:70: warning: too many arguments for format [-Wformat-extra-args]
Ubidots.cpp:326:82: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
         snprintf(allData, "%s|POST|%s|%s:%s=>", USER_AGENT, _token, _pId, _dsName);
                                                                                  ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:326:82: warning: too many arguments for format [-Wformat-extra-args]
         snprintf(allData, "%s|POST|%s|%s:%s=>", USER_AGENT, _token, _pId, _dsName);
                                                                                  ^
Ubidots.cpp:326:82: warning: too many arguments for format [-Wformat-extra-args]
Ubidots.cpp:329:84: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
         snprintf(allData, "%s%s:%f", allData, (val + i)->idName, (val + i)->idValue);
                                                                                    ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:331:70: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
             snprintf(allData, "%s$%s", allData, (val + i)->contextOne);
                                                                      ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:335:45: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
             snprintf(allData, "%s,", allData);
                                             ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp:338:40: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
     snprintf(allData, "%s|end", allData);
                                        ^
In file included from /usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/string.h:10:0,
                 from ../hal/src/electron/modem/enums_hal.h:22,
                 from ../hal/src/stm32f2xx/platform_headers.h:21,
                 from ./inc/application.h:32,
                 from Ubidots.h:29,
                 from Ubidots.cpp:27:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/stdio.h:261:5: error:   initializing argument 2 of 'int snprintf(char*, size_t, const char*, ...)' [-fpermissive]
 int _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
     ^
Ubidots.cpp: In member function 'bool Ubidots::sendAllUDP(char*)':
Ubidots.cpp:360:9: warning: unused variable 'size' [-Wunused-variable]
     int size;
         ^
Ubidots.cpp: In member function 'bool Ubidots::sendAll()':
Ubidots.cpp:351:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
make[1]: *** [../build/target/user/platform-10Ubidots.o] Error 1
make: *** [user] Error 2

Other info:
I’m using a Particle Electron 0.5.1
MacBook Pro 10.10.5

I feel like I’ve missed something pretty obvious here, so any help would be greatly appreciated.
Thanks in advance!

I’ll explain one of the warnings:

In file included from Ubidots.cpp:27:0:
Ubidots.h:34:16: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 #define SERVER "translate.ubidots.com"
                ^
Ubidots.h:56:50: note: in expansion of macro 'SERVER'
     explicit Ubidots(char* token, char* server = SERVER);

This happens because string constants are by default of type const char * not char *.

What’s the difference? Well, const variables cannot be modified, and this is good practice in general when coding in C++. But the meaning is even more important on the Particle products.

When you have a const char * string constant, obviously the value of string must be stored with your program in the flash memory. But by declaring it const, the compiler knows it will never be modified, so it doesn’t make a copy of it in modifiable RAM, thus saving RAM and making the program more efficient.

The catch is that you need to maintain the chain of const all the way to the last user of the thing. So you need to declare server at const char *. Or, if that’s not possible, explicitly cast as a (char *) so the compiler knows you really want to make a copy of the value in RAM first.

3 Likes

OK, one more:

Ubidots.cpp: In member function 'float Ubidots::getValueWithDatasource(char*, char*)':
Ubidots.cpp:203:74: error: invalid conversion from 'const char*' to 'size_t {aka unsigned int}' [-fpermissive]
   snprintf(allData, "Particle/1.1|LV|%s|%s:%s|end", _token, dsTag, idName);

It’s always good practice to use snprintf instead of sprintf, but you need to add the second parameter, which is the length of the buffer. Usually it’s something like:

snprintf(allData, sizeof(allData), "Particle/1.1|LV|%s|%s:%s|end", _token, dsTag, idName);

If allData is allocated, such as will malloc, you might have something like ALL_DATA_SIZE or some such constant specific to your code.

The reason is that sprintf doesn’t know the size of the buffer allData and will happily write past the end of the buffer if you make your buffer too small for the data. This is most annoying because most of the time your program won’t crash on that line, but memory will be corrupted and some time later you crash or get unexpected results from your code which is a pain to debug.

Fortunately, snprintf solves this problem by checking the length of the buffer first, and is always good practice, even when you think you’ve set the size of the buffer properly.

2 Likes

I’d say this was some misleading info, since I’m absolutely positive the snprintf() error would have tripped Web IDE too :wink:

The warnings are “ignorable” and wouldn’t make the build fail but it’s good to sort them when you see then and are able to.
Most the time you won’t even see them with Build or Dev unless you also have an error.

1 Like

Hello rickkas7 what version of the library are you using?
Best regards,
Metavix

I was just reading the compiler warnings and errors out of the original post; I do not know what version was used.

@rickkas7 Oh sorry my error.
@jeremygilly there is a new version of the library. You could try with the new version
Best regards,
Metavix