Ethernet based Security System

        The need for Security systems have rapidly grown from being specialized for high-risk areas (like banks, companies, governmental institutions), to be available and demanded rapidly by the average public. In the last ten years, small businesses have rapidly grown and expanded and the need of reliable, cheap and effective security system is becoming a must, not only for the business owners, but also at homes. One of the offered way of security systems offer the SMS notification service, nice but not that reliable. SMS center could have a congestion at the time the thief was sleeping in your company (or home) borders.         Well another method of creating a security system and which would be very effective is to use the Internet or Ethernet Notification system. It is reliable, when using TCP, and effective where most internet services are always on demand and it is very rare, and very unlikely to have a sudden failure.

        TCP based security system uses the Ethernet module offered within the contest, an ATmega16L microcontroller, a PIR as a sensor and few other peripheral devices, which is specially targeted for homes and small business owners.

        here we have used through for my final design a combination of the WIZ810MJ module, an ATmega16L microcontroller, a 7.3728 MHz crystal (compatible with my bootloader programmer), a DS1302 timekeeper, a PIR to sense the human movement through the body heat, a keypad for initial configuration and a 2×16 LCD for display. The design also include regulators to power up the circuits.

                                                                          



How it works :

        Well, the system should be installed near an internet source and a cable (crossover or straight-through as it depends) should be plugged in to the module’s Rj-45 jack.
         If the system is in rest mode (acting as TCP server). It waits for commands from the client (its IP address can be programmed or change initially or during operation) and execute them (after a password is entered obviously) while still inspecting for any Alarm which would arises from the PIR sensor. When any Alarm triggers, it switches into client mode (acts as a TCP client) and starts sending warning signs.
         The system uses motion sensing (based on human body heat) to trigger an alarm which sends a message to the server containing a timestamp presenting the exact time the Motion was detected acting as a real-time surveillance system. For increasing safety from the risk arising from a false alarm, it sends the alarm signals for about 5 minutes and then it stops for 10 minutes and so on assuming the alarm was still active ... Clearing the way for the costumer to stop the alarm if he noticed some kind of a failure as the system acts as a server in these 10 minutes. This false alarm is very rare and it could occur as a result of maybe a passing squirrel maybe or some relative passed by and forgot to turn the alarm off.. etc.. So although it is rare but should be taken care of.
        Once it is connected and powered on, (it takes power from 3 sources, 3.3 v ,5 v,and 12 v for the PIR) the system enters into the configuration mode.


Configuration mode :

         In the first start-up of the system, it enters the configuration mode. It is just a feature that I added for the design to be more flexible. The System asks you first (through the LCD interface) to enter a 4 digit password, I didn’t do a masking feature because I sense there would be less error when you can really see the password written on the LCD, and there isn’t off course any viruses or key loggers are there?!
        The system will also ask you about the current date and time which is necessary for the validity of the timestamp associated with the Alarms. Otherwise, if the time was miss-entered, the customer may lose track of when the alarm was really triggered.
        After entering the current date the system asks the customer to chose between accepting the default IP setting or reconfigure them depending on his own needs. The customer will be asked to enter the System IP Address, the subnet mask, the default gateway address, the MAC address desired, and the destination or Server IP address in which will give commands remotely to the security system and will also receive Alarm signals when the motion is sensed. and here ends the configuration , I didn’t really see any more necessary feature to add to the configuration, but this doesn’t block the way for future improvement as minor adjustments could be made and the user could be given more flexibility in the future versions.

