C# · Electronics

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()




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();



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







private void Form1_Load(object sender, EventArgs e)


foreach (string s in SerialPort.GetPortNames())





private void button1_Click(object sender, EventArgs e)




private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


timer1.Interval = 1000;




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);






void step(boolean dir,int steps){




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

digitalWrite(stepperPin, HIGH);


digitalWrite(stepperPin, LOW);





void loop(){

dir=' ';

if (Serial.available()) {














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!.

7 thoughts on “The DIY Surveillance System Using a Webcam – Part 3

  1. 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…:)

  2. 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?😛 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

    1. Well, not sure about brain dead or not but I have found it hard to reply. The reasons for not posting anything more apart from time is obviously this. I need to offer a great deal of support to everything. Unfortunately I am not social media active that much. I did have a facebook page but Ive taken it off now. I have linked in at : http://www.linkedin.com/pub/samer-tufail/15/6aa/9b4
      twitter page at: https://twitter.com/samertfl .
      Thanks for your comments though. Much appreciated.

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