Java Script with Proximity Sensor [Resolved]



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);


In the above function this line is executed first“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


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?


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


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!



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


Thanks its working!

Appreciate your good responses!!


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



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


in cocsole it says Uncaught syntaxError: unexpected error


@akshaykumar.byndoor Please share screenshot of console output.


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


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



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

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

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


Hi @datta.naik,

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




Sensor data

Read Sensor on PIN-4

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



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.




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


Try this solution


where does the value get stored and how to access it


Its in cache memory i guess!!