Digitalwrite function

please explain the code of digitalwrite function given on bolt cloud?
function digitalWrite(pin,val){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) {
//document.getElementById(“javascript_response”).innerHTML = “Javascript Response : “+xmlhttp.responseText;
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();
}

Send the ss of code.

Hi,
Digitalwrite sets the particular pin in the wifi module to a high or low value or it is used for writing the status of digital pin. This function has two parameters:pin number and state.

<button onclick="digitalWrite(0, 'HIGH');">ON</button>

<button onclick="digitalWrite(0, 'LOW');">OFF</button>
The above code is used in led controlling project.In the above first line of code the parameter has value 0 and state is HIGH which means that the led is connected to pin0 and led will remain in the ON condition(pin0 will be given HIGH value).The second line of code explains that the led is connected to pin0 and given LOW value which subsequently makes led in the OFF condition.

please explain the code

The XMLHttpRequest object can be used to exchange data with a server. It means that you can update parts of webpage without reloading the page.

  1. var xmlhttp = new XMLHttpRequest(); is used to create xmlhttprequest() object.

When a request to a server is sent, we want to perform some actions based on the response.
2)onreadystatechange event is triggered every time the readyState changes.

3)if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) this means when everything is ok then perform the ‘if’ part of code.

4)Then alert message with text “javascript_response” will get displayed as you have passed here //document.getElementById

5)Then JSON.parse(xmlhttp.responseText); this line is to read data from a web server, and display the data in a web page.

6)xmlhttp.open is used perform action on server before sending the response.
It uses 3 parameter:
i)GET or POST(GET is simpler and faster than POST, and can be used in most cases.)
ii) url : the file location
iii)true (asynchronous) or false (synchronous)

7)xmlhttp.send(); Sends the request to the server

Hi @jain.reechika,

Please check this thread What is boltCommands.js?

Do let me know in case you need any other information.