Ethan Pizarro
Published © GPL3+

Lane Tech HS - PCL - Air Quality Check

Checks the air quality of your local city to learn when it is safe to go outside.

BeginnerFull instructions provided1 hour59
Lane Tech HS - PCL - Air Quality Check

Things used in this project

Hardware components

Argon
Particle Argon
×1
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×4
Resistor 220 ohm
Resistor 220 ohm
×4

Software apps and online services

Air Quality API

Story

Read more

Schematics

Air Quality Checker Schematic

Code

Air Quality Checker Code

C/C++
Checks for the air quality index in Chicago and writes an analog signal to 4 LEDs accordingly.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

// setup for API return and servo
StaticJsonDocument<3072> doc;
Servo myServo;

// designate LED analog pins and servo pin
const int led1 = A0;
const int led2 = A1;
const int led3 = A2;
const int led4 = A3;
const int servoPin = D8;

// variable for LED analog and servo write values
int writeValue = 0;
int servoWriteValue = 0;

void setup() {
    // set analog pins to output
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    // start serial monitor
    Serial.begin(9600);
    // attach servo to D8 pin
    myServo.attach(servoPin);
    // set servo to first angle possible
    myServo.write(0);
    // 5 second startup delay
    delay(5000);
}

void loop() {
    // make API call
    Particle.publish("airQualityCheck");
    // subscribe to API call event
    Particle.subscribe("hook-response/airQualityCheck", aqiHandler);
    // wait 5 seconds, then write values to respective components
    delay(5000);
    analogWrite(led1, writeValue);
    analogWrite(led2, writeValue);
    analogWrite(led3, writeValue);
    analogWrite(led4, writeValue);
    myServo.write(servoWriteValue);
}

void aqiHandler(const char *event, const char *data) {
    // grab API return and print onto serial monitor
    String aqi = data;
    Serial.print("The Fine Particulate matter air quality index is at ");
    Serial.println(aqi);
    
    // make API return into integer
    int value = atoi(aqi);
    // map AQI value to analogWrite and servo write ranges
    writeValue = map(value, 0, 300, 0, 255);
    servoWriteValue = map(value, 0, 300, 0, 180);
    // print mapped values to serial monitor
    Serial.print("Analog write value: ");
    Serial.println(writeValue);
    Serial.print("Servo write value: ");
    Serial.println(servoWriteValue);
}

Credits

Ethan Pizarro

Ethan Pizarro

2 projects • 1 follower

Comments

Add projectSign up / Login