Interfacing Proximity Sensor with Bolt

can any one help me with a simple code to read from a proximity sensor

@rohitshinde356 Can you please share what you have done till now and we will help you out.

Bolt IoT

Bolt - Your Remote to Control your world

Read Sensor

this code works but i want to access the value of the proximity sensor so that i can use it in an IF statement to trigger an LED. please help

Capture

@shoeb.ahmed @PPV please respond

@rohitshinde356 The code that you have written in incomplete. You would require the boltCommands.js library to perform operations like digitalRead, analogRead etc.
You will need to include the following lines in your code head to get the library.

<script src="https://cloud.boltiot.com/static/js/boltCommands.js">

Also, the digitalRead() function that you have used will not do anything on your UI as it will just make the call. You will need to read the response from the function call and then have some logic to display it on your UI.

Also, for debugging, it is also recommended to open the Developer Console in your browser to see what error you are facing. Have a look at this video,

Capture

@shoeb.ahmed what changes should i make to this code? is it the correct way?

@rohitshinde356 As I had suggested earlier, you would require to import the boltCommands.js library using the syntax I have given. This needs to be in the < head > of your HTML code.

Also, you will need to receive the value given by the response of the digitalRead() output and parse it to get the value of the sensor.

<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT</title>
<script src="serveFile?filename=bolt.js"></script>
</head>
<script>
function digitalReadcustom(pin,element_id) {
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = function() {
    		//document.getElementById(element_id).innerHTML = xmlhttp.responseText;
    		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	    		//document.getElementById("javascript_response").innerHTML = "Javascript Response : "+xmlhttp.responseText;
	    		var obj = JSON.parse(xmlhttp.responseText);
	    		if(obj.success=="1")
	    		{
	    			document.getElementById(element_id).innerHTML = "Pin Val = "+obj.value;
				if(obj.value==0)
				{
					digitalWrite(0,HIGH);
				}
				else
				{
					digitalWrite(0,LOW);
				}
	    			
	    		}
	    		else{
	    			document.getElementById(element_id).innerHTML = "Error = "+xmlhttp.responseText;
	    		}
	    	}
		};
		xmlhttp.open("GET","/digitalRead?pin="+pin,true);
		xmlhttp.send();
	}


	
	function myFunc()
	{
		digitalReadcustom(4,'output')		
	}
	
	 
  	

</script>
<body>
<center>
<h1>Bolt - Your Remote to Control your world</h1>
  <button onclick="myFunc()">Read Sensor<br>
<span id="output">


</span>
</center>
</body>
</html>

@shoeb.ahmed i tried this code and it works but is slow is there any way we can speed up the process?

@rohitshinde356 Can you please open the console debugger and let me know how much time it is taking to do the request?

Capture

<html>
<head>
<title>Bolt IoT</title>
<script src="serveFile?filename=bolt.js">setDebug(true);</script>
</head>

<body>
<style>
.button {
  background-color: white;
font-family: 'BankGothic MD BT';font-size: 20px;
  border: none;
  color: black;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 16px;
}

.button:hover {background-color: gray}

.button:active {
  background-color: black;
  box-shadow: 0 5px #666;
  color: white;
  transform: translateY(4px);
}
#grad1 {
  height: 500px;
  background-color: red; /* For browsers that do not support gradients */
  background-image: linear-gradient(to bottom right, blue,aqua); /* Standard syntax (must be last) */
</style>

</style>
<script>
var myVar;
function digitalReadcustom(pin,element_id) {
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = function() {
    		//document.getElementById(element_id).innerHTML = xmlhttp.responseText;
    		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	    		//document.getElementById("javascript_response").innerHTML = "Javascript Response : "+xmlhttp.responseText;
	    		var obj = JSON.parse(xmlhttp.responseText);
	    		if(obj.success=="1")
	    		{
	    			document.getElementById(element_id).innerHTML = "Pin Val = "+obj.value;
				if(obj.value==0)
				{
					digitalMultiWrite([0,A1],[HIGH,HIGH]);
				}
				else
				{
					digitalMultiWrite([0,A1],[LOW,LOW]);
				}
	    			
	    		}
	    		
	    	}
		};
		xmlhttp.open("GET","/digitalRead?pin="+pin,true);
		xmlhttp.send();
	}


	function myFunc()
	{
		digitalReadcustom(4,'output');
	}
	function contRead()
	{
		myVar=setInterval(myFunc,3000);
	}
	function stopRead()
	{
		clearInterval(myVar);
	}
</script>

<center>
<h1></h1><div id="grad1" style="text-align:center;margin:auto;font-size:50px;font-weight:bold;font-family: 'Times New Roman';">
  Threat Detection System<br> 
<button class="button"; onclick="contRead()">Read Sensor<br>
<span id="output">

<button class="button"; onclick="stopRead()">Stop Sensor</button>

</centre>
</body>
</html>

@shoeb.ahmed i have made some more changes to the code so that automatically reads after 3 seconds but after it executes the if statement it reads twice , please take a look at the code and help.

@rohitshinde356 I do not think there is anything wrong with the code. I believe this issue might happen if you click twice on the Read Sensor button.
You should be adding a check inside the contRead() button to make sure that the myVar is not null before adding the interval.