The DIY Surveillance System Using a Webcam – Part 3

Im going to assume you have read the Part-1 and Part-2 of this post. Part-1 provides the hardware and Part-2 with the software for streaming and controlling the camera. This part will focus on the php file which is generated by the PHP webpage and its usage by the microcontroller to move or pan the camera. Lets get started.

Firstly we need a host program to read the data generated by the PHP and poll our “request” folder created in part-2 for updated data. Then we need to transmit this information serially to the microcontroller which pans based on that information. Im using C# to create this host program so here it goes. Start Visual Studio create a windows application. Throw in a button, a combo box and timer control. The stop button would stop the timer which will be initiated by the combo box changed event.

The code looks like:


namespace RotomaticFront

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Loader(object sender, EventArgs e)

{

string[] aFilenames;

string fileName;

byte[] buf = null;

aFilenames = Directory.GetFiles("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\Rotomatic\\request\\");

if (aFilenames.Length > 0)

{

fileName = Path.GetFileNameWithoutExtension(aFilenames[0]);

StreamReader SR;

string S;

SR = File.OpenText("C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\Rotomatic\\request\\" + fileName);

S = SR.ReadLine();

SR.Close();

File.Delete(aFilenames[0]);

SerialPort port = new SerialPort(comboBox1.SelectedItem.ToString(), 9600, Parity.None, 8, StopBits.One);

port.Open();

port.Write(dirr[1]);

//port.Write(angl[1]);

port.Close();

}

}

private void Form1_Load(object sender, EventArgs e)

{

foreach (string s in SerialPort.GetPortNames())

{

comboBox1.Items.Add(s);

}

}

private void button1_Click(object sender, EventArgs e)

{

timer1.Stop();

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{

timer1.Interval = 1000;

timer1.Start();

}

}

Lets start with the form_load() event. I populate the combo box with the com ports which are available on the system. Since the arduino board is connected to one of the com ports and I have a lot of usb/com conversions going on I have a lot of ports which keep changing, you might not have this problem so you can skip it, it’s entirely up to you.

Once the combo box index changes a timer is initiated with a 1 second interval and the event that is triggered on each tick is the Loader() event I will explain its working in a bit. The stop button simply stops the timer. The Loader event gets the file names in the request directory first, if the length of the files is >0 means a file is there, the file name is then acquired without the extension. The file is opened and the first line is read ( since we only wrote only one line we don’t need to do a while loop but you might need to if you add other options). The file is then closed and deleted. A serial port connection is opened at 9600 baud and the data read is transmitted serially to the arduino board/microcontroller. I keep saying microcontroller and not the arduino board because the end device to control the motor could be a PIC or any other microcontroller it doesn’t necessarily have to be the arduino board.

Thats it run the program make sure everything is working throw in a couple of message boxes while reading the data to make sure you are actually reading the right data.

Now lets move onto the code for the micro controller. For those who don’t know the code is written in arduino programming language based on C/C++. Here is the code:


int dirPin  = 4;

int stepperPin= 3;

int sleepPin=7;

int ledPin=8;

char dir;

void setup () {

pinMode (dirPin, OUTPUT);

pinMode (stepperPin, OUTPUT);

pinMode(sleepPin,OUTPUT);

pinMode(ledPin,OUTPUT);

Serial.begin(9600);

Serial.flush();

}

void step(boolean dir,int steps){

digitalWrite(sleepPin,HIGH);

digitalWrite(dirPin,dir);

delay(50);

for(int i=0;i<steps;i++){

digitalWrite(stepperPin, HIGH);

delayMicroseconds(100);

digitalWrite(stepperPin, LOW);

delayMicroseconds(100);

}

digitalWrite(sleepPin,LOW);

}

void loop(){

dir=' ';

if (Serial.available()) {

dir=Serial.read();

if(dir=='1'){

step(true,15);

digitalWrite(ledPin,HIGH);

//delay(500);

}

if(dir=='0'){

step(false,15);

digitalWrite(ledPin,LOW);

//delay(500);

}

}

}

The basic function for stepping is the same and was mentioned in part-1. The only new thing is the LED on PIN 8 for determining the direction of pan. The setup() function consists of a Serial.begin(9600) which initiates the serial communication followed by the baud rate settings. The Loop() function which is the arduino equivalent of C main() with the only difference as the name suggests, it keeps looping forever. Anyway Serial.availible() checks if there is any serial data to be read on the serial port. If there is it is read by serial.read() and stored in the dir variable. If the direction is one the clockwise rotation is turned on and 15 steps are taken in the clockwise direction – step(true,15)  . The opposite is true if a 0 is read from the serial port. LED turns high for the clockwise rotation and off for the anti-clockwise rotation. Its a good indicator as it helps to see data coming through is correctly being read. Load the program to the arduino board and your done.

Run the PHP webpage along with the streaming server ( “simpleSubscriber”) . Try panning and it should work and there you have it a surveillance system from a webcam :]. I would’ve  provided you with a URL to mine but its installed at my work place!!!! So I cant let me install one at home and then ill post a link. Ill also try to get a video of it going anyway, please post any questions you have in the comments section followed by the part you are having problems with or cant understand. Till then happy coding!.

About these ads

~ by garagedeveloper on December 5, 2009.

7 Responses to “The DIY Surveillance System Using a Webcam – Part 3”

  1. [...] sent us his custom surveillance system, part 1, part 2, and part 3 after needing a way to find out why some cables at work were becoming unplugged (spoiler, the [...]

  2. so did u write any The DIY Surveillance System Using a Webcam which is in programming C#? Because i need to create a surveillance for my final year project according to that title in C# programming… :)

  3. oh ya, is it possible change to use laptop fix in webcam??

  4. I hardly write responses, but I looked at some of the
    responses on The DIY Surveillance System Using a Webcam – Part 3 Garage developers Blog.
    I do have a couple of questions for you if it’s okay. Is it just me or does it look like a few of the remarks appear like they are written by brain dead individuals? :-P And, if you are posting at other social sites, I’d
    like to keep up with everything new you have to post.
    Would you list of the complete urls of your social networking sites like your linkedin profile, Facebook page or twitter
    feed?

  5. What’s up to every one, it’s actually a good for me to go to see this web site, it includes valuable Information.

  6. Thank you for sharing your info. I truly appreciate your efforts and I
    am waiting for your further write ups thanks once again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: