Proximity sensors and relays

I want to use my proximity sensor to switch on and off between the relay, so that I can switch on a bulb. But the proximity sensor will give value “0” only when something is in front of it. Can you help me with the code?
I want to switch the relay and switch on the bike when someone crosses the proximity sensor.

Hi @kandhari78

Reade sensor data every 5 seconds and if there is anything, you can switch on the relay.
Refer this code snippet :

<html>
   <head>
      <script type="text/javascript" src="/serveFile?filename=bolt.js"> </script>
   </head>
   <span id="ouput"> </span>
   <script>
      function digitalReadCustom(pin,element_id) {
              var xmlhttp = new XMLHttpRequest();
              xmlhttp.onreadystatechange = function() {
                  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                      var obj = JSON.parse(xmlhttp.responseText);
                      if(obj.success=="1")
                      {
                          document.getElementById(element_id).innerHTML = "Pin Val = "+obj.value;
                          if ( obj.value==0){
                              //Switch on the Led on 
                              digitalWrite(5, 'HIGH');
                          }
                          else{
                              //Switch off the Led 
                              digitalWrite(5, 'LOW'); 
                          }
                      }
                      else{
                          document.getElementById(element_id).innerHTML = "Error = "+xmlhttp.responseText;
                          return "Error Occured"; 
                      }
                  }
              };
              xmlhttp.open("GET","/digitalRead?pin="+pin,true);
              xmlhttp.send();
          } 
      setInterval(function(){
              //Call this function every after 5 seconds
              digitalReadCustom(4,'output')
          }, 5000);
   </script>
</html>

I tried this code, its’s not working. @rahul.singh

Here is the code I typed and uploaded as index.htm

@kandhari78 Do console log in chrome and see is any error or warning message is there.
I haven’t tested the code. Send me the screenshot of the error.

There is no error msg, the screen is blank infact.

Hi, @ Please don’t upload the screenshot of code.
First copy the code in the box. Select the code and click on </> symbols from text formatting.

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javscript" src="/servefile?filename=bolt,js"> </script>
</head>
<span id="output"></span>
<script>
   function digitalReadCustom(4,proxy)
   {  
      var xmlhttp=new XMLHttpRequest();
         xmlhttp.onreadystatechange=function(){
		 
		 if(xmlhttp.readyState==4 && xmlhttp.status==200){
		 
		 var obj=JSON.parse(xmlhttp.responseText);
		 
		 if(obj.success==1)
		 {
		     document.getElementById(proxy).innerHTML="Pin Val="+obj.value;
			 if(obj.value==0){
			     digitalWrite(5,'HIGH');
			 }
		     else{
			 digitalWrite(5,'LOW');
			 } 
		 
		 }
		 else{
		      document.getElementById(proxy).innerHTML="Error="+xmlhttp.responseText;
			  return"Error Occured";
		 }
		 
		 }
		 
		 };
           xmlhttp.open("GET","/digitalRead?pin=4"+pin,true);
           xmlhttp.send(); 

	}      

        setInterval(function(){
		 
		 digitalReadCustom(4,'output')
		 },5000);
 
 </script>
</html>

Hi @kandhari78,

