Assign a number to "ni" through a variable
-
Hello,
How to assign a number to the widget "ni" through a variable
mqttClient.publish(&In[0], "@ni1 VAL:1234\n\r"); // works, but not what I needbyte tmp = 27;
mqttClient.publish(&In[0], "@ni3 VAL:tmp\n\r"); // not work
mqttClient.publish(&In[0], "@ni3 VAL:&tmp\n\r"); // not workString strtmp = "81";
mqttClient.publish(&In[0], "@ni2 VAL:&strtmp\n\r");// not workHow can this be done?
Best Regards
-
@sato Hello,
One way (you can adapt the buffer size to your needs and you should also check the return value of snprintf):
int val = 1234; char buf[50]; snprintf(buf, 50, "@ni1 VAL:%d\n\r", val); mqttClient.publish(&In[0], buf);
You could also build a String object:
String str = "@ni1 VAL:" + String(val) + "\n\r"; mqttClient.publish(&In[0], &str[0]);
Regards,
kl3m3n -
Hello @kl3m3n
This works and is simpler. You are the best!
String str = "@ni1 VAL:" + String(val) + "\n\r";
mqttClient.publish(&In[0], &str[0]);Best regards