Declare thing() inside a function

I’m writing arduino code to connect to thinger.io using a Node MCU. I have it working, but all of the examples have the “ThingerESP8266 thing(user, device_Id, device_credentials);” declaration in the global area above setup(). I do not want to hard code my device information and need to store it in the non-volatile memory. So I will need to be able to declare the thing object in a function and yet have it global. Has anyone figured out how to do this?

Hello @treyco

This is a very interesting question,

Thinger object needs to be declared as global, but you can create a pointer of Thinger object and call the constructor later, just taking care with the pointer codification when defining the resources and calling .handle() instruction. Please take a look at the example below:

#include <ThingerESP8266.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"

ThingerESP8266 *thing;

void myThinger(){
      (*thing)=new ThingerESP8266(USERNAME,DEVICE_ID, DEVICE_CREDENTIAL);
      (*thing).add_wifi(SSID,SSID_PASSWORD);
}

void setup(){

  // digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  (*thing)["led"] << digitalPin(LED_BUILTIN);

  // resource output example (i.e. reading a sensor value)
  (*thing)["millis"] >> outputValue(millis());

}

void loop(){
 (*thing).handle();
}


Hope it helps

i used this solution in ThingerClient.h file:
add 3 line aftter line 85
const char* TD_User;
const char* TD_Device;
const char* TD_Credential;

change this line in void connect_client() to >> connected = thinger::thinger::connect(TD_User, TD_Device, TD_Credential);

and set TD_User , TD_Device and TD_Credential in sketch.

The problem was solved for me