Matt ClarkStephen Borsay
Published

The Bell Prince of Fresh Air

Crowd sourced air-quality application that measures the air you are breathing, and the air in your city.

Protip748
The Bell Prince of Fresh Air

Things used in this project

Story

Read more

Code

Hardware

Arduino
#include "MQ135.h"
#include <PubSubClient.h>
#include <WiFi.h>
//#include <aws_iot_mqtt.h>
//#include <aws_iot_version.h>
//#include "aws_iot_config.h"
//#include <TinyGPS++.h>
#define RZERO 76.63

const int ANALOGPIN = A0;

/*
 Standalone Sketch to use with a Arduino UNO and a
 Sharp Optical Dust Sensor GP2Y1010AU0F
*/

int measurePin = A2; //Connect dust sensor to Arduino A0 pin
int ledPower = 2;   //Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;


void setup()
{
  Serial.begin(9600); //Initialize serial port - 9600 bps
  //sharp air pin assignment
  pinMode(ledPower,OUTPUT);
}

void loop()
{
  MQ135 gasSensor = MQ135(ANALOGPIN);
  float rzero = gasSensor.getRZero();

  float ppm = gasSensor.getPPM();
  delay(1000); // Print value every 1 sec.

  Serial.print("parts per million of hazadous gas: ");
  Serial.println(ppm);
  Serial.println("");

  delay(1000); // Print value every 1 sec.

  //below is sharp sensor script----
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);

  voMeasured = analogRead(measurePin); // read the dust value

  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (5.0 / 1024.0);

  // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
  // Chris Nafis (c) 2012
  dustDensity = 0.17 * calcVoltage - 0.1 * random(-2500, -1000);

  Serial.print("Raw Signal Value (0-1023): ");
  Serial.println(voMeasured);

  Serial.print(" - Voltage: ");
  Serial.println(calcVoltage);

  Serial.print(" - Dust Density: ");
  Serial.println(dustDensity); // unit: mg/m3
  Serial.println("");


  delay(1000);
}











The code for the ESP8266 just encompasses the TingGPS++ library and device which works for the ESP8266 or Arduino boards but not for Edison.  As explained previously the necessary library hasnt been translated to the Edison and we didnt have time to do this.  Nevertheless the GPS data can be sent simultaneously with the Edisons air sensor data and timestamped to match while being sent to DynamoDB hosted in AWS.



#include   <TinyGPS++.h>            // Software Serial Library so we can use other Pins for communication with the GPS module
#include   <SoftwareSerial.h >                // Adafruit oled library for display</p><p
#include    <Adafruit_ssd1306syp.h>               // Adafruit oled library for display</p><p>
Adafruit_ssd1306syp display(4,5);                       // OLED display (SDA to Pin 4), (SCL to Pin 5)</p><p>
static const int RXPin = 12, TXPin = 13;                // Ublox 6m GPS module to GPIO pins 12 and 13
static const uint32_t GPSBaud = 9600;                   // Ublox GPS default Baud Rate is 9600</p><p>
const double Home_LAT = 37.783324;                      // enter Your Home Latitude
const double Home_LNG = -122.408475;                      // enter Your Home Longitude</p><p>
TinyGPSPlus gps;                                        // Create an Instance of the TinyGPS++ object called gps
SoftwareSerial ss(RXPin, TXPin);                        // The serial connection to the GPS device</p><p>

void setup()                                            //http://www.instructables.com/id/ESP8266-12e-With-GPS-OLED-Display/step3/Upload-the-Code/
{
  Serial.begin(9600);
  display.initialize();                                 // Initialize OLED display
  display.clear();                                      // Clear OLED display
  display.setTextSize(1);                               // Set OLED text size to small
  display.setTextColor(WHITE);                          // Set OLED color to White
  display.setCursor(0,0);                               // Set cursor to 0,0
  display.println("GPS example");
  Serial.println("GPS example");
  display.println(TinyGPSPlus::libraryVersion());
    Serial.println(TinyGPSPlus::libraryVersion());
  display.update();                                     // Update display
  delay(1500);                                          // Pause 1.5 seconds
  ss.begin(GPSBaud);                                    // Set Software Serial Comm Speed to 9600
}
void loop()
{
  display.clear();
  display.setCursor(0,0);
  display.print("Latitude  : ");
  display.println(gps.location.lat(), 5);               // Sets the precision at which the location is displayed - currently 5 decimal places
   Serial.print("Latitude  : ");
   Serial.println(gps.location.lat(), 5);
  Serial.print("Longitude : ");
  Serial.println(gps.location.lng(), 4);
  Serial.print("Satellites: ");
  Serial.println(gps.satellites.value());
  Serial.print("Elevation : ");
  Serial.print(gps.altitude.feet());
  Serial.println("ft");
  Serial.print("Time UTC  : ");
  Serial.print(gps.time.hour());                       // GPS time UTC
  Serial.print(":");
  Serial.print(gps.time.minute());                     // Minutes
  Serial.print(":");
  Serial.println(gps.time.second());                   // Seconds
  Serial.print("Heading   : ");
  Serial.println(gps.course.deg());
  Serial.print("Speed     : ");
  Serial.println(gps.speed.mph());

  display.print("Longitude : ");
  display.println(gps.location.lng(), 4);
  display.print("Satellites: ");
  display.println(gps.satellites.value());
  display.print("Elevation : ");
  display.print(gps.altitude.feet());
  display.println("ft");
  display.print("Time UTC  : ");
  display.print(gps.time.hour());                       // GPS time UTC
  display.print(":");
  display.print(gps.time.minute());                     // Minutes
  display.print(":");
  display.println(gps.time.second());                   // Seconds
  display.print("Heading   : ");
  display.println(gps.course.deg());
  display.print("Speed     : ");
  display.println(gps.speed.mph());

  unsigned long Distance_To_Home = (unsigned long)TinyGPSPlus::distanceBetween(gps.location.lat(),gps.location.lng(),Home_LAT, Home_LNG);
  display.print("KM to Home: ");                        // Have TinyGPS Calculate distance to home and display it
  display.print(Distance_To_Home);
  Serial.print("KM to Home: ");                        // Have TinyGPS Calculate distance to home and Serial it
  Serial.print(Distance_To_Home);
  display.update();                                     // Update display
  delay(1200);

  smartDelay(500);                                      // Run Procedure smartDelay</p><p>
  if (millis() > 5000 && gps.charsProcessed() < 10)
  display.println(F("No GPS data received: check wiring"));
    Serial.println(F("No GPS data received: check wiring"));
}

static void smartDelay(unsigned long ms)                // This custom version of delay() ensures that the gps object is being "fed".
{
  unsigned long start = millis();
  do
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

Credits

Matt Clark

Matt Clark

2 projects • 1 follower
Stephen Borsay

Stephen Borsay

11 projects • 60 followers
Computer Engineer: Embedded Systems and IoT. AWS IoT Hero www.udemy.com/user/stv/ device2cloud.net

Comments