Things used in this project

Hardware components:
Photon new
Particle Photon
×1
Adafruit MiCS5524 CO / Alcohol / VOC Gas Sensor Breakout
×1
10167 01
DHT22 Temperature Sensor
×1
Led technology l03r5000q1 image
5 mm LED: Yellow
×1
Kingbright wp7113srd d image
5 mm LED: Red
×1
Kingbright wp7113sgd
5 mm LED: Green
×1
Mfr 25frf52 10k sml
Resistor 10k ohm
×1
Custom PCB - Bakelite Board
×1
Insulated Wire
×1
Acrylic Sheet - 12x24
×1
Software apps and online services:
Particle Development Environment
Hand tools and fabrication machines:
Lasercutter
Laser cutter (generic)
Wire Strippers
Wire Cutters
Tin Snippers

Code

Particle CodeC#
This code includes sections for posting to the Phant database, tweeting out current data, and checking the sensors for temperature/humidity/air quality.
// This #include statement was automatically added by the Particle IDE.
#include <SparkTime.h>

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <SparkFunPhant.h>
UDP UDPClient;
TCPClient client;
SparkTime rtc;
// OAuth Key
#define TOKEN "847504857967632386-5zye176erv8iBZn1qcQNKTlrBOKf44G"

// Twitter Proxy
#define LIB_DOMAIN "arduino-tweet.appspot.com"
unsigned long currentTime;
unsigned long lastTime = 0UL;
String tweetStr;
int counter = 1;
char msg[128]; 
int delay1 = 0;
DHT dht(4,DHT22);
const char server[] = "data.sparkfun.com"; // Phant destination server
const char publicKey[] = "aGVRnDp0arTqW1nO4Mlp"; // Phant public key - HAS TO BE CHANGED
const char privateKey[] = "KE576jXAZBC1WR47xolM"; // Phant private key  - HAS TO BE CHANGED
Phant phant(server, publicKey, privateKey); // Create a Phant object

const int POST_RATE = 3000; // Time between posts, in ms.
unsigned long lastPost = 0; // global variable to keep track of last post time
double temp = 0;
double hum = 0;
int air = 0;
void setup() {
 rtc.begin(&UDPClient, "time.nist.gov");
  rtc.setTimeZone(-6); // gmt offset
pinMode(A0,INPUT);
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
Serial.begin(9600);
dht.begin();
}
void light(){
    
    checkHum();
    checkTemp();
    checkAir();
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    if (air > 150){
        digitalWrite(1,HIGH);
    }
    else if (air > 120){
        digitalWrite(2,HIGH);
    }
    else {
        digitalWrite(0,HIGH);
    }
    delay(60000);
}

void loop() {
    delay(4000);
    light();
    sendTweet();
  //  postToPhant();
    int i = 0;
    while (i < 60){
        light();
        i++;
        if (i % 4 == 0){
            postToPhant();
        }
    }
    
  
 



}
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("temp", temp);
    phant.add("humidity", hum);
    phant.add("air_quality", air);
        	
    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.
        }

void checkHum()
{
    hum = dht.getHumidity();
}

void checkTemp()
{
    temp = dht.getTempFarenheit();
}
void checkAir()
{
    air = analogRead(A0);
}
void sendTweet()
{
    currentTime = rtc.now();
  //  tweetStr += input;
   // if (rtc.minute(currentTime) != rtc.minute(lastTime)) {
        tweetStr += "Good morning!";
      if((rtc.AMPMString(currentTime) == "PM") && (rtc.hour(currentTime) >= 17)){
          tweetStr = "Good Evening!";
      }
      if((rtc.AMPMString(currentTime) == "PM") && (rtc.hour(currentTime) <17)){
          tweetStr = "Good Afternoon!";
      }
      
   // checkHum();         // check humidity. Sets the local variables curr_hum and curr_hum str
    //checkTemp(); 
    tweetStr += " Humidity = ";
    tweetStr += String(hum);
    tweetStr += "% Temperature = ";
    tweetStr += String(temp);
    tweetStr += " Air Quality = ";
    tweetStr += String(air);
      
    tweetStr += " ";  
      //tweetStr = "";
	tweetStr += rtc.hour12String(currentTime);
	tweetStr += ":";
	tweetStr += rtc.minuteString(currentTime);
	tweetStr += ":";
	tweetStr += rtc.secondString(currentTime);	
	tweetStr += " ";	
	tweetStr += rtc.AMPMString(currentTime);
      tweetStr.toCharArray(msg,128);
      // connect to twitter here and POST update
      delay(1000);

      client.connect(LIB_DOMAIN, 80);
      client.println("POST /update HTTP/1.0");
      client.println("Host: " LIB_DOMAIN);
      client.print("Content-Length: ");
      client.println(strlen(msg)+strlen(TOKEN)+14);
      client.println();
      client.print("token=");
      client.print(TOKEN);
      client.print("&status=");
      client.println(msg);

    counter++;
    tweetStr = "";
      lastTime = currentTime;
      
    
    
}

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

Lightweight Sliding Door Automator
Intermediate
  • 1,535
  • 7

Work in progress

Automate the opening and closing of a lightweight sliding door (e.g. a screen door), including remote controls on your phone.

Christmas Gift Box
Intermediate
  • 3,633
  • 595

Full instructions

Christmas Gift Box plays music and sends an email when it is opened.

Carbon Fiber Vacuum Chamber
Intermediate
  • 2,905
  • 94

Full instructions

Our project is a carbon fiber vacuum chamber that is monitored by multiple particle photons and various sensors.

ConnectTheDots with Particle Azure IoT Hub Integration
Intermediate
  • 841
  • 8

Protip

This project will allow you to connect your Particle device into an Azure IoT Hub for viewing data in real-time through an ASP.NET web app!

What should I wear outside?
Intermediate
  • 5,044
  • 67

Full instructions

A whimsical weather clock powered by Particle and forecast.io

Garage Door Opener with Blynk
Intermediate
  • 367
  • 2

Open, close and monitor a garage door opener with a smart phone using a Particle Photon and the Blynk application.

Add projectSign up / Login