-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutterRestService.ino
248 lines (200 loc) · 8.24 KB
/
ShutterRestService.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
//----------------------------------------------------------------------------------------
//File: PhilipsHueApi.ino
//
//Autor: Tobias Rothlin
//Versions:
// 1.0 Inital Version
//----------------------------------------------------------------------------------------
//Include ESP 8266 library
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//Include humidity & temprature sensor library
#include <DHTesp.h>
//Buffer size for request url
#define REQ_BUF_SZ 50
//String to save the request url
char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;
//Sensor object
DHTesp dht;
//Local Wifi parameters
char* ssid = "wifiName";
char* password = "wifiPassword";
//Network parameters
IPAddress staticIP(192, 168, 1, 35); //ESP 8266 static Ip Adress
IPAddress gateway(192, 168, 1, 1); //Default gateway -> standard 192, 168, 1, 1
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, 1); //Default dns Server Ip -> standard 192, 168, 1, 1
WiFiServer server(80); // Port number
//Enum to define the current postiton of the shutter
enum ShutterState {
Up,
Down,
};
//Var to save the position of the shutter
enum ShutterState ShutterPosition;
//Defining the Ports on the ESP 8266
const int DownRelays = 4; //D2
const int UpRelays = 5; //D1
const int StopRelays = 12; //D6
const int ReserveRelays = 17; //NotConnected;
const int ConnectedLEd = 14; //D5
const int dhtPin = 13; //D7
//Duration the Relays are switch on
const int delayForRelays = 4000;
//Inital setup
void setup() {
//Serial communication @Bauderate 115200
Serial.begin(115200);
//Initalise the temp/humid sensor
dht.setup(dhtPin, DHTesp::DHT11);
//Define the IO ports as outputs
pinMode(DownRelays, OUTPUT);
pinMode(UpRelays, OUTPUT);
pinMode(StopRelays, OUTPUT);
pinMode(ReserveRelays, OUTPUT);
pinMode(ConnectedLEd, OUTPUT);
//Set the output ports to LOW
digitalWrite(DownRelays, LOW);
digitalWrite(UpRelays, LOW);
digitalWrite(StopRelays, LOW);
digitalWrite(ReserveRelays, LOW);
digitalWrite(ConnectedLEd, LOW);
//Connect to the Wifi and print out the status via the serial output
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Waits until the ESP 8266 has connected to the Wifi
delay(500);
Serial.print(".");
}
//Prints out the IP address should be the same as specified in line 35
//if not the WiFi.coinfg(....) in line 90 could be wrong. Depending on the version of the
//lib and verison of the esp8266 the parameter subnet & staticIP are switched.
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(ConnectedLEd, HIGH); //Sets the status LED to HIGH to know that the ESP 8266 is ready for use
server.begin(); //Enabels the server
ShutterPosition = Up; //Deflaut the shutter position is up!
}
void loop() {
WiFiClient client = server.available();
if (client) { //Polling to check if a new request has been sent
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read(); //Gets the next Character in the request string
if (req_index < (REQ_BUF_SZ - 1)) { //Appends the Character to the HTTP_req string
HTTP_req[req_index] = c;
req_index++;
}
Serial.print(c); //Used for debuging the input string
if (c == '\n' && currentLineIsBlank) { //Enters this if statement when at the end of the request string
//Parsing the request
String httpRequest = HTTP_req;
Serial.println("HTTP_req:" + httpRequest + ":");
int startPos = httpRequest.indexOf(" /");
httpRequest = httpRequest.substring(startPos + 1);
int endPos = httpRequest.indexOf(" HTTP");
httpRequest = httpRequest.substring(0, endPos);
httpRequest.trim();
//Output the request string
Serial.println("Request:" + httpRequest + ":");
//Get the function name as Stirng
int CGIPos = httpRequest.indexOf("cgi-local");
String functionName = httpRequest.substring(CGIPos + 10);
//Output the function name as String
Serial.println("FunctionName:" + functionName + ":");
//Compares the functionName to the known functions
if (functionName == "get-sensor-values") {
//gets the values from the sonsor
TempAndHumidity newValues = dht.getTempAndHumidity();
//Sends the response header back as indicates that the response will be a json struct
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Content-type: application/json");
client.println();
//Sends the json struct back with Temp/ Hum/ State
client.print("{\"id\": ");
client.print(1);
client.print(", \"Temp\": \"");
client.print(newValues.temperature);
client.print("\", \"Hum\": \"");
client.print(newValues.humidity);
client.print("\", \"State\": \"");
if(ShutterPosition == Up){
client.print("UP");
}
else if (ShutterPosition == Down){
client.print("Down");
}
client.print("\"}");
}
//if the functionName is not to get the sensor values it must be a shutter operation
else {
//Pasring the specific shutter operation
int shuttersPos = functionName.indexOf("shutters");
String shutterOperation = functionName.substring(shuttersPos + 9);
//Outputs the shutterOperation to perform
Serial.println("shutterOperation:" + shutterOperation + ":");
//Three shutterOperations -> Up,Down,Stop
if (shutterOperation == "Up") {
//Sets the Relays to HIGH -> conection waits the specified lenght in line 62 -> sets Relays to LOW
digitalWrite(UpRelays, HIGH);
delay(delayForRelays);
digitalWrite(UpRelays, LOW);
//Set the shutter pistiton to Up
ShutterPosition = Up
}
if (shutterOperation == "Down") {
digitalWrite(DownRelays, HIGH);
delay(delayForRelays);
digitalWrite(DownRelays, LOW);
ShutterPosition = Down;
}
if (shutterOperation == "Stop") {
digitalWrite(StopRelays, HIGH);
delay(delayForRelays);
digitalWrite(StopRelays, LOW);
}
//Sends the response header back as indicates that the response will be a json struct
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Content-type: application/json");
client.println();
//Sends the json struct back with the performed shutterOperation
client.print("{\"id\": ");
client.print(1);
client.print(", \"shtterOperation\": \"");
client.print(shutterOperation);
client.print("\"}");
}
//Cleans up
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
//Terminates the connection the client
delay(1);
client.stop();
}
}
//Clears the char array used to store the inital request string
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}