try this:
#include "Particle.h"
#include <math.h>
int pin = D1;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup()
{
Serial.begin(9600);
pinMode(pin,INPUT);
starttime = millis();//get the current time;
}
void loop()
{
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
Serial.print(lowpulseoccupancy);
Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
lowpulseoccupancy = 0;
starttime = millis();
}
}
and connect your output (PIN4 output ) yellow wire to D1 on your photon
UPDATE
and here is code with Particle.variable()
and Particle.publish()
(code above just prints data to Serial)
with JSON formatted data:
#include "Particle.h"
#include <math.h>
char resultstr[128];
int pin = D1;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup()
{
Particle.variable("result", resultstr);
Serial.begin(9600);
pinMode(pin,INPUT);
starttime = millis();//get the current time;
}
void loop()
{
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy+duration;
if ((millis()-starttime) > sampletime_ms)//if the sampel time == 30s
{
ratio = lowpulseoccupancy/(sampletime_ms*10.0); // Integer percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
Serial.print(lowpulseoccupancy);
Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
sprintf(resultstr, "{\"lowpulseoccupancy\":%d,\"ratio\":%.02f,\"concentration\":%.02f}", lowpulseoccupancy, ratio, concentration);
Particle.publish("reading", resultstr);
lowpulseoccupancy = 0;
starttime = millis();
}
}
here are explanation what is the lowpulseoccupancy
, ratio
and concentration
for PPD42NS Grove Dust Sensor