You are on page 1of 79

&

Arduino

, 2012

.
,
/ .

Arduino


.

.
.
.


, ,
,
..
, ,
, , ,
.

Arduino
. ,
Arduino (Microcontroller),
( ) , Ethernet
Shield Arduino LAN.

, Arduino, , ,
Php, MySql, C, PIR, Hall

ABSTRACT

Design and construction of an embedded alarm system based on Arduinos


architecture which is supported by site management
At the present day, sensor networks are used widely in alarm systems,
surveillance systems, vehicular traffic control systems, data collection, detection of
climate changes and natural disasters and in similar areas. It is therefore a system that
mainly aims to ensure a constant surveillance of the environment and to maintain a safe
condition for interior areas especially for houses, warehouses, schools, workplaces etc.
In this thesis, the sensor system has been designed as an alarm system which is
based on Arduino architecture and is commanded by a remote computer through
internet. In particular, it is a system including an Arduino Ethernet Shield which enables
the system to work on a local area network (LAN) and which is consisted of an Arduino
microcontroller and the relevant sensors required for the owners needs.

KEY WORDS

Embedded systems, Arduino, Sensor networks, Web programming, Php, MySql, C


language, Hall effect sensor, PIR sensor


.
. ,
.
,
.

.................................................................................................................... 3
...................................................................................................................... 4
............................................................................................................ 5
.................................................................................................................. 8
.............................................................................................................. 9
............................................................................................... 11
K ............................................................................................... 13
1 - .......................................................................................... 14
1.1.

........................................................................... 15

1.2.

....................................................... 15

1.3.

................................................................................... 16

2 - ............................................................... 17
2.1.

.................................................................................... 18

2.2.

.............................................................................. 19

2.3.

........................................................................................... 19

2.4.

........................................................................... 19

2.5.

....................................................................... 20

2.6.

................................................................ 21

3 - .......................................... 24
3.1.

............................................................... 25

3.2.

........................................................................... 26

3.3.

Arduino ............................................................................................................. 26

3.3.1.

Arduino ........................................................................................ 27

3.3.2.

Arduino Uno ..................................................................... 28

3.3.3.

Arduino Shields / Ethernet Shield .............................................................. 34

3.3.4.

................................................ 36

3.4.

....................................................................................................... 39

3.4.1.

Hall (Hall Effect) .................................................. 39

3.4.2.

PIR ........................................................................... 41

3.5.

............................................................................ 44

4 - .................................. 46
4.1.

................................................................. 47

4.1.1.

.............................................................................. 48

4.1.3.

CSS ........................................................................................................... 60

4.1.4.

........................................................................................ 61

4.1.5.

Arduino ............................................................................. 65

4.2.

..................................................................................... 68

4.2.1.

.................................................................................. 69

4.2.2.

.......................................................... 72

4.3.

...................................................................................... 73

4.3.1.

.......................................................................... 73

4.3.2.

. .......................................................................... 75

5 - & ............................. 76
............................................................................................................. 79

10

3.1 . 25
3.2 Arduino Uno.

28

3.3 /

29

3.4 ..

31

3.5 32
3.6 Arduino IDE

33

3.7 Shields...............................................................................

34

3.8 Ethernet Shield..........................................................................................

36

3.9

37

3.10 Hall 39
3.11 PIR

41

3.12 PIR. 42
3.13 ..

44

3.14 Breadboard ... 45


4.1 .

47

4.2 /.

48

4.3 /

49

4.4 Log Out..

50

4.5

51

4.6 ..

52

4.7

53

4.8 ... 54
4.9

11

55

4.10

56

4.11 . 57
4.12 .. 58
4.13 Ajax ... 59
4.11 Ajax

60

4.15 .

64

4.16 string Arduino

65

4.17 Serial Monitor 66


4.18 string .

67

4.19 67
4.20 post_data.php string.

68

4.21 .

69

4.22 Arduino..

70

4.23 Setup...

71

4.24 mysql_real_escape_string() ..

74

4.25 ... 75

12

3.1 ..

26

3.2 Arduino Uno 28


