14 Replies Latest reply on Nov 1, 2016 1:25 PM by Intel Corporation

    Read onboard LED status on Edison

    varuntayur

      I am trying to read the status of the onboard LED on Edison. Before executing this code, I ensure that that the LED is Green and glowing. As soon as this code executes, the LED which is ON turns off.

       

      var mraa = require('mraa'); //require mraa
      console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
      
      var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Intel Galileo Gen2 as well as Intel Edison)
      myOnboardLed.dir(mraa.DIR_IN); //set the gpio direction to output
      var ledState = true; //Boolean to hold the state of Led
      
      periodicActivity(); //call the periodicActivity function
      
      function periodicActivity()
      {
          console.log('Status:' + myOnboardLed.read());
      }
      

       

      output

      MRAA Version: v1.3.0
      Status:0
      
        • 1. Re: Read onboard LED status on Edison
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi varuntayur,

           

          Just to clarify:  you say that the LED is green and glowing, does this mean the LED is blinking already or that it’s on a steady green light? How are you running the code, are you using an IDE? What image version are you using, you can check with cat /etc/version.

           

          I looked at your code and it seems that there’s a section missing, the code to get the on board led to blink should be the following:

           

          var mraa = require('mraa'); //require mraa
          console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
          
          
          var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Intel Galileo Gen2 as well as Intel Edison)
          myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output
          var ledState = true; //Boolean to hold the state of Led
          
          periodicActivity(); //call the periodicActivity function
          
          function periodicActivity()
          {
            myOnboardLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low)
            ledState = !ledState; //invert the ledState
            setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds)
          }
          

           

           

          Also, it’s recommended you update the MRAA and UPM libraries with
          1. echo "src maa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf
          2. opkg update
          3. opkg upgrade

           

          We’ll be waiting for your results.

           

          -Sergio

          • 2. Re: Read onboard LED status on Edison
            varuntayur

            Thanks for your response. I am currently not using any IDE. I am executing it on an SSH terminal.

             

            Board version : 201606061707

             

            I ran the update commands for the mraa and upm. The result is the same. Please note I am not trying the "blink" example. I am just trying to read the status of the LED onboard (named DS2) the Intel Edison board.

             

            Problem: I ensure the onboard LED is glowing green before trying to read the Pin status. As soon as the pin.read() API is called the LED which is glowing simply turns off. So, the read() returns the status as 0 always. The read() is supposed to just read the status instead it is having some side effect turning off the LED and then returning the status as 0.

            • 3. Re: Read onboard LED status on Edison
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi varuntayur,

               

              I ran some tests on my board using the latest image and the read() API seems to work correctly. First, I ran the code you’re using and just as you describe, it always returns a Status 0. I then ran the code from my first reply and the LED DS2 started to blink as expected. I then tried the read function along with the blink example. The code is basically the same, just add the read() API at the end of the periodicActivity() function. The result of this was the status going from 0 to 1 alternately. The program ran successfully and displayed a 1 every time the DS2 LED was on. How are you ensuring the LED is on and at the same time reading its value?

              To see this, add the read() API to the periodicActivity() such as:

              function periodicActivity() 
              { 
                myOnboardLed.write(ledState?1:0); //if ledState is true then write a '1' (high) otherwise write a '0' (low) 
                ledState = !ledState; //invert the ledState 
                setTimeout(periodicActivity,1000); //call the indicated function after 1 second (1000 milliseconds) 
              console.log('Status:' + myOnboardLed.read()); 
              } 
              

               

              I hope you find this helpful.

               

              -Sergio

              • 4. Re: Read onboard LED status on Edison
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hi varuntayur,

                Do you still need assistance with this case?

                -Sergio
                 

                • 5. Re: Read onboard LED status on Edison
                  varuntayur

                  Sorry for the delayed response.

                   

                  Here is my code: to Turn On/read pin DS2 , Turn on works perfectly fine. The only problem is while reading the pin status.

                   

                  app.js

                  var express = require("express");
                  var bodyParser = require("body-parser");
                  var app = express();
                  var mraa = require('mraa');
                  app.use(bodyParser.json());
                  app.use(bodyParser.urlencoded({ extended: true }));
                  var routes = require("./routes.js")(app,mraa);
                  var server = app.listen(3000, function () {
                      console.log("Listening on port %s...", server.address().port);
                  });
                  

                       routes.js

                   

                    app.post("/bulb/:id", function(req, res) {
                                  if(req.params.id === undefined){
                                          res.json({error: "missing value"});
                                  }
                                  else{
                                          // connect the mraa lib to the GPIO needed
                                          var currentPin = new mraa.Gpio(parseInt(req.params.id));
                                          // set it in output to write data
                                          currentPin.dir(mraa.DIR_OUT);
                                          console.log("recvd. value param -> " + parseInt(req.param("value")));
                                          // write the output needed
                                          currentPin.write(parseInt(req.param("value")));
                                          // send response to client
                                          res.json({value: req.params.value,id: req.params.id, dir: "out", pinstatus:"high"});
                                  }
                          });
                  
                       app.get("/bulb/:id", function(req, res) {
                                  if(req.params.id === undefined){
                                          res.json({error: "missing value"});
                                  }
                                  else{
                                          // connect the mraa lib to the GPIO needed
                                          var currentPin = new mraa.Gpio(parseInt(req.params.id));
                                          // set it in input to read data
                                          currentPin.dir(mraa.DIR_IN);
                                          var digValue = currentPin.read();
                                          console.log('current o/p:'+ digValue);
                                          // send response with reading the GPIO
                                          res.json({value: digValue ,id: req.params.id, dir: mraa.DIR_IN});
                                  }
                          });
                  

                   

                  I do this from POSTMAN -

                  POST http://192.168.1.38:3000/bulb/13
                  BODY:
                  value:1
                  

                  --------- after this the DS2 led glows green ----------------------------

                   

                  Next I do this from POSTMAN -

                   

                  GET http://192.168.1.38:3000/bulb/13
                  

                   

                  -------- BOOM -> Light turns off.

                  • 6. Re: Read onboard LED status on Edison
                    Intel Corporation
                    This message was posted on behalf of Intel Corporation

                    We’ll look into this to see if we can find useful information. We’ll come back as soon as we have an update.

                    -Sergio
                     

                    • 7. Re: Read onboard LED status on Edison
                      Intel Corporation
                      This message was posted on behalf of Intel Corporation

                      Hi varuntayur,

                      Thank you for your patience.
                      This seems to be an issue with the code and the configuration with the pin. When using POST the LED is expected to blink. The pin is configured as an output and given a value of 1. At this point the LED should be glowing. When you get to the GET section, the pin is no longer configured as an output, but as an input. While the pin is in an input state, the LED will not be glowing. This is why when you read the LED is off.

                      Edit the code to avoid this condition. We’ll be waiting for your results.

                      -Sergio
                       

                      • 8. Re: Read onboard LED status on Edison
                        varuntayur

                        Intel Corporation wrote:

                         

                        Hi varuntayur,

                        Thank you for your patience.
                        This seems to be an issue with the code and the configuration with the pin. When using POST the LED is expected to blink. The pin is configured as an output and given a value of 1. At this point the LED should be glowing. When you get to the GET section, the pin is no longer configured as an output, but as an input. While the pin is in an input state, the LED will not be glowing. This is why when you read the LED is off.

                        Edit the code to avoid this condition. We’ll be waiting for your results.

                        -Sergio

                        Dear Sergio,

                         

                        Sorry, I didn't still understand how to read the status of the PIN - if it is glowing before I try to read the status - why is it turning off the PIN - it should simply give me a status as "1". That is my requirement too - Assume - I need to show the status of this PIN in the GUI. If the status is "1" - i show "ON" if it is "0" i show "OFF".

                         

                        Are you saying lien 14 should be "OUT" direction for executing the read() API on the pin?

                         

                        if(req.params.id === undefined){  
                          res.json({error: "missing value"});  
                        }  
                        else
                        {  
                          // connect the mraa lib to the GPIO needed
                        
                        
                          var currentPin = new mraa.Gpio(parseInt(req.params.id));  
                        
                          // set it in input to read data
                        
                        
                          currentPin.dir(mraa.DIR_IN);  
                        
                          var  digValue = currentPin.read();  
                        
                          console.log('current o/p:'+ digValue);  
                        
                          // send response with reading the GPIO
                        
                        
                          res.json({value: digValue ,id: req.params.id, dir: mraa.DIR_IN});  
                        }  
                        
                        • 9. Re: Read onboard LED status on Edison
                          Intel Corporation
                          This message was posted on behalf of Intel Corporation

                          Hi varuntayur,

                          Please let me know if I’m understanding your concern correctly. From what I understand from your case: no, line 14 shouldn’t be set as an output. If the pin is set as an output then the LED is going to blink, but you’re not going to be able to read it using the same pin. Here’s an example to read digital values: https://github.com/gomobile/iotapp-template-digital-read/blob/master/main.js . Notice the gpio is set as an input, and then the read function is called. This example works properly to read digital values.

                          I believe your problem is you’re trying to write and read at the same time with the same pin. To read, the pin needs to be set as an input and, to write you need to set the pin as an output, but the pin can’t be set as both at the same time. I’d recommend you to look at the blink example from our first reply and at the link shared in this post (digital read example) and adjust your code to read the value of the link in the way showed in the example.

                          Let us know your results or if I’m misunderstanding your question.

                          -Sergio
                           

                          • 10. Re: Read onboard LED status on Edison
                            varuntayur

                            Intel Corporation wrote:

                             

                            Hi varuntayur,

                            Please let me know if I’m understanding your concern correctly. From what I understand from your case: no, line 14 shouldn’t be set as an output. If the pin is set as an output then the LED is going to blink, but you’re not going to be able to read it using the same pin. Here’s an example to read digital values: https://github.com/gomobile/iotapp-template-digital-read/blob/master/main.js . Notice the gpio is set as an input, and then the read function is called. This example works properly to read digital values.

                            I believe your problem is you’re trying to write and read at the same time with the same pin. To read, the pin needs to be set as an input and, to write you need to set the pin as an output, but the pin can’t be set as both at the same time. I’d recommend you to look at the blink example from our first reply and at the link shared in this post (digital read example) and adjust your code to read the value of the link in the way showed in the example.

                            Let us know your results or if I’m misunderstanding your question.

                            -Sergio

                            My requirement is very simple, like i have stated above. I would like to know whether Pin 13 is on(or off). If Pin 13 is On, I would like to toggle the status to Off. I wish to do this not in a loop/periodic activity as explained by you. It is driven by the user, think of this as a button on some UI.

                             

                            To know what is the status of the Pin - I have a GET API - which is trying to read the status of the PIN, what I am feeling to understand now is that the read(), write() are not to be performed on the same PIN. So, if read() is done, the status must be saved internally and if the PIN needs to be toggled the internal state is to be used to write(). So, developing a REST API like the one that I am doing is a bad idea as REST is supposed to be 'stateless'.

                            • 11. Re: Read onboard LED status on Edison
                              Intel Corporation
                              This message was posted on behalf of Intel Corporation

                              Hi varuntayur,

                              To know whether pin 13 is on or off you can add a new variable to your code. Pin 13 is set as an output and can be either On (1) or Off (0). This variable will be called after setting pin 13 to on or off. So basically pin 13 is set to a value of 1, the LED glows and this new variable is assigned a “true” or “on” state. The rest of the code is executed and at the time pin 13 gets a value of 0, the LED will turn off and the variable will be set as a “false” or off value. You can choose the type of this variable as you prefer, it can be a Boolean value, for example. Then, to know the state of the pin at a certain moment you just need to read the state this variable.

                              Let us know if you find this useful.

                              -Sergio
                               

                              • 12. Re: Read onboard LED status on Edison
                                Intel Corporation
                                This message was posted on behalf of Intel Corporation

                                Hi varuntayur,

                                Did you have a chance to try our latest suggestion? Do you still need assistance with this case?

                                -Sergio
                                 

                                • 13. Re: Read onboard LED status on Edison
                                  varuntayur

                                  Thank you. I did try it.

                                   

                                  However, I am not really convinced about maintaining "state" in a REST implementation. I would like to keep it stateless.

                                   

                                  Please close the case. Thanks for your help.

                                  • 14. Re: Read onboard LED status on Edison
                                    Intel Corporation
                                    This message was posted on behalf of Intel Corporation

                                    Hi varuntayur,

                                    Thank you. We’ll proceed to close this ticket now. If you need further assistance don’t hesitate to open a new thread or reopen this one in order to provide continuity to your inquiry.

                                    -Sergio