Arduino controlled automated blinds with Web UI
This instructable is a detailed guide on how to build an automated controller to run a motor that can operate the opening and closing of blinds. The controller enables the blinds to open and close based on a schedule, room temperature and out door luminosity. The controller settings can be set up via a web UI which enables a user to open or close the blinds manually or place them on a schedule with certain parameters.
This project will guide you through creating simple circuits for the sensors, hacking the motor shield so it can be stacked with the arduino and ethernet shield and implementation of the code (note all code is provided and is fully commented). The project is laid out in the following manner. First the controller and system will be constructed. Then the logic behind the code will be explained. The last page will contain the code and link to download so it can be installed on the arduino.
So I hope you enjoy this instructable, gain from it and most importantly have fun.
Table of contents:
1.Requirements and parts
2.Wiring the sensors.
3.Wiring the arduino and shields.
4.Hacking the motor shield.
5.Code logic.
This project will guide you through creating simple circuits for the sensors, hacking the motor shield so it can be stacked with the arduino and ethernet shield and implementation of the code (note all code is provided and is fully commented). The project is laid out in the following manner. First the controller and system will be constructed. Then the logic behind the code will be explained. The last page will contain the code and link to download so it can be installed on the arduino.
So I hope you enjoy this instructable, gain from it and most importantly have fun.
Table of contents:
1.Requirements and parts
2.Wiring the sensors.
3.Wiring the arduino and shields.
4.Hacking the motor shield.
5.Code logic.
Step 1: Requirements and parts
This project is fairly simple to create and set up since the most difficult part, the code, is fully provided. The setting up the arduino and shields is fairly straight forward. Below is a list of parts you will need for this project. I have also listed the prices for these parts if you need do not have them. Most of these can be ordered from Adafruit, Sparkfun or the robotshop.
1. Arduino ($30)
2. Ethernet shield ($50)
3. Motor Shield ($20)
4. Analog temperature (TMP36) sensor ($2)
5. Photocell ($1)
6. 20 pin 0.1" Female Headers ($3)
7. Breadboarding Wire Bundle (6)
8. Breadboard friendly 2.1mm DC Jack ($1)
9. 12 V Stepper Motor (14)
10. 100 ohm resistor
11. MicroSD card (1gb card is good and can be found for as little as $2).
12. 2 Full breadboards ($14)
13. 12V AC adapter. (powers the motor only)
14. battery pack for the arduino
Remember, all these parts can be used for other projects, so don't worry too much about the cost because you'll be able to use them in many more projects, unless of course you are gonna use this as a permanent installment.
Equipment needed
1.Soldering iron
2.Soldering Fan
3. Small screwdriver.
1. Arduino ($30)
2. Ethernet shield ($50)
3. Motor Shield ($20)
4. Analog temperature (TMP36) sensor ($2)
5. Photocell ($1)
6. 20 pin 0.1" Female Headers ($3)
7. Breadboarding Wire Bundle (6)
8. Breadboard friendly 2.1mm DC Jack ($1)
9. 12 V Stepper Motor (14)
10. 100 ohm resistor
11. MicroSD card (1gb card is good and can be found for as little as $2).
12. 2 Full breadboards ($14)
13. 12V AC adapter. (powers the motor only)
14. battery pack for the arduino
Remember, all these parts can be used for other projects, so don't worry too much about the cost because you'll be able to use them in many more projects, unless of course you are gonna use this as a permanent installment.
Equipment needed
1.Soldering iron
2.Soldering Fan
3. Small screwdriver.
Stepper Motor and Motor Shield
Connect the 12 V Stepper motor to the motor shield in the M3 and M4 slots. Instructions on how to solder the motor shield can be found here: http://www.ladyada.net/make/mshield/make.html
DC Jack
If you have the breadboard friendly 2.1mm DC jack, plug it into the breadboard. Place it near the edge of the board., Later all of the ground ports will be placed in series with the ones from the DC Jack. In the image above, three wires were soldered to the DC jack to extend it length.
Temperature sensor
Place the analog temperature sensor in the breadboard as shown above. The first pin is power, the second is output and the third is ground. Wire the ground to the in series with the ground pins for the DC jack.
Photocell
Place the photocell on the breadboard and connect a resistor to it as shown above. The pins are in the same order as on the temperature sensor. Again connect the ground to the ports we are using for all the ground connections.
Hack
Now place the motor shield in the second breadboard with the motor facing away from the arduino (as in the image above; notice that the motor is not in the picture).
Now for a hardware hack. The arduino communicates to the ethernet shield and motor shield on several different pins but pin 12 is common to both and this causes conflict. To fix this, we are gonna do a software hack so that data the arduino usually sends through pin 12 goes through pin 9 instead. First wire output pin 9 on the ethernet shield to the input slot that matches with 12 on the motorshield.
Now go find motors.h on your computer (usually in the arduino file under mydocuments/arduino/libraries) and open it up. Find the line #define MOTORLATCH 12 and change that to a 9. Save this file.
And there we go.
Now for a hardware hack. The arduino communicates to the ethernet shield and motor shield on several different pins but pin 12 is common to both and this causes conflict. To fix this, we are gonna do a software hack so that data the arduino usually sends through pin 12 goes through pin 9 instead. First wire output pin 9 on the ethernet shield to the input slot that matches with 12 on the motorshield.
Now go find motors.h on your computer (usually in the arduino file under mydocuments/arduino/libraries) and open it up. Find the line #define MOTORLATCH 12 and change that to a 9. Save this file.
And there we go.
Wiring outputs to inputs
Now finish wiring the rest of the output pins on the ethernet shield to there corresponding pin on the motor shield (except for pin 9 and 12).
More wiring
Connect the out put pins for 3V, 5V Gnd, 0 and 1 in series to thier corresponding ports on the breadboard for pins on the motor shield. For example, in the image above, the orange wire goes from 3 V on the enternet shield to 3 V on the motor shield.
Giving the motor shield power
Connect a wire (teal in the image above) in series with the power output from the DC jack and wire this to the port on the breadboard that is in series with the VIN port on the motor shield.
Grounding
Now connect all the ground wires that are in series to the ground port on the ethernet shield. Observe the held blue wire in the image. Just as a mention, don't forget to place a micro SD card in your ethernet shield.
All Done
No finish by taking the power wire for the temperature sensor and connecting it to 3 V on the motor shield. Do the same for the photocell but connect to 5 V (remember that first wire is power). The second wire goes to the input on the motor shield. Place the temp sensor on 0 and photcell on 1.
AND YOU ARE DONE CONSTRUCTING THE DEVICE.
AND YOU ARE DONE CONSTRUCTING THE DEVICE.
Step 14: The code
A detailed explanation for the code will be coming soon. For now you can download the code below and run it. I have commented throughout to make modification easy.
Important:
1. you will need to manually enter the IP address of your device and its MAC address into the code before running. You can get the IP address after connecting the ethernet shield to a router and loading the DHCPaddress program found under exmaples in the arduino sketch program.
Go through the code and enter your IP address anywhere it says or you notice an IP. There is only 3.
DOWNLOAD HERE: http://www.2shared.com/file/guPvu-HK/WindowBlindsSever1_4.html
Important:
1. you will need to manually enter the IP address of your device and its MAC address into the code before running. You can get the IP address after connecting the ethernet shield to a router and loading the DHCPaddress program found under exmaples in the arduino sketch program.
Go through the code and enter your IP address anywhere it says or you notice an IP. There is only 3.
DOWNLOAD HERE: http://www.2shared.com/file/guPvu-HK/WindowBlindsSever1_4.html
Final output
Once the sketch is loaded, you will be able to access the web UI via the IP address. It should appear as the images above.
The program works the following way:
-Via the web UI you can maunually open and close the blinds.
-You can also set a automatic timer that will open and close the blinds based on the following conditions.
If temperature exceeds the temp entered by user, the blinds close, else open.
If brightness is below luminosity entered by user, blinds close, else open.
else it will open and close at the times specified.
The program works the following way:
-Via the web UI you can maunually open and close the blinds.
-You can also set a automatic timer that will open and close the blinds based on the following conditions.
If temperature exceeds the temp entered by user, the blinds close, else open.
If brightness is below luminosity entered by user, blinds close, else open.
else it will open and close at the times specified.
Step 16: Running your blinds
The motor shaft can be connected in various ways to blinds to operate them. Easiest way to implement this is to use IKEA rollar blinds and attack the motor to the main shaft of the blinds. The design I have shown you here is just to give an idea of the set up of how such a system would work. Modifications are needed on your part to make it fit into the set up you have.
Build_it_Bob
Binary sketch size: 32,184 bytes (of a 32,256 byte maximum)
avrdude: stk500_recv(): programmer is not responding
The sketch compiles just fine when I verify it, but it fails during upload part. When I tried with 1.0 it was complaining about the size of the sketch, so I'm wondering if it has anything to do with that as it is very close to the limit.
Anybody else experiencing the same?
14. battery pack for the arduino
LOL
/*
Automated Blinds Controller
Programmed by Sarbjeet Singh
Last revision: August 30, 2012
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Time.h>
#include <AFMotor.h>
#include <SD.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x0D, 0xE0 };
IPAddress ip(192,168,0,10); //<<< ENTER YOUR IP ADDRESS HERE!!!
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
byte mem0=0;
byte mem1=0; //variables to set transition state
byte TemPin=0; //Input Pin for Temperature sensor
byte photocellPin=1; //Input Pin for Photocell
byte stepperRevolution=200; //number of steps taking for stepper to make on revolution.
byte limitRev=5;//number of revolutions needed to open or close blind.
byte NumberOfRevsFor=0; //Counter for number of revolutions completed by stepper motor going forward.
byte NumberOfRevsBack=limitRev; //Counter for number of revolutions completed by stepper motor in reverse.
AF_Stepper motor(200,2); //200 step/rev motor connect to M3 and M4 on motor shield
File myFile;
String value;
byte SDreadAtStart=0;
byte openHour,closeHour,ctltemp, OvRCtl=0, timeHour, timeMin;
int Luminosity; //variable which sensor luminosity reading is compared against.
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT); //set up reading and writing to SD card
SD.begin(4);
motor.setSpeed(250); //set motor 200 rpm.
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//SDreader("BlindCtl/Settings/Settings.txt"); //read values in from Time file. Hour and minute values are saved temporarily saved to Luminosity and ctltemp
//setTime(timeHour,timeMin,0,10,7,2012); //set current time on arduino
delay(1000); //Give ethernet and sensors time to set up.
}
void loop()//main
{
byte SDread;
SDread=EthernetClients(); //checks to see if client is connected
if(SDreadAtStart==0||SDread==1)
{
SDreader("BlindCtl/Settings/Settings.txt"); //reads values from Settings.txt
setTime(timeHour,timeMin,0,10,7,2012);
SDreadAtStart=1;
}
//Serial.println(Luminosity);
motors(Luminosity,ctltemp,openHour,closeHour);
}
void motors(int luminosity, byte ctltemp, byte openHour, byte closeHour)//(byte Open, byte Close)
{
// Serial.println(F("motors is started"));
time_t t=now();//set current time to variable t
byte curTime=hour(t); //saves the current hour
Serial.println(curTime);
//String readFile, value;
analogRead(TemPin);
delay(10);
int temRead=analogRead(TemPin); //reads in from temperature sensor
float temVoltage=temRead*5.0;
temVoltage/=1024.0;
float temperature=(temVoltage-0.5)*100;
delay(10); //delay to let voltage settle before reading from photocell
analogRead(photocellPin);
delay(10);
int photocellReading=analogRead(photocellPin);//reads the photocell value
delay(10);
if(OvRCtl==1)//checks for variable that indicates if blinds should auto open and then sets requirements to low.
{
luminosity=200;
ctltemp=temperature+5;
openHour=0;
closeHour=24;
}
else if(OvRCtl==2)//checks variable to see if override for close is enables and then sets requirements high.
{
luminosity=999;
ctltemp=25;
openHour=0;
closeHour=24;
}
else
{
if(luminosity<=200||luminosity>=1000)//error checks to make sure user has not entered values outside of scope.
luminosity=600;
if(ctltemp<=5||ctltemp>=60)
ctltemp=25;
if(openHour<=(-1)||openHour>=25)
openHour=13;
if(closeHour<=(-1)||closeHour>=25)
closeHour=20;
}
if(mem0==0&&mem1==0)//State one where blinds are closed
{
// Serial.print("Blinds are closed \n");
if((photocellReading>=luminosity)&&(curTime>=openHour)&&(curTime<closeHour)&&(temperature<=ctltemp))//If all three of these conidtions are met, blinds will move into opening state.
{
mem0=1;
}
}
else if(mem0==1&&mem1==0)//Moving forward state
{
//Serial.print("Blinds are opening \n");
if(NumberOfRevsFor==limitRev)//checks to see if blinds are fully open and if so, switches to fully open state.
{
mem1=1;
mem0=1;
}
else if((photocellReading>=luminosity)&&(curTime>=openHour)&&(curTime<closeHour)&&(temperature<=ctltemp))//&&(NumberOfRevsFor!=TravelForward))//confirms that opening condition is still present and continues to move motor forward
{
motor.step(stepperRevolution, FORWARD, DOUBLE);
motor.release();
NumberOfRevsFor++;
NumberOfRevsBack--;
}
else if(temperature>ctltemp||curTime>=closeHour||photocellReading<luminosity)//switches state to move back in closing condition has become true
{
mem1=1;
mem0=0;
}
}
else if(mem0==1&&mem1==1)//Full Open position
{
// Serial.print("Blinds are open \n");
if(temperature>ctltemp||curTime>=closeHour||photocellReading<luminosity)//condition to move into closing state
{
mem1=1;
mem0=0;
}
}
else if(mem0==0&&mem1==1)//Blinds closing state
{
//Serial.print("Blinds are closing \n");
if(NumberOfRevsBack==limitRev)//switches to closed state if blinds are fully closed.
{
mem1=0;
mem0=0;
}
else if((temperature>ctltemp||curTime>=closeHour||photocellReading<luminosity))//&&NumberOfRevsBack!=TravelBack)//checks for closing conditions and then proceeds to close blinds.
{
motor.step(stepperRevolution, BACKWARD, DOUBLE);
motor.release();
NumberOfRevsBack++;
NumberOfRevsFor--;
}
else if(photocellReading>=luminosity&&curTime>=openHour&&curTime<closeHour&&temperature<=ctltemp)//if conditions for blinds opening become true, switches to blind open state
{
mem1=0;
mem0=1;
}
}
Serial.print(mem0);
Serial.print(mem1);
return;
}
byte EthernetClients()//web clients that takes what the user sets and saves them to teh SD card
{
boolean stopreading=0,variablePresent=0,storevalue=0,timeOn=0,clientC=0, equalsCount=0, SDread=0;
String readURL;
delay(10);
EthernetClient client = server.available();
//Serial.println("Eth");
if (client)
{
// an http request ends with a blank line
boolean currentLineIsBlank = true;
//Serial.println("Client available");
Serial.println(F("Client"));
while (client.connected()) {
if (client.available()) {
char c = client.read();
if(c=='?')
{
variablePresent=1;
}
if (c=='z'){//stops reading into the string once end of header is reached.
stopreading=1;}
else if(c!='z' && stopreading==0&&variablePresent==1)//reads in GET from the HTTP header
{
readURL+=c;
//Serial.println(readURL);
}
//delay(1000);
if(stopreading==1&&variablePresent==1&&storevalue==0)//reads stored GET header to extract variables
{
for(int i=0; i<=(readURL.length()-1); i++)
{
if(readURL[i]=='=')
{
int j=i+1;
while(readURL[j]!='&'&&readURL[j]!='x')
{
value+=readURL[j];
j++;
}
if(value=="Open")
OvRCtl=1;
if(value=="Close")
OvRCtl=2;
if(equalsCount==0&&value=="Time"){
timeOn=1;
OvRCtl=0;}
else if(value!="Open"&&value!="Close"&&equalsCount==0){
//timeOn=0;
SD.remove("BlindCtl/Settings/Settings.txt");
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("Luminx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
if(equalsCount==1)
{
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("Tempx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
if(equalsCount==2)
{
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("OpenHourx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
if(equalsCount==3)
{
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("CloseHourx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
if(equalsCount==4)
{
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("Hourx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
if(equalsCount==5)
{
myFile= SD.open("BlindCtl/Settings/Settings.txt",FILE_WRITE);
myFile.print(F("Minx"));
myFile.print(value);
myFile.print("z");
myFile.close();
}
equalsCount++;
value="";
}
}
storevalue=1;
}
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println();
//Any variables cannot contain the characters of z or x! Last variable values must end with xz.
client.println(F("<div align='center'"));
client.println(F("<h1>Windows Blinds Client V 1.0</h1>"));
client.println(F("</div>"));
// client.println("<div align='center'");
if(timeOn==0)
{
client.println(F("<form name='input' action='192.168.0.10' method='get'>"));
client.println(F("<input type='radio' name='action' value='Openxz' /> Open"));
client.println(F("<input type='radio' name='action' value='Closexz' /> Close"));
client.println(F("<input type='radio' name='action' value='Timexz' /> Time"));
client.println(F("</br>"));
client.println(F("<input type='submit' value='Submit'/>"));
client.println(F("</form>"));
}
else if(timeOn==1)
{
client.println(F("<form name='input' action='192.168.0.10' method='get'>"));
client.println(F("Luminosity:<input type='text' name='Hour' value='600'/>"));
client.println(F("Temp(close):<input type='text' name='Minutes' value='26'/>"));
client.println(F("</br>"));
client.println(F("Open Blinds At*(hour):<input type='text' name='openTime'/>"));
client.println(F("Close Blinds At(hour):<input type='text' name='closeTime'/>"));
client.println(F("</br>"));
client.println(F("Current Hour:<input type='text' name='Hour'/>"));
client.println(F("Current Min:<input type='text' name='Min'/>"));
client.println(F("<input type=hidden name='action' value='xz' />"));
client.println(F("<input type='submit' value='Submit'/>"));
client.println(F("</form>"));
client.println(F("</br>"));
}
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
// if (loopcounter<=readURL.length())
//loopcounter++;
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
SDread=1;
//Serial.print('Broswer is closed');
}
return SDread;
}
void SDreader(const char* fileName)//function to read the values from teh SD card into variables
{
byte equalsCount=0;
String readURL;
//Serial.println('SD');
myFile= SD.open(fileName);
while(myFile.available())//reads the entire file in a String
{
char c=myFile.read();
readURL+=c;
}
myFile.close();
//Serial.println(readURL);
for(int i=0; i<=(readURL.length()); i++)//extracts values from file that are between x and z
{
if(readURL[i]=='x')
{
int j=i+1;
i=j;
while(readURL[j]!='z')
{
value+=readURL[j];
j++;
}
//Serial.println(value);
if(equalsCount==0)
{
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
Luminosity=atoi(string_as_char);
//Serial.println(Luminosity);
}
if(equalsCount==1)
{
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
ctltemp=atoi(string_as_char);
//Serial.println(Luminosity);
}
if(equalsCount==2)
{
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
openHour=atoi(string_as_char);
//Serial.println(Luminosity);
}
if(equalsCount==3)
{
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
closeHour=atoi(string_as_char);
}
if(equalsCount==4)
{
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
timeHour=atoi(string_as_char);
}
if(equalsCount==5){
char string_as_char[value.length()+1];
value.toCharArray(string_as_char,value.length()+1);
timeMin=atoi(string_as_char);
}
equalsCount++;
value="";
}
}
//Serial.print(Luminosity);
return;
}