3.3 Arduino 38
4.1

63

4.2 ...

63

4.3 .. 63
4.4 Events..

63

4.5 .

63

4.6 ..

64

13

14

1.
1.1.

.
,
. ,
, ,
.
,
.

--- ..
,
,
.
.

1.2.

Arduino

, ,
.


,
,

15

,
.

1.3.


, ,
,
.
3 .
.
, .
4 .
5
.

16

17

2.
2.1.

[11]
.
,
. ,
, ,
.
, ,
, ,
,
.
, .
,

, ,
. ,
,

, ,

.


, ,
24
.

18

2.2.

[5]
, ,

. ,

, , ,
.

,
.

2.3.

[3], [8]

, , , , .
.


.

, ,
,
,
, ..

2.4.

PHP (Hypertext PreProcessor) [15],



HTML. HTML ,
19


HTML.
,
JavaScript,
.
.
,
,
.
, PHP
,

2.5.

,
, (computer on a chip).
VLSI ,
, Silicon Wafers.


. ,
.

.

.
[5]
1 0.
, assembly,
.
20

, ,
C.

. ,
.

, compiler () interpreter
(). ,
.

2.6.

Xampp

XAMPP [14] ,

http Apache, MySQL
PHP Perl.
XAMPP :

X ( "cross-platform"
)

Apache HTTP

MySQL

PHP

Perl

21

XAMPP

PHP/MySQL. Microsoft Windows, Linux,
Solaris, and Mac OS X
PHP, JSP Servlets.

Apache Web Server

. Apache [14],
, ,
Linux, Unix, Windows .. C
HTTP . ,
, PHP.
Perl, Python, Tcl, , SSL TLS.
, . PACHE,

,
.
,
, ,
.
. ,
PHP,
.

MySql Server

H MySql [17]
UNIX ,
Win32.
, MySql
standards SQL.

22


MySql
.

Arduino IDE

Arduino Arduino
IDE [21]. 3.

23

24

3.
3.1.

1 9 Arduino.
Arduino 9
. O 8
( ).
,
. /
.

3.1

25

3.2.


.

. .
:

Arduino Uno

25

Ethernet Shield

50

Hall

PIR

2xLed

0,20
:

84,20

3.2

3.3.

Arduino

Arduino
[20]
ATmega Atmel , software
,
. ,
,
/
,
.

26

Arduino

, .

Arduino Arduino.
Arduino
.

3.3.1.

Arduino


Arduino Uno.
:
-

Arduino Diecimila

Arduino Nuova Generarazione

Arduino Extreme

Arduino Usb

Arduino Lilypad

Arduino Nano

Arduino Mini

Arduino Duemilanove

Arduino Uno

Arduino Mega

27

Arduino

3.3.2.

Arduino Uno

3.2 Arduino Uno

ATMEGA328

5V

()

7-12V

6-20V

14(6 PWM )

40mA


3.3V

50mA

flash

32KB(ATMEGA328)

SRAM

2KB(ATMEGA328)

EEPROM

1KB(ATMEGA328)

16MHz

3.2 Arduino Uno

28

Arduino Uno
USB,
2.1mm ( ) -
Arduino. ,
7 12V ,
DC.

3.3 /

:
Vin: .
.
(jack), Vin.
5V:
5V. , , 5V
USB, Vin.
3.3V: FTDI.
50mA.
GND: .

29

ATmega328 . flash
memory, Arduino sketch, SRAM (static random access
memory), sketch
, EPPROM,
.
-

2K SRAM

, ..
runtime. ,
Arduino reset.

1K EEPROM

/ ( datatype) byte
runtime. SRAM, EEPROM
reset .

32K Flash, 2K

firmware Arduino . firmware


Arduino bootloader
USB,
hardware programmer. 30K Flash
,
. Flash, EEPROM
reset. , Flash
runtime ,
(2K SRAM + 1K
EEPROM),
(30K ).

30

Arduino interface. ATmega


, Arduino Serial-overUSB USB.

Arduino, Arduino
. , Arduino
14 , 0 13. 14
Arduino Uno
.
5 .
40 mA pull-up 20-50 k.
,
HIGH LOW, Arduino
.
LED .
,
(HIGH LOW)

