Ben Clark
Published © CC BY-NC-SA

Air Quality Index Monitoring with Photon Internet Button

See the Air Quality Index (AQI) at a glance to determine if it is safe for you or your loved ones' environmental to be out.

IntermediateShowcase (no instructions)1 hour1,180
Air Quality Index Monitoring with Photon Internet Button

Things used in this project

Hardware components

Internet Button
Particle Internet Button
×1

Software apps and online services

AirNowAPI
Used to poll the current Air Quality Index from a nearby monitoring station.

Story

Read more

Code

AQI Display on Internet button.

C/C++
The code could be cleaner but it works.
// This #include statement was automatically added by the Particle IDE.
#include "InternetButton/InternetButton.h"
InternetButton b = InternetButton();
int AirQual []= {0,0,0};
int AirValCount = 0;

void setup() {
b.begin();
  // Subscribe to the webhook response event
  Particle.subscribe("hook-response/AQI", myHandler, MY_DEVICES);
  RGB.control(true);
}

// Handle the webhook response
void myHandler(const char *event, const char *data) {
  
  RGB.color(10,00,00); show data is being parsed. 
  static int i = 0;
    char dataString[4 * 4 + 3 + 1];
    strncpy(dataString, data, sizeof(dataString));
    i = 0;
    //Split string into tokens with ~ 
    char * AQI = strtok(dataString, "~"); 
    while (AQI != NULL) {
        
        // string to integer conversion operator. atoi
        AirQual[i] = atoi(AQI);
          AQI = strtok(NULL, "~");
        i++; //add one to i then do it again? 
        AirValCount=i;
    }
    
}


void loop() {

  static unsigned long nextTrigger = 10 * 1000; //wait 10 seconds to run 1st run. 

    if (nextTrigger < millis()) {
        // polling Webhook every 10 minutes.
        nextTrigger = millis() + 10*60*1000;
        String data = String(10);
        // Trigger the webhook
        Particle.publish("AQI", data, PRIVATE); // Get that data !
    }
    int i =0;
    
    for ( i = 0; i < AirValCount; i++ ){
        b.allLedsOff();
        if (i == 0){
            RGB.color(00,00,10); //o3 
        }
        if (i == 1){
            RGB.color(10,10,00); //PM 2.5
        }
        if (i == 2){
            RGB.color(10,00,10); //PM 10
        }
            //Set LED Ring accordingly
            //... with a shit load of if statements. 
            //What the values are:
            //0-50  Good  Green 0,228,0  
            //51-100  Moderate Yellow 255,255,0
            //101-150  Unhealthy Sensitive Groups Orange 255,126,0
            //151-200  Unhealthy Red 255,0,0
            //201-300  Very Unhealthy Purple 153,0,76
            //301-500  Hazardous Maroon 126,0,35
        if (AirQual[i] > 0) { 
            b.ledOn(1, 0, 16,00);
        }

        if (AirQual[i] > 25) {
            b.ledOn(2, 0, 16,00);
        }

        if (AirQual[i] > 51) { 
            b.ledOn(3, 16,16,0);
        }

        if (AirQual[i] > 75) {
            b.ledOn(4, 16,16,0);
        }

        if (AirQual[i] > 101) {
            b.ledOn(5, 23,8,0);
        }

        if (AirQual[i] > 125) {
            b.ledOn(6, 23,8,0);
        }

        if (AirQual[i] > 151) {
            b.ledOn(7, 16,0,0);
        }

        if (AirQual[i] > 175) {
            b.ledOn(8, 16,0,0);
        }
 
        if (AirQual[i] > 201) {   
            b.ledOn(9, 19,0,8);
        }
 
        if (AirQual[i] > 225) {
            b.ledOn(10, 19,0,8);
        }

        if (AirQual[i] > 301) {
            b.ledOn(11, 16,0,2);
        }    
        //pause for 5 seconds be fore moving to the next value.
        delay(5000);
        
    }
}

        

Credits

Ben Clark

Ben Clark

1 project • 1 follower

Comments