Java Script with Proximity Sensor [Resolved]


#21

Hi,

Bolt.js uses Javascript and xmlhttprequests (similar to ajax).

I’ll explain one function which you can then co-relate and understand the others…

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;
var obj = JSON.parse(xmlhttp.responseText);
if(obj.success==“1”)
{
alert(obj.value);
}

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

In the above function this line is executed first xmlhttp.open(“GET”,"/digitalWrite?pin="+pin+"&state="+val,true);

Where in it makes a http get request from your browser without typing in the URL the command /digitalWrite?pin=x&state=y

xmlhttp.send(); This line executes the request

xmlhttp.onreadystatechange = function() This function is called automatically once the above request is executed on bolt and the data is sent back to the this script

if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && debug == 1) This line check if the reply is valid

Inside the If loop the data sent from bolt which is JSON format is separated based on the “success” field data and the “value” field data present in the JSON string


#22

Sir thanks.
But my real queation is that where is code which declares the pin as analog or digital or pwm, code which starts serial comm like we used to write for Arduino in the beginning of program i.e. the declerations and the libraries.(the basic microcontroller code)
Please reply.
Is it inbuilt?
I dont get it?


#23

Yes that is inbuilt in the bolt fimware… these are the API used to call the functions in the firmware which will carry out necessary tasks


#24

Yes it working now…

Does the output displayed as expected in the program because in the tutorial its said it will display as below,

  • {“success”:“1”,“value”:“0”} - If there is no obstacle in front of it.
  • {“success”:“1”,“value”:“1”} - If there is obstacle in front of it.

But in my case, it works the reverse as in the screen shot,

If there is no obstacle in front of it,

And if there is obstacle in front of it,

Also does the span function works to display the output adjacent the button??

Sorry I am new to html and web pages!


#25

Hi,

Sometimes the sensors come as active HIGH sensors (High when obstacle) instead of active LOW. Don’t worry much about this as long as the sensor can correlate to a certain value HIGH/LOW and Obstacle being detected or not.

Also regrading span add a “br” tag after the /button tag to get the value to the next line


#26

Thanks its working!

Appreciate your good responses!!


#27

SIR I am not able to get output in proximity sensor using java script, I tried all the corrections given above please help


#28

@akshaykumar.byndoor,

Open console in chrome and see if any error or warning message is there.


#29

in cocsole it says Uncaught syntaxError: unexpected error


#30

@akshaykumar.byndoor Please share screenshot of console output.


#31

<img src="/uploads/default/original/1X/6222372a81d7c774f3a93febef68a4ae7fe95e38.png" width=“666” height=“450”


#32

@akshaykumar.byndoor , share your code with us , think there is some extra character in javascript code.


#33


#34

@akshaykumar.byndoor Try this code. I have tested this code on my bolt and it is working fine.

<!doctype html>
<html>
<head>
<title>BOLT</title>
<script type="text/javascript" src="/serveFile?filename=bolt.js"> setDebug(true);  </script>     
</head>
<body bgcolor="#01FFFF" > 
<center>
<h3> Sensor data</h3> 
<button onclick="digitalRead(4,'output');"> Read Sensor on PIN-4 <br> 
</button> 
<span id="output"> </span> </center> 
</body> 
</html>

In your onclick=“digitalRead(4,“output”);” , put output in single quotes. It should be "digitalRead(4,‘output’);"


#35

Hi @datta.naik,

For me, Nothing is happening, when I am clicking the Read Sensor on PIN-4 Button.

       



BOLT
     

 

Sensor data

Read Sensor on PIN-4

Could you please review my Code and I don’t find any error in HTML Inspect Section.

Thanks,
Prateek


#36

Hi @rahul.singh,

I have tried above code, but it doesn’t worked. I mean, nothing was happening, When I was clicking Read Sensor Button. Please help me out.

Thanks,
Prateek


#37

@prateek.elex,

First, try that digitalRead is working or not.To do so
type bolt_ip/digitalRead?pin=4 and let me know the output.


#38

Try this solution


#39

where does the value get stored and how to access it


#40

Its in cache memory i guess!!