DexterInd / GrovePi

GrovePi is an open source platform for connecting Grove Sensors to the Raspberry Pi.
https://www.dexterindustries.com/grovepi/
Other
490 stars 487 forks source link

Cannot get started with node.js sample #438

Open wmmihaa opened 5 years ago

wmmihaa commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

marcellobarile commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

RobertLucian commented 5 years ago

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.