Mqtt connection error

#1

Hi,
I am running mqtt-client locally on my machine. I want to trigger the device.on(‘command’,fun(command){})
on click of a button in my experience view page other than dashboard. How to do that?
Kindly suggest.

0 Likes

#2

The recommended approach would be for the button in the Experience Page to invoke an ajax call to an Experience Endpoint. The workflow triggering on that endpoint can then use a Device Command node to send the command.

So the HTML in the Experience Page would look something like this:

<script>
$('#myButton').click(function() {
  $.ajax({
    method: "POST",
    url: "/command",
    data: { foo: "bar" }
  }).done(function( msg ) {
    alert( "Response: " + msg );
  });
});
</script>

<button id="myButton">Send Command</button>

In this example I have an Experience Endpoint with the route set to /command. When this button is clicked, it will invoke that route and the workflow for that endpoint. The workflow will automatically parse the data and add it to the payload, so you can pass whatever information you need from the Experience in order to correctly trigger the command.

0 Likes

#3

The Experience we bootstrap for you automatically includes jQuery. Here’s the documentation for the ajax call.

http://api.jquery.com/jquery.ajax/

0 Likes

#4

Thanks Brandon!
Just need a small info,
Is there anyway by which, I can be able to display the response of my command into my experience view page, without reloading the page itself.

0 Likes

#5

Anything the workflow replies with using the Endpoint Reply node is available in that msg object in the above example. You can use jQuery to update any element on the page without refreshing.

<script>
$('#myButton').click(function() {
  $.ajax({
    method: "POST",
    url: "/command",
    data: { foo: "bar" }
  }).done(function( msg ) {
    $('#myElement').text(msg); // setting the text on myElement
  });
});
</script>

This is, however, a response from the workflow, and not a response from the device. There is currently no direct way for a device to respond to a command. Commands are one-way communication from the cloud to the device.

0 Likes