Control widget to publish a simple text string


#1

Hi,

I have what will hopefully be a pretty simple question… I’d like to create a control widget that basically operates exactly like the standard “switch” widget, only instead of sending a 1 byte message as the current switch widget does (a “0” or “1”), I’d like to send my own text message. Looking at the examples online the closest I could find was “Example 2: Simple text box to set a variable” from the document “HTML Canvas Widget Examples”, though this appears to set a variable vs publish a value. Is there another example out there that publishes a value or is there a simpler approach you can point me to (such as the actual code for the switch widget)? Thanks!


#2

Greetings, you may modify the example that you reference to send the value of the text box in the variable’s context, in that way you may store your custom string. The Json that you should build is as follows:

{'my-variable': {'value': 1, 'context': {'text': 'my-string'}}}

All the best


#3

Hi,

Thanks for the quick reply! Since I’d posted that initial query I’d found “https://ubidots.com/docs/hw/#retrieve-data-subscribe” which (I think) clarified things a bit. I’m not really java knowledgeable (mostly c++) so I think if I understand correctly the java code in the example will update the variable which will automatically be sent to anyone that has subscribed to that variable. So, to first just be sure I understood that correctly I created the widget per the instructions on my dashboard, changing only the variable line to “var VARIABLE_ID = ‘CustomControl’;” and the token line to “var TOKEN = ‘BBFF-…’;” (with my login token). But I’m stuck on what I should be subscribing to in my client. Since the widget isn’t really connected specifically to any device I’m not sure what goes in the ??? space for device id -> “/v1.6/devices/???/CustomControl/lv”. I’ve tried several things but I’m not getting messages delivered when I click the widget. If I can get the number messages delivered I can take a look at your suggestion and hopefully figure out how to get some text sent. Anyway, thanks again for your help!


#4

you are mixing concepts, the variable ID is an unique alphanumeric identifier in the whole Ubidots ecosystem:

while the Variable API Label (like customControl) is a custom identifier for data ingestion or retrieve that you may set:

Said this, you must replace at the VARIABLE_ID field the unique ID of your variable, not its API Label.

Referencing our docs, the topic structure is as follows:

/v1.6/devices/{DEVICE_LABEL}/{LABEL_VARIABLE}/lv

so you should replace it with your device label and variable label.

All the best


#5

Fantastic! Thanks again for the help. Pretty much straight away it works. Now I’ll mess with it to see if I can change the data from numeric to string. Thank you again!


#6

Hmmm, after a few hours of messing with it I’m thinking maybe my initial question wasn’t clear. I found one of your other posts where you explained the changes to the html to accept text instead of numbers -


<p><div class="notification--container" id="notification">
  <p>Value sent successfully</p>
</div>

<div class="send-value--container">
  <input type="text" placeholder="Enter text" class="send-value--input" id="value" />
  <button type="button" class="send-value--button" id="send-value">Send value</button>
</div></p>

that worked, and then I tried many permutations of the line you gave me ({‘my-variable’: {‘value’: 1, ‘context’: {‘text’: ‘my-string’}}}) and eventually came up with the following line replacement in the javascript -

changed -

 data: JSON.stringify({ value: parseFloat($value.val(), 10) }),`

to -

 data: JSON.stringify({ value: 1, context: {text: $value.val()} }),`

of all the permutations I tried this was the only one that would update the variable successfully. Looking at the data an example line reads -

DATE                         VALUE      CONTEXT
2019-05-06 16:52:04 -07:00   1.00       {"text":"abc123"} 

but after all that, no matter what I’ve tried, the payload that arrives at my client application only consists of the value string (i.e. “1”). Is that correct? I probably misunderstood but I thought the payload would include or be replaced by the context text? Anyway, again thanks for your help…


#7

Hi there @ctbee , my apologies, it was my mistake in my last post. You should not use ‘lv’ at the end of the topic, lv means that you just wish to retrieve the last value of your variable. To retrieve the context, please use the topic below:

/v1.6/devices/{DEVICE_LABEL}/{LABEL_VARIABLE}

All the best


#8

Thank you! No need to apologize, it’s probably documented somewhere and I just missed it. At any rate changing that worked straight away. So thanks…

I have a little IoT edge platform (www.omniot.com) that I’m developing that I’ve just added mqtt support to so users can send their sensor data to cloud platforms/dashboards without doing any coding. The mqtt support is not released yet but when I do (next few weeks) I’ll add a little tutorial on building out ubidots dashboards and publishing/visualizing data from our platform to yours. So anyway, again, thanks for your help!


#9

Hi, oddly I logged out of my account and can’t log back in. It keeps letting me change my password but then won’t accept the new password (or what I’m pretty sure the original password was)…


#10

Hi @ctbee, if you are using an educational account please make sure to log in at https://app.ubidots.com, if you are an Industrial user please log in at https://industrial.ubidots.com

All the best