You are on page 1of 7

#include

#include
#include
#include
#include
#include

<SD.h>
<Adafruit_VC0706.h>
<Servo.h>
<SPI.h>
<Ethernet.h>
<SoftwareSerial.h>

SoftwareSerial cameraconnection = SoftwareSerial(69, 3);


Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

String readString;
int distance;
int i;
int button = 2;
int press = 0;
int greenLed = 6;
int redLed = 7;
const int pos_reset = 0;
boolean stare = true;
#define trigPin 13
#define echoPin 12
#define potPin 13
/************ SDCARD STUFF ************/
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
File myfile;
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
Servo myservo; // create servo object to control a servo
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
IPAddress ip(169,254,114,126);
//byte ip[] = { 10, 5, 2, 181 }; // fixed IP addr in LAN
//byte gateway[] = { 10, 5, 0, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 252, 0 }; //subnet mask
EthernetServer server(80); //server port

//////////////////////
void setup(){
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT);
// set the SS pin as an output (nec
essary!)
digitalWrite(10, HIGH);
// but turn off the W5100 chip!
/* if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
if (!root.openRoot(&volume)) error("openRoot failed");*/
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// see if the card is present and can be initialized:

//pinMode(6, OUTPUT); //pin selected to control LED


//start Ethernet
Ethernet.begin(mac, ip);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println(" Sistem de control "); // so I can keep track
pinMode(button,INPUT);
myservo.attach(9);
digitalWrite(button, HIGH);
myservo.write(pos_reset);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(greenLed,OUTPUT);
pinMode(redLed,OUTPUT);
myservo.write(0);
digitalWrite(redLed, HIGH);
}
// How big our line buffer should be. 100 is plenty!
#define BUFSIZ 100
void loop()
{
char clientline[BUFSIZ];
char *filename;
int index = 0;

int image = 0;
// Create a client connection
EthernetClient client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100)
{
//store characters to string
readString += c;
//Serial.print(c);
}
// If it isn't a new line, add the character to the buffer
if (c != '\n' && c != '\r')
{
clientline[index] = c;
index++;
// are we too big for the buffer? start tossing out data
if (index >= BUFSIZ)
index = BUFSIZ -1;
// continue to read more data!
continue;
}
// got a \n or \r new line, which means the string is done
clientline[index] = 0;
filename = 0;
if (strstr(clientline, "GET /") != 0)
{
// this time no space after the /, so a sub-file
if (!filename) filename = clientline + 5; // look after the "GET /" (5
chars)
// a little trick, look for the " HTTP/1.1" string and
// turn the first character of the substring into a 0 to clear it out.
(strstr(clientline, " HTTP"))[0] = 0;
myfile = SD.open(filename, FILE_READ);
int16_t c;
while ((c = myfile.read()) >= 0)
{
// uncomment the serial to debug (slow!)
//Serial.print((char)c);
client.print((char)c);
}
myfile.close();
}
//if HTTP request has ended
if (c == '\n')

{
///////////////
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Dezvoltarea unui sistem de control al trafic
ului pentru cladiri inteligente</TITLE>");
client.println("</HEAD>");
client.println("<BODY bgcolor='lavender'>");
client.println("<H2>DEZVOLTAREA UNUI SISTEM DE CONTROL AL TRAFICULU
I PENTRU CLADIRI INTELIGENTE</H2>");
client.println("<hr />");
client.println("<br />");
client.println("<a href=\"/?barrierup\"\">DESCHIDE BARIERA</a>");
client.println("<a href=\"/?barrierdown\"\">INCHIDE BARIERA</a><br
/>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("?barrierup") >0)//checks for on
{
Serial.println("Barrier Up");
BarrierUP();
}
else if(readString.indexOf("?barrierdown") >0)
{
//digitalWrite(6, LOW); // set pin 6 low
Serial.println("Barrier Down");
BarrierError();
}
readString=""; //clearing string for next read
}
}
}
}
press = digitalRead(button);
digitalWrite(redLed, HIGH);
if(press == LOW)
{
Serial.println("Barrier Up");
BarrierUP();
}
digitalWrite(trigPin,LOW);
delayMicroseconds(10);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);

distance = pulseIn(echoPin,HIGH) / 29 >> 1;


if(distance > 5 && distance < 20 && distance != 0)
{
TakePicture();
}
delay(map(analogRead(potPin),0,10000,200,2000));
Serial.print(distance);
Serial.print(" cm");
Serial.println();
}
void BarrierError()
{
delay(5000);
digitalWrite(redLed, HIGH);
avertizare(redLed, LOW);
delay(100);
myservo.write(pos_reset);
}
void avertizare(int led, int state)
{
for(int i = 0; i < 11; i++)
{
state =! state;
digitalWrite(led, state);
delay(500);
}
}
void BarrierUP()
{
delay(5000);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
myservo.write(90);
delay(5000);
avertizare(redLed, LOW);
delay(100);
digitalWrite(greenLed, LOW);
myservo.write(0);
}
void TakePicture()
{
myservo.detach();

#define chipSelect 10
#if !defined(SOFTWARE_SPI)
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if(chipSelect != 53) pinMode(53, OUTPUT); // SS on Mega

#else
if(chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno, etc.
#endif
#endif

// Try to locate the camera


if (cam.begin()) {
Serial.println("Camera Found:");
} else {
Serial.println("No camera found?");
return;
}
/* // Print out the camera version information (optional)
char *reply = cam.getVersion();
if (reply == 0) {
Serial.print("Failed to get version");
} else {
Serial.println("-----------------");
Serial.print(reply);
Serial.println("-----------------");
}*/
// Set the picture size - you can choose one of 640x480, 320x240 or 160x120
// Remember that bigger pictures take longer to transmit!
cam.setImageSize(VC0706_640x480);
//cam.setImageSize(VC0706_320x240);
//cam.setImageSize(VC0706_160x120);

// biggest
// medium
// small

// You can read the size back from the camera (optional, but maybe useful?)
uint8_t imgsize = cam.getImageSize();
/* Serial.print("Image size: ");
if (imgsize == VC0706_640x480) Serial.println("640x480");
if (imgsize == VC0706_320x240) Serial.println("320x240");
if (imgsize == VC0706_160x120) Serial.println("160x120");*/
Serial.println("Snap in 3 secs...");
delay(3000);
if (! cam.takePicture())
Serial.println("Failed to snap!");
else
Serial.println("Picture taken!");
if (SD.exists("IMAGE00.JPG")){
SD.remove("IMAGE00.JPG");
}
// Create an image with the name IMAGExx.JPG
char filename[13];
strcpy(filename, "IMAGE00.JPG");
/*
for (int i = 0; i < 100; i++) {
filename[5] = '0' + i/10;
filename[6] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;

}
}*/
// Open the file for writing
File imgFile = SD.open(filename, FILE_WRITE);
// Get the size of the image (frame) taken
uint16_t jpglen = cam.frameLength();
Serial.print("Storing ");
Serial.print(jpglen, DEC);
Serial.print(" byte image.");
int32_t time = millis();
// Read all the data up to # bytes!
byte wCount = 0; // For counting # of writes
while (jpglen > 0) {
// read 32 bytes at a time;
uint8_t *buffer;
uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but
may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appea
r locked up
Serial.print('.');
wCount = 0;
}
//Serial.print("Read "); Serial.print(bytesToRead, DEC); Serial.println(" b
ytes");
jpglen -= bytesToRead;
}
imgFile.close();
time = millis() - time;
Serial.println("done!");
Serial.print(time); Serial.println(" ms elapsed");
myservo.attach(9);
myservo.write(pos_reset);
}

You might also like