Writing the JavaScript code to communicate with our MQTT Broker

Now that we have our HTML and CSS files, let's create the JavaScript file that will make the magic of MQTT happen:

  1. Inside the scripts folder, create a file called index.js using an HTML editor.
  2. Type the following into the index.js file and save it:
function moveForward() {    client = new Paho.MQTT.Client("m10.cloudmqtt.com", 38215, "web_" + parseInt(Math.random() * 100, 10));    // set callback handlers    client.onConnectionLost = onConnectionLost;    var options = {        useSSL: true,        userName: "vectydkb",        password: "ZpiPufitxnnT",        onSuccess: sendMoveForwardMessage,        onFailure: doFail    }    // connect the client    client.connect(options);}// called when the client connectsfunction sendMoveForwardMessage() ...

Get Internet of Things Programming Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.