Dheerendra Singh Tomar
Published © CC BY-ND

Realtime Air Quality Monitor

This is first edition project made using LinkIt One. The final outcome will be a portable device showing air quality on web.

IntermediateShowcase (no instructions)2 hours2,115
Realtime Air Quality Monitor

Things used in this project

Story

Read more

Code

Air-Quality Device

C/C++
#include <SPI.h>
#include "PubNub.h"

//import all the necessary files for GPRS connectivity
#include "LGPRS.h"
#include "LGPRSClient.h"

#include <LGPS.h>
#include "GPSWaypoint.h"
#include "GPS_functions.h"

#define ledPin 13
/*
JST Pin 1 (Black Wire)  =&gt; //Arduino GND
 JST Pin 3 (Red wire)    =&gt; //Arduino 5VDC
 JST Pin 4 (Yellow wire) =&gt; //Arduino Digital Pin 8
 */
 
 int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 2000;//sampe 30s&nbsp;;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;

GPSWaypoint* gpsPosition;


//define the required keys for using PubNub
char pubkey[] = "pub-c-9d4ba088-6b35-4afb-b1ab-fb72728b51c3";
char subkey[] = "sub-c-fa4c8f02-638c-11e5-85c0-0619f8945a4f";
char channel[] = "bike-map";

void setup()
{
    pinMode(ledPin, OUTPUT);

    pinMode(8,INPUT);
    Serial.begin(9600);
    Serial.println("Serial setup");

    //Connect to the GRPS network in order to send/receive PubNub messages
    Serial.println("Attach to GPRS network with correct APN settings from your mobile network provider");
    //example here is with mobile provider EE in the UK
    //attachGPRS(const char *apn, const char *username, const char *password);
  
     //while (!LGPRS.attachGPRS("mobile.o2.co.uk", "o2web", "password"))
     while (!LGPRS.attachGPRS("bsnlnet", "", "")) 
    {
    Serial.println(" . ");
    delay(1000);
    }

    Serial.println("LGPRS setup");

    PubNub.begin(pubkey, subkey);
    Serial.println("PubNub setup");

    LGPS.powerOn();
}

 
void flash(bool success)
{
     
    /* Flash LED three times. */
    for (int i = 0; i < 3; i++) {
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
    }
}


void loop()
{
    ratio = lowpulseoccupancy/(sampletime_ms*10.0);  // Integer percentage 0=&gt;100
    concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
    Serial.print("concentration = ");
    Serial.print(concentration);
    Serial.println(" pcs/0.01cf");
    Serial.println("\n");
    lowpulseoccupancy = 0;
    
    Serial.println("Getting GPS Data");
    //Aquire GPS position
    char GPS_formatted[] = "GPS fixed";
    gpsPosition = new GPSWaypoint();
    gpsSentenceInfoStruct gpsDataStruct;
    getGPSData(gpsDataStruct, GPS_formatted, gpsPosition);
    Serial.println(" GPS Data aquired");

    
    char* buffer_latitude = new char[30];
    sprintf(buffer_latitude, "%2.6f", gpsPosition->latitude);

    char* buffer_longitude = new char[30];
    sprintf(buffer_longitude, "%2.6f", gpsPosition->longitude);
    
   // float concentration_example = 1.2323;
    
    String upload_GPS = "[{\"latlng\":[" + String(buffer_latitude) + "," + String(buffer_longitude)+ "], \"data\":\""+concentration+"\"}]";
    
    const char* upload_char = upload_GPS.c_str();
     
    //Once Position is Aquired, upload it to PubNub
    LGPRSClient *client;

    
    Serial.println("publishing a message");
    client = PubNub.publish(channel, upload_char, 60);
    if (!client) {
        Serial.println("publishing error");
        delay(1000);
        return;
    }
    while (client->connected()) {
        while (client->connected() && !client->available()); // wait
        char c = client->read();
        Serial.print(c);
    }
    client->stop();
    Serial.println();
    flash(true);


    delay(5000);
}

Air-Quality Device

This code enables us to interface the dust sensor and take readings from it and push it to PubNub cloud.

Credits

Dheerendra Singh Tomar

Dheerendra Singh Tomar

1 project • 3 followers
Hobbyist maker!!

Comments