Things used in this project

Hardware components:
Photon new
Particle Photon
×1
10167 01
DHT22 Temperature Sensor
×1
SainSmart MQ135
×1
Double-side Prototype PCB Universal Printed Circuit Board
×1
09264 1
RGB Diffused Common Cathode
×1
11026 02
Jumper wires (generic)
×1
Adafruit industries ada592 image 75px
USB-A to Micro-USB Cable
×1
Mfr 25frf52 10k sml
Resistor 10k ohm
×1
Mfr 25frf52 100r sml
Resistor 100 ohm
×3
Hand tools and fabrication machines:
Lasercutter
Laser cutter (generic)

Custom parts and enclosures

Box Enclosure
SVG File containing the plans for our enclosure.
co2_box_W5lNLDkMJA.svg

Schematics

Photon with MQ135, DHT22, and RGB LED
This was the breadboard layout we used to connect our Photon to the DHT22 sensor, the MQ135 sensor, and an RGB LED.
project3breadboard_5XFnt29qPH.fzz

Code

Particle Photon CO2 Sensor to Phant DatabaseArduino
This code collects input from a DHT22 and MQ135 sensor, and uploads it to a phant database.

Be sure to include the SparkFunPhant, MQ135, and Adafruit_DHT libraries when you implement this yourself.
// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>
// This #include statement was automatically added by the Particle IDE.
#include <MQ135.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

#define DHTPIN 3    // the DHT pin
#define DHTTYPE DHT22		// DHT 22 (AM2302)
#define MQ135PIN A0  //the CO2 Sensor pin

MQ135 MQ135(MQ135PIN);

DHT dht(DHTPIN, DHTTYPE);

//RGB pin spots
int redPin = D1;
int greenPin = D2;
int bluePin = D0;

//Declare variables
double hum = 0;
double temp = 0;
double co2 = 0;

//Phant setup
const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "Insert public key here"; // Phant public key - HAS TO BE CHANGED
const char privateKey[] = "Insert private key here"; // Phant private key  - HAS TO BE CHANGED
Phant phant(server, publicKey, privateKey);

void setup() {
    Serial.begin(9600);
    pinMode(DHTPIN, INPUT);
    Particle.variable("hum", hum);
    Particle.variable("temp", temp);
    Particle.variable("co2", co2);
    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);  
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

void loop() {
    
    double tempTemp = dht.getTempFarenheit();
    if(tempTemp > 0 && tempTemp < 100){
        temp = tempTemp;
    }
    double tempHum = dht.getHumidity();
    if(tempHum > 0 && tempHum < 101){
        hum = tempHum;
    }
    
    double tempco2 = MQ135.getCorrectedPPM(temp, hum);
    if(tempco2 > 0){
        co2 = tempco2*100;
    }
    
    if(co2 < 500){
        setColor(0, 200, 0);
    }else{
        setColor(0, 0, 200);
    }
    
    Serial.println("temp: ");
    Serial.println(temp);
    Serial.println("hum: ");
    Serial.println(hum);
    Serial.println("co2: ");
    Serial.println(co2);
    
    postToPhant();
    delay(60000);
    
}

int postToPhant()
{    
    // Use phant.add(<field>, <value>) to add data to each field.
    // Phant requires you to update each and every field before posting,
    // make sure all fields defined in the stream are added here.
    phant.add("humidity", hum);
    phant.add("temp", temp);
    phant.add("co2", co2);
        	
    TCPClient client;
    char response[512];
    int i = 0;
    int retVal = 0;
    
    if (client.connect(server, 80)) // Connect to the server
    {
		// Post message to indicate connect success
        Serial.println("Posting!"); 
		
		// phant.post() will return a string formatted as an HTTP POST.
		// It'll include all of the field/data values we added before.
		// Use client.print() to send that string to the server.
        client.print(phant.post());
        delay(1000);
		// Now we'll do some simple checking to see what (if any) response
		// the server gives us.
        while (client.available())
        {
            char c = client.read();
            Serial.print(c);	// Print the response for debugging help.
            if (i < 512)
                response[i++] = c; // Add character to response string
        }
		// Search the response string for "200 OK", if that's found the post
		// succeeded.
        if (strstr(response, "200 OK"))
        {
            Serial.println("Post success!");
            retVal = 1;
        }
        else if (strstr(response, "400 Bad Request"))
        {	// "400 Bad Request" means the Phant POST was formatted incorrectly.
			// This most commonly ocurrs because a field is either missing,
			// duplicated, or misspelled.
            Serial.println("Bad request");
            retVal = -1;
        }
        else
        {
			// Otherwise we got a response we weren't looking for.
            retVal = -2;
        }
    }
    else
    {	// If the connection failed, print a message:
        Serial.println("connection failed");
        retVal = -3;
    }
    client.stop();	// Close the connection to server.
    return retVal;	// Return error (or success) code.
}

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Blynk GPS Tracker
Intermediate
  • 41
  • 2

Full instructions

Simple GPS tracker using a Particle Photon (or Electron) and an EM406 GPS module with location visible on Blynk app map.

Pool Buddy
Intermediate
  • 1,619
  • 11

Work in progress

Monitor and log water quality (pH & ORP) and temperature from everywhere.

IoT Thermometer Using Python
Intermediate
  • 986
  • 8

Full instructions

How to develop a simple but powerful IoT thermometer using a Zerynth-powered single-board MCU and the Zerynth App.

Particle Photon Flip Dot Clock
Intermediate
  • 2,162
  • 31

Full instructions

The flip dot display uses an electromechanical dot matrix that spin from black to yellow to display text, numbers and more!

Patriot iOS App
Intermediate
  • 547
  • 9

Use an old iPhone device to control and display the status of your Patriot IoT devices.

Patriot iOS App

Team Patriot

What To Wear LCD
Intermediate
  • 48
  • 1

Ever wondered what to wear due to unpredictable weather??? Wonder no longer!

Add projectSign up / Login