-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeAutomationLocal.ino
288 lines (258 loc) · 7.12 KB
/
HomeAutomationLocal.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
const int numOfDevices = 4;
int devices[numOfDevices] = {22, 24, 26, 28};
int deviceOffline[numOfDevices] = {23, 25, 27, 29};
bool stateOffline[numOfDevices] = {false, false, false, false};
#define MAX_STRING_LEN 20
char tagStr[MAX_STRING_LEN] = "";
char dataStr[MAX_STRING_LEN] = "";
char tmpStr[MAX_STRING_LEN] = "";
char endTag[3] = {'<', '/', '\0'};
int len;
int count = 0;
boolean tagFlag = false;
boolean dataFlag = false;
byte mac[] = { 0x30, 0x10, 0xB3, 0x48, 0x9A, 0x96 };
IPAddress ip(192, 168, 0 , 200);
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = {255, 255, 255, 0};
byte server[] = { 192, 168, 0, 100};
//char server[] = "";
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.println("Connecting...");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
for (int i = 0; i < numOfDevices; i++) {
pinMode(devices[i], OUTPUT);
pinMode(deviceOffline[i], INPUT);
}
delay(1000);
}
int x = 0;
void loop() {
if (client.connect(server, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /test_IOT/test.xml HTTP/1.1");
client.println( "Host: 192.168.0.100");
client.println();
client.println("Connection: close");
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while (client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) {
xmlread();
}
client.stop();
client.flush();
Serial.println("Disconnecting");
//delay(250);
physicalControl();
x++;
Serial.println(x);
}
String tempStr;
void physicalControl() {
Serial.println("......................................................It goes well.");
for (int n = 0; n < numOfDevices; n++) {
if (digitalRead(deviceOffline[n]) == HIGH) {
delay(300);
Serial.println("Changed");
stateOffline[n] = !stateOffline[n];
if (stateOffline[n]) {
Serial.println("Make HIGH");
switch (n) {
case 0:
tempStr = "state=ON&number=0";
break;
case 1:
tempStr = "state=ON&number=1";
break;
case 2:
tempStr = "state=ON&number=2";
break;
case 3:
tempStr = "state=ON&number=3";
}
} else {
Serial.println("Make LOW");
switch (n) {
case 0:
tempStr = "state=OFF&number=0";
break;
case 1:
tempStr = "state=OFF&number=1";
break;
case 2:
tempStr = "state=OFF&number=2";
break;
case 3:
tempStr = "state=OFF&number=3";
}
}
if (client.connect(server, 80)) { //starts client connection, checks for connection
Serial.println("connected...............2nd time");
}
else {
Serial.println("connection failed.................2nd Time"); //error message if no client connect
Serial.println();
}
Serial.println(".........................Reading data from offline...........");
if (client.connected() || client.available()) {
Serial.println("Really");
client.print("GET /test_IOT/xmlupdate.php?");
client.print(tempStr);
client.println("HTTP/1.1");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println( "Host: 192.168.0.100");
//client.println("Cache-Control: no-cache");
//client.println("User-Agent: Arduino/1.0");
//client.println("Accept: */*");
client.println("Connection: close");
//client.print("Content-Length: ");
//client.println(tempStr.length());
//Serial.println(tempStr);
//client.println();
client.println();
//client.println("Connection: close");
client.stop();
Serial.println("Disconnecting................................2nd Time");
client.flush();
}
}
}
}
void xmlread() {
char inChar = client.read();
if (inChar == '<') {
addChar(inChar, tmpStr);
tagFlag = true;
dataFlag = false;
} else if (inChar == '>') {
addChar(inChar, tmpStr);
if (tagFlag) {
strncpy(tagStr, tmpStr, strlen(tmpStr) + 1);
}
clearStr(tmpStr);
tagFlag = false;
dataFlag = true;
} else if (inChar != 10) {
if (tagFlag) {
addChar(inChar, tmpStr);
if ( tagFlag && strcmp(tmpStr, endTag) == 0 ) {
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
}
}
if (dataFlag) {
addChar(inChar, dataStr);
}
}
if (inChar == 10 ) {
if (matchTag("<state>")) {
//Serial.print("state: ");
//Serial.println(dataStr);
//Serial.print("length--");
//Serial.println(strlen(dataStr));
count = count + 1;
//Serial.println(count);
devicescontrol(count, dataStr);
if (count == 4) {
count = 0;
clearStr(tmpStr);
clearStr(tagStr);
clearStr(dataStr);
//client.stop();
//client.flush();
//Serial.println("disconnecting.");
}
}
clearStr(tmpStr);
clearStr(tagStr);
clearStr(dataStr);
tagFlag = false;
dataFlag = false;
}
}
void clearStr (char* str) {
int len = strlen(str);
for (int c = 0; c < len; c++) {
str[c] = 0;
}
}
void addChar (char ch, char* str) {
char *tagMsg = "<ERROR>";
char *dataMsg = "-ERROR";
if (strlen(str) > MAX_STRING_LEN - 2) {
if (tagFlag) {
clearStr(tagStr);
strcpy(tagStr, tagMsg);
}
if (dataFlag) {
clearStr(dataStr);
strcpy(dataStr, dataMsg);
}
clearStr(tmpStr);
tagFlag = false;
dataFlag = false;
}
else {
str[strlen(str)] = ch;
}
}
boolean matchTag (char* searchTag) {
if ( strcmp(tagStr, searchTag) == 0 ) {
return true;
} else {
return false;
}
}
void devicescontrol(int devicescount, char *devicestate) {
if (!strncmp("ON", devicestate, 2)) {
//Serial.print("on..........on......");
//Serial.println(devicescount);
digitalWrite(devices[devicescount - 1], HIGH);
stateOffline[devicescount-1] = true;
updateSerialMonitor(devicescount, true);
}
else if (!strncmp("OFF", devicestate, 3)) {
//Serial.print("off......off.......");
//Serial.println(devicescount);
digitalWrite(devices[devicescount - 1], LOW);
stateOffline[devicescount-1] = false;
updateSerialMonitor(devicescount, false);
}
}
void updateSerialMonitor(int deviceNo, bool state) {
switch (deviceNo) {
case 1:
Serial.print(">Switch-1 \t");
dispState(state);
break;
case 2:
Serial.print(">Switch-2 \t");
dispState(state);
break;
case 3:
Serial.print(">Switch-3 \t");
dispState(state);
break;
case 4:
Serial.print(">Switch-4 \t");
dispState(state);
}
}
void dispState(bool state) {
if (state) Serial.println("ON");
else Serial.println("OFF");
}