ปิดปิดไฟบ้านผ่านมือถือ
ชุดคิทโปรเจค arduino นี้จะทำ arduino เชื่อมต่อ Bluetooth Module HC06 กับ โทรศัพท์มือถือ สมาร์ทโฟน แท็บเล็ต สั่งงานจาก โทรศัพท์มือถือ ผ่าน Bluetooth ส่งค่าการกดปุ่มต่างๆในมือถือ เข้ามาให้ arduino ประมวลผล เพื่อสั่งงานรีเลย์ ให้ควบคุมเปิดปิดไฟบ้าน และ เครื่องใช้ไฟฟ้าต่างๆ ภายในบ้าน
/*
2
3
Software serial multple serial test
4
5
6
Receives from the hardware serial, sends to software serial.
7
8
Receives from software serial, sends to hardware serial.
9
10
11
The circuit:
12
13
* RX is digital pin 2 (connect to TX of other device)
14
15
* TX is digital pin 3 (connect to RX of other device)
16
17
18
Note:
19
20
Not all pins on the Mega and Mega 2560 support change interrupts,
21
22
so only the following can be used for RX:
23
24
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
25
26
27
Not all pins on the Leonardo support change interrupts,
28
29
so only the following can be used for RX:
30
31
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
32
33
34
created back in the mists of time
35
36
modified 25 May 2012
37
38
by Tom Igoe
39
40
based on Mikal Hart's example
41
42
43
This example code is in the public domain.
44
45
46
*/
47
48
#include <SoftwareSerial.h>
49
50
int i =0;
51
char str[11]={'0','0','0','0','0','0','0','0','0','0','0'};
52
SoftwareSerial mySerial(2, 3); // RX, TX
53
54
55
void setup()
56
57
{
58
pinMode(8, OUTPUT);
59
pinMode(9, OUTPUT);
60
pinMode(10, OUTPUT);
61
pinMode(11, OUTPUT);
62
digitalWrite(8, HIGH);
63
digitalWrite(9, HIGH);
64
digitalWrite(10, HIGH);
65
digitalWrite(11, HIGH);
66
Serial.begin(9600);
67
68
while (!Serial) ;
69
70
mySerial.begin(9600);
71
72
}
73
74
75
void loop()
76
77
{
78
79
if (mySerial.available()){
80
//Serial.write(mySerial.read());
81
//Serial.println("ok");
82
i=i+1;
83
str[i]=mySerial.read();
84
//Serial.println(str[i]);
85
if(i ==9)
86
i=0;
87
if(str[5] == '1' && str[7] == '3')
88
digitalWrite(8, LOW);
89
if(str[5] == '1' && str[7] == '2')
90
digitalWrite(8, HIGH);
91
if(str[5] == '2' && str[7] == '3')
92
digitalWrite(9, LOW);
93
if(str[5] == '2' && str[7] == '2')
94
digitalWrite(9, HIGH);
95
if(str[5] == '3' && str[7] == '3')
96
digitalWrite(10, LOW);
97
if(str[5] == '3' && str[7] == '2')
98
digitalWrite(10, HIGH);
99
if(str[5] == '4' && str[7] == '3')
100
digitalWrite(11, LOW);
101
if(str[5] == '4' && str[7] == '2')
102
digitalWrite(11, HIGH);
103
}
104
105
106
if (Serial.available())
107
108
mySerial.write(Serial.read());
109
110
}
แหล่งที่มา
http://www.myarduino.net/product/1810/%E0%B8%8A%E0%B8%B8%E0%B8%94%E0%B8%84%E0%B8%B4%E0%B8%97%E0%B9%82%E0%B8%9B%E0%B8%A3%E0%B9%80%E0%B8%88%E0%B8%84-arduino-%E0%B9%
80%E0%B8%9B%E0%B8%B4%E0%B8%94%E0%B8%9B%E0%B8%B4%E0%B8%94%E0%B9%84%E0%B8%9F%
E0%B8%9A%E0%B9%89%E0%B8%B2%E0%B8%99%E0%B8%9C%E0%B9%88%E0%B8%B2%E0%B8%99%E0%
B8%A1%E0%B8%B7%E0%B8%AD%E0%B8%96%E0%B8%B7%E0%B8%AD-%E0%B9%80%E0%B8%8A%E0%B8%
B7%E0%B9%88%E0%B8%AD%E0%B8%A1%E0%B8%95%E0%B9%88%E0%B8%AD-bluetooth
ความคิดเห็น
แสดงความคิดเห็น