Server Mode : what happens when the alarm isn’t on :

        Well, in this mode, I added some features which I guess were essential and primary for this application. When I first thought about the design, I said hey,, lets control all the damn house, why not right? especially that the wiznet chip can tolerate such feature. But as time ran over I decided to stick to the main idea of security system. why would anyone control his home while he isn’t there ?!! He would just need to protect it ,,, and thus I optimized the control for security purposes only.
        OK, what happens when the customer connects to the system. Off course everyone would expect there will be a password so not anyone who would connect to the system would mess up your house. So you must enter a password before you can send commands. The command set is limited actually for the purpose I mentioned above to be dedicated only for security. Therefore, you can turn the lights on and off. This feature looks at the first time as control but it is an effective way to say to the burglar stop, I am in the house and fully alert ... The burglar wouldn’t expect that you have my product.
         Anyway, you can also change the destination IP address of the server in which it is eligible to connect to the system as you could change your location and as a result you may have to change the server address remotely. Off course, you have to logout after you finish configuration to be safe and secure that no one but you could log in to your system.
         One nice feature I added is the ability to turn Lights on and off to convince any potential thief that the house is already occupied, so he would forget the whole operation from the start, and while it is a control feature, it is a very good security feature. PS: the led’s o/p could be connected to any relay’s input and it will work splendidly.

Client Mode : When a movement is sensed :

    When the system detects any motion through the PIR, It directly switches into the client mode. It reads the current time and sends the alert message containing the timestamp. As I mentioned before it keeps doing so for 5 minutes, stopes for 10 minutes and resume the operation if the alarm was still triggered and so on Through these 10 minutes, it goes back to server mode just in case the customer know that it was a flow and wanted to turn it off.



Block Diagramm:

        I was really interested since I went to college to study communication systems ( I am now in the junior year in college in the CEE department, Communications and Electronic Engineering), to be able to monitor my home in my absence. This is a big issue here since most people in my country own summer homes in the mountains which remains unprotected and empty all winter. There were I first decided to build a compact remote security system designed for ordinary people to protect their valuables. I did chose the Internet as the abstract though, since through my search on the net it seemed very complicated and expensive. Therefore when I heard about the contest from my colleague I was both thrilled and surprised. Thrilled to find my call and surprised to hear about such a product. When the wiznet module first came three weeks ago I was so enthusiastic I began developing the module adapter right away. (The PCB was already published on the net , I just did the fabrication)

        As I run over the datasheet I was really relieved that the module supports SPI as I usually chose components in embedded design to be compatible with SPI and I worked a lot with it on the past. It was really easy to control W5100 sing SPI and after I initialized The PORTS and the SPI registers, the rest was easy using the following function:

unsigned char spi_transfer(volatile unsigned char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))); // Wait for the end of the transmission
return SPDR; // return the received byte
}

It was a little disappointment though the lack of support for the SPI Interrupt handler. But It worked smoothly nevertheless.

After we have forgotten all about SPI using this function the next thing to do is to write functions which would be able to read and write to the module’s registers, and as you can see below, this wasn’t a hard job to do either.

unsigned char wiz_rd(unsigned int address)
{
unsigned char data = 0x00;
PORTB &= ~(1<<PIN_SLAVE_SELECT) ; //enable slave
spi_transfer(WIZNET_OPCODE_READ);
spi_transfer((address>>8) & 0x00FF);
spi_transfer(address& 0x00FF);
data = spi_transfer(DUMMY_DATA);
PORTB |= (1<<PIN_SLAVE_SELECT) ; // Disable slave
return data;
}
void wiz_wr(unsigned int address, unsigned int targetValue)
{
PORTB &= ~(1<<PIN_SLAVE_SELECT) ; //enable slave
spi_transfer(WIZNET_OPCODE_WRITE);
spi_transfer((address>>8) & 0x00FF);
spi_transfer(address & 0x00FF);
spi_transfer(targetValue);
PORTB |= (1<<PIN_SLAVE_SELECT) ; // Disable slave
}

        And after this was done, my first task was to make the module accept the ping to its IP address. And unfortunately it was a challenge, why? ,, lets see. 
        As I first run the application, I was so surprised it didn'’t work, I used the terminal to figure out where the problem was and my functions were 100% correct, I could write and read from the device register but still the ping command didn'’t work.
That’s when I began to worry about being a problem with the module itself. Especially that the module wasn'’t sending any signals for being alive. After days of manipulating the code and checking connections which led to re-examine the module’s adapter. Finally, I realized what the problem was, I noticed that the power-on LED faints every time I insert the module which led me to a theory that the current wasn'’t enough for the module and I was right. By replacing the old adapter with a powerful 1200 mA adapter and that’s it. The module finally started to give response, both leds of the Rj45 jack were illuminating, and it successfully gave reply to my ping request. So I was relieved and went through with experimenting the W5100.

