ARDUINO FAN
This one controls the speed of a fan depending on the current temperature. The current temperature is looked up from Yahoo (you can enter your zip code). Python then maps that to a fan speed (temperature <=50 degrees: no fan; temperature >= 90 degrees: maximum speed).
COMPONENTS USED
- Arduino, breadboard
- DC motor
- Battery pack
- Resistors and transistors
PYTHON CODE
weather.py:
import xml.dom.minidom
import urllib2
def get_temperature(zipcode):
c=urllib2.urlopen("http://weather.yahooapis.com/forecastrss?p=" + str(zipcode) + "&u=f")
content=c.read()
doc = xml.dom.minidom.parseString(content)
el = doc.getElementsByTagName("yweather:condition")[0]
att = el.getAttribute("temp")
return int(att)
fan.py:
import sys
import time
import serial
import weather
ser = serial.Serial('/dev/tty.usbserial-A4001nKF', 9600)
def arduino_write(msg, ser):
ser.write(msg)
time.sleep(0.1)
def set_location(zipcode):
temperature = weather.get_temperature(zipcode)
# print temperature
def get_speed(temperature):
# print "temp: " + str(temperature)
minTemp = 50
maxTemp = 90
minSpeed = 1
maxSpeed = 255
speed = (temperature - minTemp) * maxSpeed / (maxTemp - minTemp)
speed = max(minSpeed, speed)
speed = min(maxSpeed, speed)
return speed
def set_speed(zipcode):
print "Setting fan speed according to temperature in " + str(zipcode)
temperature = weather.get_temperature(zipcode)
speed = get_speed(temperature)
print "Temperature: " + str(temperature) + ", fan speed: " + str(speed)
ser.write(str(speed))
def off():
ser.write(str(1))
ARDUINO CODE
int potPin = 0; // select the input pin for the potentiometer
int motorPin = 9; // select the pin for the Motor
int speed = 0;
int val = 0; // variable to store the value coming from the sensor
char serInString[100];
void setup() {
Serial.begin(9600);
}
void loop() {
//read the serial port and create a string out of what you rea
readSerialString(serInString);
speed = atoi(serInString);
// val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
Serial.println(speed);
if(speed > 0) {
analogWrite(motorPin, speed); // analogWrite can be between 0-255
}
delay(50);
clearSerialString();
}
//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
// clearSerialString();
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
void clearSerialString() {
for (int i=0; i < 10; i++) {
serInString[i] = ' ';
}
}
Comments
Comments from GSIs
Nice job connecting your Arduino to real-world data... It's almost like a Yahoo-weather driven air conditioner! As people start working on their final projects, they may find it useful to be able to read RSS data as well. If you're willing to help them out (or even to write up a couple of bits of example code), it would be really helpful!