.

14 ,

/ . :

3.4

31

0 1 RX TX

. ,
, USB
Serial-Over-USB, 0
.
interface, 2 / .

2 3 interrupt (interrupt 0

1 ).
,

. interrupt
.

3, 5, 6, 9, 10 11

PWM (Pulse Width Modulation),



.

3.5
-

Arduino, ANALOG IN,

6 pin, 0 5
1.1V ( 2 5V)
pin AREF
. , AREF 3.3V
32

1.65V, Arduino
512.

3.6 Arduino IDE

Arduino Uno

.
USB FTDI (drivers).

bootloader, .
Arduino Arduino IDE. Arduino
IDE Java :
-

(
sketch Arduino)
,
33

compiler sketch,

serial monitor

(USB),

Arduino
debugging sketch
-

sketch Arduino.

3.3.3.

Arduino Shields / Ethernet Shield

Shield
Arduino .
hardware plugin, addon extension
software. Arduino Shields
.

3.7 Shields
Shield Arduino
,
Shield. , Shield
34

Arduino
Shield. Shield
pin Arduino .
, Shield Arduino ( ..
Shield),
extender Shield Arduino
Shield ,
. Arduino, Shield
hardware
sketch
high level. Shield
Arduino :

Ethernet Shield: Arduino LAN


internet Ethernet.

WiFi Shield: Ethernet Shield, .

Shield : Arduino.
calculator OLED touchscreen
iPhone.

Wave Shield: Arduino /


SD.

GPS Shield: GPS Arduino ( ).

Motor Shields:
( DC, servo, stepper ..) Arduino.

ProtoShield: ,
Arduino Shield.

35

3.8 Ethernet Shield

Ethernet Shield,
Arduino .
Arduino Ethernet Shield Arduino
Ethernet.

3.3.4.

Sketch

IDE Arduino sketch


.
INO.

Arduino : (structure),
(values) (functions).

36

3.9

