Proximity sensors and relays


#25

Hey @rahul.singh


#26

There was another tytpo error of “bolt.js”, I thought that’s it, but it gives this same error again of Uncaught ReferenceError.


#27

The file name should be bolt.js and not bolt,js
Hence the code with correction will be:
<script type=“text/javscript” src="/servefile?filename=bolt.js"> </script>


#28

Hey @pranav.kundaikar I took care of that already, but still the digitalWrite is not being executed.


#29

The issue could be due to the bolt.js not being included in the SD card.
Could you open the source code of the page and check if the bolt.js file is actually getting included in the webpage. To check, select the complete link in the <script type="text/javscript" src="/servefile?filename=bolt.js"> </script> open the link and check if it opens the github of the bolt.js file.
If it does not open, then you may need to include the file from here,


#30

The, bolt.js file is present on the SD card. I think there is something wrong with the code, that error on the console log(pic above^), what does it mean @rahul.singh, @pranav.kundaikar @shoeb.ahmed


#31

It just tells that the browser was not able to find the function digitalRead() and hence returned the error.
That’s why we were asking you to check if the bolt.js file is being correctly referenced.


#32

How should I check if the file is being referenced?
@rahul.singh @shoeb.ahmed


#33

Hi @kandhari78
type bolt_ip/serveFile?filename=bolt.js in chrome browser and see if you are able to see the javascript code of bolt.js.
For example: 192.168.2.4/serveFile?filename=bolt.js


#34

@rahul.singh yes sir, I am able to see the javascript code similar as mentioned above by @shoeb.ahmed


#35

in you code in the following lines
digitalWrite(5,'HIGH');
digitalWrite(5,'LOW');
can you remove the quotes around HIGH & LOW and try it again? It should look like this:
digitalWrite(5,HIGH);
digitalWrite(5,LOW);
Just update all the occurences of these types of lines in the code and reupload the code and check if it works and let me know.


#36

No, no change @pranav.kundaikar, LED still dosen’t light up.

have a look at the code

<html>
   <head>
      <script type="text/javscript" src="/servefile?filename=bolt.js">setDebug(true); </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 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",true);
              xmlhttp.send(); 
      
      }      
      
       setInterval(function(){
        console.log("Set setInterval")
        digitalReadCustom(4,'output')
        },5000);
      
   </script>
</html>

#37

Hey, please look at the error, it says, digitalWrite is not defined at XMLHttpRequest.xmlhttp.onreadystatechange

What does it mean?
@pranav.kundaikar @rahul.singh @shoeb.ahmed


#38

Hi,
can you add this code to see if the LED glowing works.

<html>
   <head>
      <script type="text/javscript" src="/servefile?filename=bolt.js">setDebug(true); </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 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",true);
              xmlhttp.send(); 
      
      }      

      function digitalWriteCustom(pin, state)
      {  
        console.log("Inside digitalWrite 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)
        {
             alert('digitalWrite successfull');
          }
            else{
             alert('digitalWrite unsuccessfull');
          } 
        
        }
        else{
             alert('Some error has occurred please try again');
        }
        
        }
        
        };
              xmlhttp.open("GET","/digitalWrite?pin="+pin+"&state="+state,true);
              xmlhttp.send(); 
      
      }
      
       setInterval(function(){
        console.log("Set setInterval")
        digitalReadCustom(4,'output')
        },5000);
      
   </script>
   <body>
       <button onclick="digitalWriteCustom('0','HIGH')">LED ON</button>
        <button onclick="digitalWriteCustom('0', 'LOW')">LED OFF</button>
    </body>
</html>

Please try this code. I have added code for trying to glow an LED connected to pin 0. Make sure you have connected the longer leg of LED to GPIO 0 and the shorter of the LED to GND pin. Then upload the code and see whether you are able to glow the LED and switch it off using the ON and OFF buttons appearing on the page.

Do let me know if this works.


#39

No, sir its is not working @pranav.kundaikar it gives the error of “Uncaught ReferenceError: digitalWriteCustom is not defined at HTMLButtonElement.onclick”


#40

@kandhari78 Please share your entire code here and also a screenshot of the error. Please copy paste the code so that I can make updates.


#41

I copy-pasted the code that you have mentioned above @pranav.kundaikar. And this is the error on the console log.


#42

can you try the code below and let me know whether it works?
in case of errors please send the console screenshot.
`

LED ON LED OFF `

#44

Hey @pranav.kundaikar , the LED on/off is working but then by using proximity sensor gives the same error.


#45

That’s great. Just replace the following lines present inside the digitalReadCustom function:
digitalWrite(0, HIGH); replace with digitalWriteCustom('0','HIGH')
digitalWrite(0, LOW); replace with digitalWriteCustom('0','LOW')

Just make this update and the proximity sensor should also work without any error.