Totem of pollution

Hello guys,
This is my first project with the photon. I am trying to build a totem of pollution for a school around my work place. I am using as sensor :

Shinyei PPD42
Grove Air quality sensor V1.3
Barometer sensor BME280
Sunlight sensor V1

And as shield

Spark battery shield and grove base shield.
The code is taken from the grove website, from the examples for arduino.
Most of the sensor give normal reading but for the past 3 days, I’m not able to make the Air quality sensor working.

Here is my .ino code

#include "AirQuality.h"
#include "application.h"
AirQuality airqualitysensor;
int current_quality =-1;
void setup()
{
    Serial.begin(9600);
    airqualitysensor.init(14);
}
void loop()
{
    current_quality=airqualitysensor.slope();
    if (current_quality >= 0)// if a valid data returned.
    {
        if (current_quality==0)
        Serial.println("High pollution! Force signal active");
        else if (current_quality==1)
        Serial.println("High pollution!");
        else if (current_quality==2)
        Serial.println("Low pollution!");
        else if (current_quality ==3)
        Serial.println("Fresh air");
    }
}
// ISR(TIMER2_OVF_vect)
// {
//     if(airqualitysensor.counter==122)//set 2 seconds as a detected duty
//     {
//
//         airqualitysensor.last_vol=airqualitysensor.first_vol;
//         airqualitysensor.first_vol=analogRead(A0); // change this value if you use another A port
//         airqualitysensor.counter=0;
//         airqualitysensor.timer_index=1;
//         PORTB=PORTB^0x20;
//     }
//     else
//     {
//         airqualitysensor.counter++;
//     }
// }

Any ideas

Does this mean it worked before these three days or that you tried for three days and never got it to work?

BTW, where did you get the library from and how have you included it in your project?
What IDE are you using?
Also how have you wired the sensor and/or Grove Base Shield (which anyway)?

I was never able to make it work.
The libraries are from the official webpage of the sensor
and the wiring is done with the grove cable as in the example. I’ll put a picture soon.

Still need to know

The base shield has multiple ports so you need to select the respective pin in code too.

I’m using particle dev. Through the computer not the one in the webpage. And i have included the .cpp and .h files in the same folder as the .ino

The library needs some corrections and porting to the Particle platform as it uses inconsistend pin usage and also relies on 10bit ADCs while the Photon sports 12bit.

However, this library doesn’t really anything fancy. You should be able to dump it completely and just go with analogRead() and your own range checks.

int analogvalue = 0;

String aString;

void setup()
{
  Particle.variable("analogvalue", analogvalue);

  pinMode(A0, INPUT);
}

void loop()
{

  analogvalue = analogRead(A0);
  delay(200);
}

do you mean something like this

Something like this, but you can/should omit the pinMode() for analogRead().
And after you got some reference readings (in the range 0…4095) this way you may want to add some categorisation to put the values in a more readable/understanable range:

with the code like this, i do not obtain any readings

int analogvalue = 0;


void setup()
{
  Particle.variable("analogvalue", analogvalue);
}

void loop()
{
  analogvalue = analogRead(A0);
    Particle.variable("analogvalue", analogvalue);
  delay(200);
}

@E404, Particle.variable() is only declared in setup() once. After that, any changes to that variable will be reflected when the variable is queried through the cloud.

You may want to add a Serial.print() statement to print out the value on a serial console to validate the value as well.

1 Like

You had that Particle.variable() right before, why change it?

We are still waiting for that

You mean to leave the particle.variable() in the setup only ?

Also another small question from the I2C sensor am i limited to only 4 variables?

Because as you can see i have 2 I2C sensor and when i put both ino codes on one single one then it only outputs 4 variables, even though i have more then data .

Yup, only in setup().

I’m not sure what you mean by this. Perhaps showing your entire code would help. I’ll have to look at the Grove code to get a sense of what is required.

But can I put the particle.publish() in a loop??

I just realized that the photon can only publish 4 variables at the same time. Is that right ?

void setup() {
  Serial.begin(9600);

//For Dust
  pinMode(8,INPUT);
  Particle.publish("Concentration:", String(concentration)+"pcs/0.01cf");
//For Sunlight
  SI1145.Begin();
  Particle.publish("Vis: ", String(SI1145.ReadVisible()));
  Particle.publish("IR: ", String(SI1145.ReadIR()));
  Particle.publish("UV:", String((float)SI1145.ReadUV()/100));
//For Barometer
  Wire.begin();
  Particle.publish("Pressure : ", String(pressure));
  Particle.publish("Temperature in Celsius : ", String(cTemp));
  Particle.publish("Temperature in Fahrenheit : ", String(fTemp));
//Current time
  starttime = millis();
}

I guess your Air Quality Sensor is the one stuck under the remote control, right?

If you want to make more than four values available you could use a string variable which contains all your desired values in one string.
But I think the limit of four has been lifted a while ago. I think someone had used 10 somewhere.

You should be able to find out wheter a variable could be registered by checking the return value of the Particle.variable() call. If it returns false it couldn’t be registered anymore (e.g. due to exceeding the limit of possible variables).

But the way you are using it won’t work.
You need to have a global variable to expose as Particle.variable() - things like String(SI1145.ReadVisible())) do not satisfy that demand.
You need a global double or a global String to receive the value in order to expose it to the cloud.

The air quality one is the one next to the barometer, left side of the photon.

@E404, publish is limited to 1 publish per second or a burst of 4 with a 4 second pause before the next burst. You can publish a string contain the text for all your variables in a single statement instead. For example:

Particle.publish("all_sensors", "Vis:"+String(SI1145.ReadVisible())+" IR:"+String(SI1145.ReadIR())+" UV:"+String((float)SI1145.ReadUV()/100));
2 Likes

You mean the one that is connected to the Grove port D7?
Then I’m not surprised you don’t get its data via A0.

No is connected to A0