There is one mistake in your code `function digitalReadCustom(4, proxy)’.
1 You can not use the integer in function definitions.

Also, refer this link to know more about Ajax in HTML https://www.w3schools.com/xml/ajax_intro.asp
Also, do console log to track the debug. Console is nothing but print statement that you can see on Chrome console or Mozilla Console https://stackoverflow.com/questions/4539253/what-is-console-log.

<html>
   <head>
      <script type="text/javscript" src="/servefile?filename=bolt.js"> </script>
   </head>
   <span id="output"></span>
   <script>
      function digitalReadCustom(pin,proxy)
      {  
        console.log("Inside digitalRead function")
         var xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function(){
        
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
        
        var obj=JSON.parse(xmlhttp.responseText);
        console.log(obj)
        if(obj.success==1)
        {
            document.getElementById(proxy).innerHTML="Pin Val="+obj.value;
          if(obj.value==0){
           console.log("Calling Led on funciton on pin 5")
              digitalWrite(5,'HIGH');
          }
            else{
          digitalWrite(5,'LOW');
          } 
        
        }
        else{
             document.getElementById(proxy).innerHTML="Error="+xmlhttp.responseText;
           return"Error Occured";
        }
        
        }
        
        };
              xmlhttp.open("GET","/digitalRead?pin=4",true);
              xmlhttp.send(); 
      
      }      
      
       setInterval(function(){
        console.log("Set setInterval")
        digitalReadCustom(4,'output')
        },5000);
      
   </script>
</html>

First try with simple LED and proximity sensor then try with relay. Also check if bolt.js file is present in Bolt SD card.

Hey @rahul.singh I got this error. For both the code you mentioned above and for the code that I have typed below.

I used the previous code but chnaged the gpio pin value to.
\

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javscript" src="/servefile?filename=bolt,js"> </script>
</head>
<span id="output"></span>
<script>
   function digitalReadCustom(pin,proxy)
   {  
      var xmlhttp=new XMLHttpRequest();
         xmlhttp.onreadystatechange=function(){
		 
		 if(xmlhttp.readyState==4 && xmlhttp.status==200){
		 
		 var obj=JSON.parse(xmlhttp.responseText);
		 
		 if(obj.success==1)
		 {
		     document.getElementById(proxy).innerHTML="Pin Val="+obj.value;
			 if(obj.value==0){
			     digitalWrite(0,'HIGH');
			 }
		     else{
			 digitalWrite(0,'LOW');
			 } 
		 
		 }
		 else{
		      document.getElementById(proxy).innerHTML="Error="+xmlhttp.responseText;
			  return"Error Occured";
		 }
		 
		 }
		 
		 };
           xmlhttp.open("GET","/digitalRead?pin=4"+pin,true);
           xmlhttp.send(); 

	}      

        setInterval(function(){
		 
		 digitalReadCustom(4,'output')
		 },5000);
 
 </script>
</html>

By pin 5, you mean GPIO 0, right? or GPIO 5? Because I can’t use GPIO 5 as no GND is available due to proximity sensor connect there.

@kandhari78,
You can use any GPIO pins.Just change the parameter in digitalWrite(); function .

But I can’t use GPIO pin 5 as the GND pin alongside it is being used by the proximity sensor, so I used the GPIO 0 pin but it shows an error that I have mentioned above. Is the numbering of the pins different for JS and HTML. Because in the LEAD with JavaScript experiment we use the number '4’in digitalWrite but connect the LED to the GPIO 0 pin

Hi @kandhari78
I think there is the typo error in content.
First type the bolt_ip/digitalRead?pin=0
For example : 192.168.43.101/digitalRead?pin=0 and also try with different combinnation and see which one is working.

Ok, what about that error message on the website. What is the reason behind that?
Image above^

Hey @rahul.singh, what do you think of the error message displayed on the screen?
What could be wrong in the code I have used both the codes. Refer above^

Hey @rahul.singh, I still keep getting the error message, any thoughts?

Hi @kandhari78
I got one small typo error in your code.
Have a look at this code snippet.

xmlhttp.open("GET","/digitalRead?pin=4"+pin,true);
The correct syntax will be
xmlhttp.open("GET","/digitalRead?pin=4",true);

If you want to use pin as variable then
xmlhttp.open("GET","/digitalRead?pin="+pin,true);

Hey, @rahul.singh the last edit worked, the proximity sensor displays data every 5 seconds, but the digitalWrite is not being executed, the LED never glows up.

1 Like

Hi @kandhari78
Do console log(Go to View ->Developer -> JavascriptConsole) and check os there is any error or warning message. Send me the screenshot of your console.