Why TCP :

        Actually I first started my project using UDP packets, and all operations went smoothly. Then again, I noticed that I needed a more reliable method for the security system as UDP is very unreliable, for the data loss is always possible, and you cant really figure out if you are really connected to the server or not. So TCP was undoubtedly a more secure solution for the system to work reliably as it requires.

        The Transmission Control Protocol (TCP) is one of the core protocols of the Internet protocol suite. TCP provides reliable, in-order delivery of a stream of bytes making it very suitable for secure data communications.

                    

        This is the TCP packet simplified, scary isn’t it. But the great thing about W5100 is that it relief you from all the messy work of having to worry about sequence number and checksums, etc .. And DO BELIEVE me when I tell you this because I suffered alot dealing with nokia’s F-bus protocol which has annoying checksum and sequence number fields. Anyways back to our subject, the W5100 Ethernet chips let you worry about handling only the data, and it stores the destination IP address and PORT address in special registers. 

TCP MODES :

Server Mode :

        TCP can act as a client or a server. Acting as a server it listens continuously for connection requests until a request is received. It off course keep monitoring in the process the PIR for any motion detection. In this stage, the peer IP address isn’t of great importance, since when you send back information to the client his IP address will be initially captured by the W5100 and stored in the proper registers. Although I thought of limiting the connection to the module as a server to only one IP address, but it seems the password solves the security problem as the customer may use a shared computer at work from one side, and for the other side, it makes space for more flexibility to send commands from everywhere and from any computer through the net. Therefore as you see I only locked the IP address in which the ALARM is sent to.

Here is how it is done :

server_mode:
mode=2;
Listen_TCP(Sock0); // Listen for requests on socket0
while (wiz_rd(Sn_SR(Sock0)) == SOCK_LISTEN)
{
if(Alarm) //if Alarm is detected -> connect to server
{
lcd_clear();
lcd_putsf("Alarm Detected");
goto tcp; //switch to client mode
}
}; //wait for request

Client Mode :

        When acting as a client it directly connects to the previously saved destination IP address and send an embedded message containing the alarm and the time of occurance. The rest of the functionality is as explained earlier is all about alarming for 5 minutes, suspension for ten minutes to allow disabling the alarm in case of a fault , and so on .

client_mode:

mode=1;
wr_dst_addr_TCP(Sock0,config_dst_ip_address,config_dst_port_address);
Connect_TCP(Sock0);

The PIR :

        Well the PIR was easy to interface, it has 3 pins : 1 connected to V+ , One to Ground, and one to the microcontroller. whenever body heat based movement is detected, the PIR triggers high and the microcontroller is notified and the info is transmitted. This sensor could be replaced with any similar sensor but the primary application for this project was protection against burglary, so this PIR fits perfectly.

The LCD, Keypad, and the Timekeeper :

        Well, the LCD and the timekeeper had already developed functions embedded inside the Code Vision AVR, so I used it directly to save time developing the projects and it fitted perfectly .

        As for the keypad I had developed a software for the mission a year before, so this was a good opportunity to really use it.
Unfortunately I had a little error back then, the values of the first row are shifted right ! Looks like a keypad hardware error, BUT I over passed it by a little code manipulation as you see below so it worked with me perfectly.

Free Software Used:

        As for receiving packets and controlling at the other end you can use any free software available, I use Hercules, because it is easy to use and supports both TCP client and server. So this software works perfectly. You can Use also Ethereal or any other similar free software.

                            


Project Overview Block Diagram:
Complete Schematic Diagram:                                                                                                                                                                                                                                                                         
Parts List:

1) ATmega16L microcontroller
2) 7.3728 MHz crystal oscillator
3) PIR
4) 2 33pF capacitors
5) A timekeeper
6) 1 65536 Hz oscillator for the timekeeper
7) 1 LCD
8) 1 Keypad
9) WIZ810MJ module