Dust Sensor with nodejs

Hello,

I tried to make the dust sensor working from nodejs.
http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor
I mapped the Arduino Sketch to nodejs, but i can not replicate the pulseIn function in nodejs.

Do you have any tip how i can have a custom pulseIn function in nodejs?

What i have so far:

var DigitalSensor = require('./base/digitalSensor')
var commands     = require('../commands')
var duration;
var starttime;
var sampletime_ms = 30000;//sampe 30s ;
var lowpulseoccupancy = 0;
var ratio = 0;
var concentration = 0;

function DustDigitalSensor(pin) {
	DigitalSensor.apply(this, Array.prototype.slice.call(arguments));
  
    starttime = new Date().getTime();
	var write = this.board.writeBytes(commands.dRead.concat([this.pin, commands.unused, commands.unused]))
	
	if (write) 
	{
		this.board.wait(200)
		var singleByte = this.board.readByte()
		var bytes = this.board.readBytes();
		
		setInterval(function ()
		{
			duration = 10; //pulseIn(pin, 0, 9999999);  this function is missing
			lowpulseoccupancy = lowpulseoccupancy+duration;

			if ((new Date().getTime() - starttime) > sampletime_ms)
			{
				ratio = lowpulseoccupancy/(sampletime_ms*10.0); 
				concentration = 1.1*Math.pow(ratio,3)-3.8*Math.pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
				console.log('Dust Sensor concentration: ' + concentration + ' pcs/0.01cf');
				lowpulseoccupancy = 0;
				starttime = new Date().getTime();
			}
		},  100);
	}
}

Thank you and best regards,

Simon

Hey Simon, saw this and I think it might be something you want to post on the Nodejs section of the GrovePi in Github. https://github.com/DexterInd/GrovePi/issues

Hey Simon,
We got the dust sensor working with the GrovePi and the PulseIn in implemented in the firmware now. If you update the firmware on the GrovePi to v1.2.4 from here https://github.com/DexterInd/GrovePi/tree/master/Firmware/Source/v1.2/grove_pi_v1_2_4, you should be able to simply use this example: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_dust_sensor.py to read the data back from the dust sensor.

-Karan

you must caculate the “duration”.
But in your code
duration = 10; //pulseIn(pin, 0, 9999999); this function is missing
duration is set to be a constant.
the following is what I wrote in git, wish it can help.

Hey SunHaoli,
Trying to find the reference to duration=10, all I am able to find is this: https://github.com/DexterInd/GrovePi/blob/master/Firmware/Source/v1.2/grove_pi_v1_2_4/grove_pi_v1_2_4.ino#L546

-karan

Hi! karan. Thank you for your reply.
I was trying to reply to s0h0. “duration=10” was what s0h0 wrote.
The following is what I wrote in github.

And I have a question for you.
“duration = pulseIn(8, LOW);”
Dose this means that if I plug the dust sensor in D8 port, it will work?
And I’m using Raspberry Pi, will this work on RPi too?

Hey SunHaoli,
Sorry for that, I didn;t notice that you were referencing the above post.

I don;t think pulseIn would work properly with nodejs. PulseIn is run on the microcontroller itself and it returns a value in microseconds which is used to calculate the concentation. The NodeJS library is a user controbuted one, but the way it is implemented in the python is that you call a function which sends a command to the GrovePi and the GrovePi calculates the concentation and sends it back. Here is the example: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_dust_sensor.py and here is what is sent to the GrovePi: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grovepi.py#L520-L534. It might be possible to implement something similar with NodeJS. You might have to update the firmware on the GrovePi to v1.2.4 to get the dust sensor working (here is the discussion on the forums about that: http://www.dexterindustries.com/topic/grove-dust-sensor/#post-34648).

Do let us know if this helps.

-Karan

Hi @Karan, just to make sure, you’re mentioning that concentration is returned by the GrovePi

However check here

While in the comments area it is mentioned that concentration is returned, the variable is named ‘lowpulseoccupancy’. Is the variable name wrong?