'//'. (//)
'/*' '*/' . ( /* *//)
IDE ,
debugging.

Arduino Wiring [19] C/C++


AVR ATmega,
C C++. compiler
AVR

gcc

AVR

libc.

C [6], Arduino [9], [4]


,
, C.
, ,
hardware Arduino.
Arduino : (structure), (values)
(functions).
:

37


setup()

I/O

HIGH LOW

pinMode()

INPUT OUTPUT

digitalWire()

if

true - false

digitalRead()

if..else

integer constants

for

floating point constants

loop()

switch case

I/O

analogReference()
analogRead()

while

void

do...while

boolean

break

(unsigned)char

tone(), noTone()

continue

byte

shiftOut()

return

(unsigned)int

pulseIn()

goto

word

analogWrite()
I/O

(unsigned)long

millis(), micros()

float

delay(), delayMicroseconts()

{}

double

// /* */

string ( char)

min(), max(),abs(), constraint(), map()

#define

String ()

pow(), sqrt(), sin(), cos(), tan()

#include

array

randomSeed(), random()

Bits/Bytes

char(), byte(), int(),


word()

lowByte(), highByte(), bitRate(),


bitWrite

+, -, *, /, %

long(), float()

bitSet(), bitClear(), bit()

Interrupts

==, !=

variable scope, static

attachInterrupt(), detachInterrupt()

<, >, <=, >=

volatile, const

interrupts(), noInterrupts()


&&, ||, !

sizeof()

Serial

3.3 Arduino

38

3.4.

:
-

Hall (Hall Effect) [12], [18]

PIR [13], [18]

3.4.1.

Hall (Hall Effect)

3.10 Hall
Hall [1], [10]
(
) / .
,
(DIS) .

.
" " ,
. ,
39


. Hall
"" on-off "" .
(AC)
, Hall

.

.
Hall :
, .
:
-

bit (1 , 0
)

,
.

Parallax Arduino

3.3Volt 5 Volt 100


,
.

/ .

40

3.4.2.

PIR

3.11 PIR

[1]
(Passive Infrared Sensor). ,

.
PIR ,
, ,
.
.
,
PIR. ,

.
galliumnitride,
41

. H 34 C,
.

3.12 PIR

,

.

.
, . ,
,

,
, .
. ,
9 10 m. ,
8 14 m

. , Fresnel
.
42


.
, IR
.
.
,
.
. PIR
Arduino.
:
-

bit (1 , 0
)

,
.

Parallax Arduino

3.3Volt 5 Volt 100


(
) Arduino
Pin . o ,
.

43

3.5.

3.13

Hall PIR
pull-up 10 k Data Sheets [12], [13]
Led
( HIGH).
(Breadboard) 3.13.

44

3.14 Breadboard

3.14
Breadboard .

45

46

4.
.

.
. ,

.
.

4.1.

Php, MySql
Ajax [15], [16], [17].
Web Server settings.php.

4.1


Hostname, ,
,
.
47

4.1.1.


.
.
// , ,
,
.

.
/ 4.2.

4.2 /

48

.
,
.
Session,
. Session
.

4.3 /

:
, .
:

49

4.4 Log Out


Session

.
:
:
-

Arduino

50

4.5

,
(log) .

51


:
-

,
( ), ( ),
.
, .
, Admin Settings:

4.6


:
52

4.7


.
, , Arduino ID
, Pin ID
Arduino, .
, .

53

4.8


.
, .

54

4.9


:
-

Member Settings
.
/ e-mail .
:

55

4.10

Sensors
,
.
-

rduino ID

56

4.11

Sensors
ID , .

57

4.12

4.1.2.

Ajax

AJAX (Asynchronous Javascript and XML)



rich Internet . AJAX, web
server background
.
, website
. ,
browser . ,
AJAX, web

58

bandwith
.
interface browser client
,
. ,
server
. AJAX, server ,
(scripts) style sheets , ,
JavaScript ,
. 4.13,
7 .

4.13 Ajax

59

4.12 Ajax

( 4.14) Ajax
( 4.13)
.

4.1.3.

CSS

CSS (Cascading Style Sheets- ) (


)

.
HTML XHTML,
. CSS

, ,
html.
CSS .
60

h1{
color: red;
fontsize: 12px;
}
h1, html tag
h1, selector
12 pixel.

4.1.4.

MySql (RDBMS)
,
. , ,
.

.
.
MySql,
phpMyAdmin, PHP
MySql . ,
MySql, ,
SQL , .
[7],
, , .
, ,

.

61


,
,
( ,
, ..), .
:
-


: ID, Arduino Device

ID Pin ID , , Type ID
, .
-


: ID

, .
-


: ID, ,

,
.
-


: ID .

ID (History ID), , Event ID, Device ID


, Pin ID, Zone ID Sensor ID.
-

Events
5 Events

. , ,

62

, . Event
ID .
,
:
Sensors
sensor_id

device_id

pin_id

sensor_name

type_id

zone_id

sensor_status

4.1

Sensor types
type_id

type_name

4.2

Zones
zone_id

zone_name zone_status

4.3

Events
event_id

event_name

4.4 Events

History
history_id

history_date event_id device_id

4.5

63

pin_id

zone_id

sensor_id

Users
user_id

username

password

email

date

user_level

4.6

,
( )
.
( 4.15) [2]
:

4.15

64

4.1.5.

Arduino

Arduino
Php:

arduino.php

String 4.16.
GET string arduino.php.

4.16 string Arduino


Arduino, DHCP Server
IP.
HTTP string
. string Pin ID
Arduino Device ID . string @
#.
&. , &
Pin ID Arduino
Device ID .
String.
4.16 string :
ID 3 8 Arduino ID 1. string
Arduino 5 Arduino ID

65

Serial Monitor
4.17.

4.17 Serial Monitor

post_data.php
.

string
post_data.php .
,
php string HTTP
, Pin ID, Device ID
Event ID ( 5). E 4.18 string
Arduino , :
Pin 3 Arduino ID 1.

66

4.18 string

string ( post_data.php)
(E 4.20),

4.18.

4.19

(4.19)
,
.

67

4.20 post_data.php string

String post_data.php
.
(event_id, pin_id, device_id )
Arduino.

4.2.

,
.
()

Loop.

.
.
Request String
String
.
String ( 4.1.5),

68

.
.

4.21

4.2.1.

(SPI,Ethernet,TimerOne) [21], [4]



.
( 4.22)
MAC Ethernet Shield, (
localhost IP), ,
Arduino (Device ID) .

69

4.22 Arduino

setup ( 4.23)
. setup
loop .
( loop), setup
noLoop();. .

70

4.23 Setup

71

Setup MAC Ethernet Shield


Request DHCP
Server.
2,3,4,5,6,7,8,9 (INPUT).
arduino.php Request String .
5
Timer1

TimerOne

4.2.2.

4.2.2.


,
.
,
.

SPI

(SPI)

. ,
.

SPI

, Arduino (master device).

Ethernet

Arduino
.
client .
(
). Arduino Uno Ethernet Shield
SPI. , digital pins 10,11,12 13 Ethernet
Shield.
72

TimerOne

TimerOne 16-bit
Timer1 ATmega168/328.

4.3.

.

.
.

4.3.1.

:
-

Session:

URL (sensors.php, zones.php, settings.php, ..)


Sessions.
,
session_start(); session $_SESSION['entrance']
$_SESSION['entrance']

redirect

admin_settings.php/user_settings.php.

admin_settings.php/member_settings.php

session_start(); $_SESSION['entrance'].
(/),
. Logout
Session
session_destroy(); .

73

mysql_real_escape_string:
SQL Injection

hackers. sql

.
. (Bug). ,
,emails Hackers .

.
PHP
.
, , escaped
o SQL server '
, .
addslashes() string, ,
, .
, directive
magic_quotes_gpc,
addslashes() gpc ($_GET, $_POST) .

mysql_real_escape_string().
String SQL,
charset .

4.24 mysql_real_escape_string()
74

mysql_real_escape_string()
( 4.24) .

4.3.2.


. string

string .

4.25

75

&

76

1.

,
, ,
Arduino.

.
Server
.


Internet of things [21].
(Internet of things),
, (Future
Internet).
,
, , ()
. ()

(mainframes, servers)
(Internet),
.

. ,

-

(..
, ..)

77


Zigbee,
,

LAN GPRS Modem


.

(
event).

Android iPhone.

78

[1] . . , , 2003.
[2] Batini C., Cesi S. and Navathe S., Conceptual Database Design: An EntityRelationship Approach,1992.
[3] Mahalik, N., Sensor Networks and Configuration: Fundamentals, Standards,
Platforms and Applications, 2007,
[4] Noble, J., Programming interactivity: A Designers Guide to Processing,
Arduino and openFramework, O Reilly Media,2009.
[5] Barr, M., Programming Embedded Systems in C and C ++, 1999
[6] Tan H., D orazio T., C , , 2000.
[7] Ramakrishnan R. and J. Gehrke, ,
, 2002.
[8] Ilyas M., Mahgoub I., Handbook of Sensor Networks: Compact Wireless and Wired
Sensing Systems, CRC Press, 2005.
[9] Evans, B., Arduino Programming Notebook, 2007.
[10] Lenz JE. A review of magnetic sensors, IEEE, 1990.
[11] . , , , 2009.
[12] Data Sheet of Hall Effect Sensor, US1881, 2006
[13] Data Sheet of PIR Sensor, SE-10
[14] Apache friends XAMPP: http://www.apachefriends.org, 2012
[15] PHP - Hypertext Preprocessor: http://www.php.net, 2012
[16] W3C Cascading Style Sheets: http://www.w3.org, 2012
[17] MySql - Open source database: http://www.MySql.com/, 2012
[18] Sensirion AG: http://www.sensirion.com, 2012
[19] Wiring: http://www.wiring.org.co/, 2012
[20] Arduino Homepage: http://www.arduino.cc, 2012
[21] The internet of things | Are you ready for the Internet of Things?:
http://www.theinternetofthings.eu/, 2012

79

You might also like