What is boltCommands.js?

    <script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js">
    </script>
    <script>
    setKey('{{ApiKey}}','{{Name}}');
    </script>

Can anyone explain what exactly do these four lines do ?

hi @abhronilpaul, as you know every bolt wifi module has its own unique id and API key so for this to embed any program in your wifi module via JavaScript we use this command.

in first line we provide url of functions that we will use and in setKey ApiKey resolves to our api key and so is device name is auto-initialized by bolt cloud

Hi @abhronilpaul , @Pritam , @joshibhaumik20

Open this URL in your browser https://cloud.boltiot.com/static/js/boltCommands.js and you can see there are some functions written. These are some prewritten function written so that you can easily use in your code. you can write your own functions also and you don’t need to use Bolt Commands.js once you understand the AJAX.

ApiKey and Name is the API key and device id returned on the control page from Bolt Cloud. You can use it anywhere on your code.

For example

<script>
device_name = '{{Name}}'
alert(device_name)
</script>

In the below line we are calling the setKey() functions and passing API key and device name as parameter.

You can check the definition of functions inside the boltCommands.js. api_key and d_name is global variable.

function setKey(key, dev_name){
    api_key = key;
    d_name = dev_name;
}

I will explain to you one function and after that, it becomes very easy to understand the other code.

Before you start you need to understand the basics of Ajax request.
You can refer the basics from here https://www.w3schools.com/xml/ajax_intro.asp

function digitalWrite(pin,val){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) {
            alert(xmlhttp.responseText);

            var obj = JSON.parse(xmlhttp.responseText);

            if(obj.success=="1"){

                    alert(obj.value);

            }

        }

    };  
    xmlhttp.open("GET", base_url+api_key+"/digitalWrite?pin="+pin+"&state="+val+"&deviceName="+d_name,true);

    xmlhttp.send();    

}

In the digital write function, we are passing the pin number that we want to control and the value of pin number.
and in the next line var xmlhttp = new XMLHttpRequest();
we are creating an object of ajax. and after that control will go to this line

xmlhttp.open("GET", base_url+api_key+"/digitalWrite?pin="+pin+"&state="+val+"&deviceName="+d_name,true);

   xmlhttp.send(); 

Here ware doing the get request to the cloud the cloud. It very similar to typing the URL in the browser and pressing enter button.

For example (https://cloud.boltiot.com/remote/44b2de6b-7e68-40e7-a27f-814b58afe008/restart?&deviceName=BOLT9161541)

And after sending the GET request to cloud the control will go to this section

xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) {
            alert(xmlhttp.responseText);

            var obj = JSON.parse(xmlhttp.responseText);

            if(obj.success=="1"){

                    alert(obj.value);

            }

        }

    };  

Where the actual cloud response will be stored in xmlhttp.responseText

Do let me know in case you need further assistance.

5 Likes