Sunday, October 10, 2010

Adding to my Room Sensor

I also bought an ambient light sensor that suppose to be more sensitive than a Light Dependent Resistors. I needed my small arduino for another experiment so I moved everything over to a arduino mega. i2c is in a different spot so that's why the Temp/Humidity sensor moved.
IMG_2341

Here is the arduino code
#include <Wire.h>
#include 
#define samples 5

float H[samples];
float T[samples];
int   L[samples];
byte current = 0;

LibHumidity humidity=LibHumidity(0);

boolean readSerial()
{
  if(Serial.available() && (Serial.read()==65)) {
    return true;
  }

  Serial.flush();
  return false;
}

void setup(){
  Serial.begin(9600);
  pinMode(58, OUTPUT);
  pinMode(59, OUTPUT);
  digitalWrite(58, HIGH);
  digitalWrite(59, LOW);
}
void loop(){
  H[current]=(float)humidity.GetHumidity();
  T[current]=(float)(humidity.GetTemperature() * 1.8 )+32;
  L[current] = analogRead(3);
  current++;
  if(current == samples){
    current = 0;
  }
  
  if(readSerial() == true){
    float h2 = 0;
    float t2 = 0;
    int l2 = 0;
    
    for(byte a=0;aSerial.print(h2/samples);
    Serial.print(',',BYTE);
    Serial.print(t2/samples);
    Serial.print(',',BYTE);
    Serial.println(l2/samples);
  }
  delay(1000);
}

And here is the Processing code that uploads the data to Pachube
import processing.serial.*;
import eeml.*;

DataOut dOut;
float lastUpdate;

Serial myPort;      // The serial port
int whichKey = -1;  // Variable to hold keystoke values
String inByte;// = ' ';    // Incoming serial data
String[] message;

float humidity;
float temp;
int light;

void setup() {
  size(400, 300);
  // create a font with the third font available to the system:
  PFont myFont = createFont(PFont.list()[2], 14);
  textFont(myFont);

  println(Serial.list());   // List all the available serial ports:

  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil(10);
    
  // set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key   
  dOut = new DataOut(this, "http://api.pachube.com/v2/feeds/10505.xml", "API KEY GOES HERE");   
  
  //  and add and tag a datastream    
  dOut.addData(0,"Humidity");
  dOut.addData(1,"Temperature");
  dOut.addData(2,"Light");
}

void draw() {
  background(0);
  text("Light: " + light, 10, 220);
  text("Temp: " + temp, 10, 190);
  text("Humidity: " + humidity, 10, 160);
  text("Last Received: " + inByte, 10, 130);
  text("Last Sent: " + lastUpdate, 10, 100);
 
  if ((millis() - lastUpdate) > 60000){
        myPort.write(65);
        delay(5000);
        println("ready to POST: ");
        dOut.update(0, humidity); // update the datastream 
        dOut.update(1, temp); // update the datastream 
        dOut.update(2, light); // update the datastream 
        int response = dOut.updatePachube(); // updatePachube() updates by an authenticated PUT HTTP request
        println(response); // should be 200 if successful; 401 if unauthorized; 404 if feed doesn't exist
        lastUpdate = millis();
  }   
}

void serialEvent(Serial myPort) {
  inByte = trim(myPort.readString());
  message = split(inByte,',');
  humidity = float(message[0]);//float(myPort.readStringUntil(','));
  temp = float(message[1]);//float(myPort.readString());
  light = int(message[2]);
}

void keyPressed() {
  // Send the keystroke out:
  myPort.write(65);
  whichKey = key;
}

No comments:

Post a Comment