Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get started with node.js sample #438

Open
wmmihaa opened this issue Jan 29, 2019 · 7 comments
Open

Cannot get started with node.js sample #438

wmmihaa opened this issue Jan 29, 2019 · 7 comments

Comments

@wmmihaa
Copy link

wmmihaa commented Jan 29, 2019

I have a newly installed pi3 with node.js 9.9.0 and npm 5.6.0. I created a groove directory and installed the node-grovepi package using:
npm install node-grovepi

I also ran the quick install command:
curl -kL dexterindustries.com/update_grovepi | bash

It seems to all be installed correctly, but I don't quite get it working. When trying the analog light for instance, I keep getting the same value (~20.0), but it makes no difference if I cover the sensor or hold it up to the light. If I put the sensor on a different port I get different readings (~0.01). So it seems like it can read the sensor but are not getting the right values.

If I try the leds nothing happends, not even an error...

var led = new GrovePi.sensors.DigitalOutput(2);
board = new GrovePi.board({...});
led.turnOn();

Appreciate any help ;)

@marcellobarile
Copy link
Contributor

marcellobarile commented Jan 29, 2019

hi @wmmihaa,
please try the example provided in the readme page.

var board = new Board({
    debug: true,
    onError: function(err) {
      console.log('Something wrong just happened')
      console.log(err)
    },
    onInit: function(res) {
      if (res) {
        console.log('GrovePi Version :: ' + board.version())

        var lightSensor = new LightAnalogSensor(2)
        console.log('Light Analog Sensor (start watch)')
        lightSensor.on('change', function(res) {
          console.log('Light onChange value=' + res)
        })
        lightSensor.watch()
      }
    }
  })

ideally, you can't get access to the sensors before the board is not ready yet (due to the async nature of Node.js).

@wmmihaa
Copy link
Author

wmmihaa commented Jan 29, 2019

Thanks @marcellobarile,
That is actually what I do (copied from the sample). For some reason I choose hide it with three dots ;)

@marcellobarile
Copy link
Contributor

alright but then the "led.turnOn" call should be placed inside the "onInit" handler.
You can find something more here: https://github.com/DexterInd/GrovePi/tree/master/Software/NodeJS/tests

@wmmihaa
Copy link
Author

wmmihaa commented Jan 29, 2019

That is the exact sample I'm running:

var GrovePi = require('node-grovepi').GrovePi

// put led in port D3 
var led = new GrovePi.sensors.DigitalOutput(2);

// status will tell us if the led is on or off
var status = 0;

var board;

function toggle() {

    if (status == 0){
        console.log("toggle off");
        led.turnOff();
        status = 1;
    }
    else {
        console.log("toggle on");
        led.turnOn();
        status = 0;
    }
}

function start() {
  console.log('starting')

  board = new GrovePi.board({
    debug: true,
    onError: function(err) {
      console.log('TEST ERROR')
    },
      
    onInit: function(res) {
        console.log("OnInit");
        if (res) {
            // call toggle every second
            setInterval(toggle, 1000)
        }
    }
  })

  board.init();
} // end start()
 

// called on Ctrl-C. 
// close the board and clean up 
function onExit(err) {
  console.log('ending')
  board.close()
  process.removeAllListeners()
  process.exit()
  if (typeof err != 'undefined')
    console.log(err)
}

// starts the test
start()
// catches ctrl+c event
process.on('SIGINT', onExit)

@wmmihaa
Copy link
Author

wmmihaa commented Jan 29, 2019

Could there be any type of configurations of the pi I could have missed? I've enabled ic2 and gpio along with running curl -kL dexterindustries.com/update_grovepi | bash

@wmmihaa
Copy link
Author

wmmihaa commented Jan 30, 2019

Ad I didn't use the designated sd card (lost it), is this image downloadable?

@RobertLucian
Copy link
Contributor

Well, you can download Raspbian For Robots that already has everything in it, or you can get a Raspbian and then run the above command and install it. Regardless, you have to run the same above command in order to get the GrovePi lib (mostly the python stuff) up to date.

Here's the link to the image:
https://www.dexterindustries.com/howto/install-raspbian-for-robots-image-on-an-sd-card/

Also, personally, I'd go with a fresh distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants