Thinger_Device_Name == Hostname (Solved !)

Hi,
I am looking after a solution to make a generic configuration for many devices.
The device name should be automatic from its Hostname


ThingerESP32 thing(THINGER_USERNAME, WiFi.hostname(), THINGER_DEVICE_CREDENTIALS);

That code only seems to do the job, but the thinger library will not accept a string as a parameter.
The error is:
no matching function for call to ‘ThingerESP8266::ThingerESP8266(const char [9], String, const char [8])’

Only a static char type is accepted .So we need another solution.

Ideally the Thinger library should populate the device name automatically with the Host Name if the parameter is missing.
Thank you…

Edited the first message: the thinger library will not accept a string as a parameter. Only a static char type is accepted .So we need another solution.

Hi,

This could do the job, define a char array

char THINGER_DEVICE[20]

Convert the desired String to the definded char array

WiFi.hostname().toCharArray(THINGER_DEVICE, 20);

Use the char array at the thinger definition
ThingerESP32 thing(THINGER_USERNAME, WiFi.hostname(), THINGER_DEVICE_CREDENTIALS);

Try it and let us know how it goes.

Hope this helps.

I tried that code:

#if defined(DASHBRD_IS_THINGER)
char ThingerDevice[20];
WiFi.hostname().toCharArray(ThingerDevice, 20);
#if defined(CONTR_IS_ESP8266)
ThingerESP8266 thing(THINGER_USERNAME, ThingerDevice, DEVICE_NAME_CREDENTIALS);
//ThingerESP8266 thing(THINGER_USERNAME, DEVICE_NAME, DEVICE_NAME_CREDENTIALS);
#else
ThingerESP32 thing(THINGER_USERNAME,  ThingerDevice, DEVICE_NAME_CREDENTIALS);
//ThingerESP32 thing(THINGER_USERNAME,  DEVICE_NAME, DEVICE_NAME_CREDENTIALS);
#endif
#endif

The problem is when its placed before setup(), I get

'WiFi' does not name a type

and inside setup, Thinger is not properly initialized for the rest of the code.

Hi,

I have just checked the wifi library

    const char * getHostname();
    bool setHostname(const char * hostname);
    bool hostname(const String& aHostname) { return setHostname(aHostname.c_str());

I think WiFi.hostname() returns a bool value, not a String.

Try with WiFi.getHostname(); maybe you can declare it directly into thinger initialization command without any other transformation.

Hope this helps.

Thank you Ega.

ThingerESP8266 thing(THINGER_USERNAME, WiFi.getHostname(), DEVICE_NAME_CREDENTIALS);

That worked perfectly !
Having several devices using directly their Hostname as Thinger device is a feature that permits using the same hex file on many devices without need to recompile if the user and the credentials are shared.
IMHO that should be documented.

It can even be done more elegantly.

At the beginning of the sketch you give the directive:
#define DEVICE_NAME WiFi.getHostname()
or of you prefer a fixed name
#define DEVICE_NAME “my_fixed_name”
so it works in Thinger, but also on print instructions and on the OTA initalisation.