-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino-home-automation.ino
236 lines (186 loc) · 6.42 KB
/
Arduino-home-automation.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
// Include libraries
#include <SPI.h>
#include <Ethernet2.h>
#include <NewRemoteReceiver.h>
#include <NewRemoteTransmitter.h>
#include <SD.h>
#include <OneWire.h>
#include <EEPROM.h>
// Create MAC
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFD };
// Create IP address
// IPAddress ip(192,168,1,102);
// Create the server
EthernetServer server(80);
// To save whether the light is on or not
NewRemoteTransmitter transmitter(123, 3, 260, 3); // Create transmitter
String HTTP_req; // stores the HTTP request
File webFile;
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
// Let's do the setup
void setup()
{
Ethernet.begin(mac); // Initialize Ethernet device
server.begin(); // Start to listen for clients
// Initialize SD card
if(!SD.begin(4)){
return; // init failed
}
// check for index.htm file
if (!SD.exists("index.htm")) {
return; // can't find index file
}
}
void loop()
{
EthernetClient client = server.available(); // Try to get a client
if(client) // If there is a client
{
boolean currentLineIsBlank = true; // Blank lines
while(client.connected()){ // If client is connected
if(client.available()){ // If client is available
char c = client.read(); // Read 1 byte/character from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if(c == '\n' && currentLineIsBlank){ // If response is a blank line
// Send standard http response header
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Connection: close"));
client.println();
if(HTTP_req.indexOf("button") > -1){// Check for ajax
handle_light_ajax();
}
else if(HTTP_req.indexOf("getTemperature") > -1){
client.println(getTemp());
}
else {
// Let's send our webpage
webFile = SD.open("index.htm"); // Open webpage
if(webFile){
int device = 1; // Start at device one
char output[16];
byte out;
while(webFile.available()){
for(int i = 0; i < sizeof(output) + 5; i++){
out = webFile.read();
if(out == 94){
if(read_config(device) == 1){
out = 49;
}
else{
out = 48;
}
device++;
}
if(out == 255){out = 0;}
output[i] = out;
}
client.write(output);
}
webFile.close(); // Close the file
}
}
HTTP_req = ""; // finished with request, empty string
break;
} // End if c == '\n' && currentLineIsBlank
if(c == '\n'){
// last character on line of receiracter read
currentLineIsBlank = true;
}
else if(c != '\r'){
// A text character was received from client
currentLineIsBlank = false;
}
} // End if client.available()
} // End while client.connected()
delay(1); // Give the browser time to receive the data
client.stop(); // Close the connection
} // End if client
}
/*
* Process a light from GET request *
*/
void handle_light_ajax(){
int startUrlIndex= HTTP_req.indexOf("button");
int endUrlIndex = HTTP_req.indexOf(" HTTP");
String url = HTTP_req.substring(startUrlIndex, endUrlIndex);
int startButtonIndex = url.indexOf("device-") + 7;// 7 is length of device-, I really just want the number.
int endButtonIndex = url.indexOf("&");
String button = url.substring(startButtonIndex, endButtonIndex);
int startStateIndex = url.indexOf("state=") + 6; // 6 is length of state=, I really just want the number.
String state = url.substring(startStateIndex);
int device = button.toInt();
int newState = state.toInt();
send_signal(device, newState, "toggle");
write_config(device, newState); // Save state
}
/*
* Dim a light
*/
bool send_signal(int unit, int level, String button_type) {
if (level == 0) {
transmitter.sendUnit(unit, false);
return true;
}
else{
if(button_type == "dimmer"){
if (level >= 1 && level <= 12) {
transmitter.sendDim(unit, level);
return true;
}
else {
return false;
}
}
else if(button_type == "toggle"){
transmitter.sendUnit(unit, true);
return true;
}
else{
return false;
}
}
}
void write_config(int address, byte value){
EEPROM.write(address, value);
}
int read_config(int address){
return EEPROM.read(address);
}
/*
* Get the temperature from the temperature sensor
*/
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return -1001;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
return -1002;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}