sunnuntai 3. kesäkuuta 2012

Final Serial processing stuff

Ok, so the delay in the Arduino sketch is now 10s as opposed to 0.5 seconds
(last line in sketch is now:   delay(10000); // 10 seconds delay between readings)

The python script creates a file name based on the date + hour and records 1024 values from then on.

final script:


#!/usr/bin/python

import datetime
import serial
import time

# important!!!! set FILEPATH , FILE extension andserial port
FILEPATH = 'C:/Users/Sumita/Desktop/Work/iaq/'
FILEXT = '.txt'
PORT = 'COM3'

# read timeout in seconds
# post is COM3 for windows and  /dev/ttyUSB0 for linux

ser = serial.Serial(port=PORT,baudrate=9600,timeout=20)

# loop forever, well atleast till the Arduino is connected
while True:
    maxread = 1024
    filetime = datetime.datetime.now()
    filename =  FILEPATH + str(filetime.month) + '_' + str(filetime.day) + '_'  + str(filetime.hour) + FILEXT
    file = open(filename, 'a')
    while maxread > 0:
    byte = ser.readline()
    if byte != None:
    byte = int(byte)
    else:
    byte= 0
   
    now = time.time()
    json_data = '{"time":"%s", ' % now
    json_data += '"value":%s}\n' % byte
    print json_data
    file.writelines(json_data)
        maxread -= 1
    file.close()


NOTE: the delay (ins ms) in the arduino sketch should be shorter than the timeout (in s) in the python script!!!
there seems to be an almost 30second delay in writing to the file.. not sure why but doesn't seem a cause for concern

to get this to work: you need Python 2.6 or higher (not 3.0), and pySerial: http://pypi.python.org/pypi/pyserial





Reading the Serial Data


a short python script to read serial value with a timestamp and convert to JSON format:

#!/usr/bin/python

import datetime
import serial
import json

# read timeout in seconds
# post is COM3 for windows and  /dev/ttyUSB0 for linux

ser = serial.Serial(port='COM3',baudrate=9600,timeout=60)

# loop forever, well atleast till the Arduino is connected
while True:
    byte = ser.readline()
    now = datetime.datetime.now()
    json_data = '"datetime":("%s"),' % now
    json_data += '"value":("%s")' % byte
    print json_data

Output:

"datetime":("2012-06-03 22:47:00.149000"),"value":("52")
"datetime":("2012-06-03 22:47:00.613000"),"value":("51")
"datetime":("2012-06-03 22:47:01.070000"),"value":("51")
"datetime":("2012-06-03 22:47:01.558000"),"value":("51")
"datetime":("2012-06-03 22:47:02.019000"),"value":("51")
"datetime":("2012-06-03 22:47:02.494000"),"value":("51")
"datetime":("2012-06-03 22:47:02.984000"),"value":("51")

Now to save it on a file for the Webapp!

Our final code

Looks like this will be our final code: (not much different than v0.1)


// Servo Motor
#include <Servo.h>
Servo myservo;
int pos =0;

// VOC thresholds
const int lowthreshold = 300;
const int highthreshold = 350;

// LED pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
int gasSensor = A0; // select input pin for gasSensor
int val = 0; // variable to store the value coming from the sensor


int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int DISPLAY_TIME = 200;

void setup()
{
Serial.begin(9600);
myservo.attach(6);
}

void loop()
{
  val = analogRead(gasSensor); // read the value from the pot
  Serial.println( val );
 
  // green LED is on, when the device is on
    if (val < lowthreshold)
    {
     greenIntensity =255;
     blueIntensity = 0;
     redIntensity =0;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    }
 
  if (val > lowthreshold && val < highthreshold)
    {
     greenIntensity =0;
     redIntensity =0;
     blueIntensity = 255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
   
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(15);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(15);
     }
    }
   
     
  if (val > highthreshold)
    {
     greenIntensity =0;
     blueIntensity = 0;
     redIntensity =255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
   
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(5);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(5);
     }
    }
  delay(500);
}

lauantai 5. toukokuuta 2012

Our Final Circuit

















In our meeting this week (Friday 4th May, 16-18) we got a lot of things done, like finalize our circuit and design.

Now we need a web app, but first, we need to get the data from the serial prompt to a file or an MySql db...

perjantai 4. toukokuuta 2012

The code v.0.1

Hello,

We got the first implementation of the project to work.  The sensor is connected to a rgb-led and a servo-motor. Here´s the code:

// Servo Motor
#include <Servo.h>
Servo myservo;
int pos =0;

// VOC thresholds
const int lowthreshold = 300;
const int highthreshold = 350;

// LED pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
int gasSensor = A0; // select input pin for gasSensor
int val = 0; // variable to store the value coming from the sensor


int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;

const int DISPLAY_TIME = 200;

void setup()
{
Serial.begin(9600);
myservo.attach(6);
}

void loop()
{
  val = analogRead(gasSensor); // read the value from the pot
  Serial.println( val );

  // green pin is on when the device is on
  greenIntensity = 255;
  redIntensity =0;
  blueIntensity = 0;
  analogWrite(GREEN_LED_PIN,greenIntensity);
 
  if (val > lowthreshold && val < highthreshold)
    {
     greenIntensity =0;
     redIntensity =0;
     blueIntensity = 255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(15);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(15);
     }
    }
   
     
  if (val > highthreshold)
    {
     greenIntensity =0;
     blueIntensity = 0;
     redIntensity =255;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    
     // get the motor running
     for (pos=0; pos < 180; pos +=1)
     {
       myservo.write(pos);
       delay(5);
     }
     for (pos=180; pos >= 180; pos -=1)
     {
       myservo.write(pos);
       delay(5);
     }
    }
   
    if (val < lowthreshold)
    {
     greenIntensity =255;
     blueIntensity = 0;
     redIntensity =0;
     analogWrite(GREEN_LED_PIN,greenIntensity);
     analogWrite(BLUE_LED_PIN,blueIntensity);
     analogWrite(RED_LED_PIN,redIntensity);
    }

  delay(500);
/*  for (greenIntensity =0;
       greenIntensity <= 255;
       greenIntensity +=5)
       {
       redIntensity = 255-greenIntensity;
       analogWrite(GREEN_LED_PIN,greenIntensity);
       analogWrite(RED_LED_PIN,redIntensity);
       delay(DISPLAY_TIME);
       }
   for (blueIntensity =0;
       blueIntensity <= 255;
       blueIntensity +=5)
       {
       greenIntensity = 255-blueIntensity;
       analogWrite(BLUE_LED_PIN,blueIntensity);
       analogWrite(GREEN_LED_PIN,greenIntensity);
       delay(DISPLAY_TIME);
       } */
      
}

We have also found a cardboard box for the future testing purposes.

torstai 26. huhtikuuta 2012

Yay Yay! Our VOC rocks!

In our group meeting today, which went on for like 2.5 hours, we finally finished our circuit and got VOC readings on the Arduino Serial Prompt!

Next steps: Add a servo motor and an RGB LED to the circuit and code in the logic to change the LED color and power the motor depending on some VOC thresholds.

Pics from today:

maanantai 23. huhtikuuta 2012

Blinking LEDs and more

Today we worked with our Ardunio and
- made a yellow LED blink!
- made a Servo motor do the disco!


Pictures below: